Advertisement
eXFq7GJ1cC

Untitled

Apr 19th, 2012
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.09 KB | None | 0 0
  1. import numpy
  2. from os import devnull
  3. from random import randrange, uniform
  4. from subprocess import Popen, PIPE
  5.  
  6. width = 640
  7. height = 360
  8. boxsize = height / 8
  9. quality = 18.0                  # 0 - 51, smaller = higher bitrate
  10. framerate = '30000:1001'        # 29.97 fps (NTSC)
  11. numframes = 900
  12. outfilename = '20120419.mp4'
  13.  
  14. def rectangle(frame, top, left, w, h, Y, P):
  15.     top, left, bot, right = map(lambda x: int(round(x)), (top, left, top + h, left + w))
  16.     frame[0, top:bot, left:right] = round(Y * 219 + 16)         # Y' [0, 1] -> Y' [16, 235]
  17.     frame[1, top:bot, left:right] = round(P[0] * 224 + 128)     # Pb [-0.5, 0.5] -> Cb [16, 240]
  18.     frame[2, top:bot, left:right] = round(P[1] * 224 + 128)     # Pr [-0.5, 0.5] -> Cr [16, 240]
  19.  
  20. def bouncing(min, max, delta, callback=None):
  21.     val = uniform(min, max)
  22.     while True:
  23.         yield val
  24.         val += delta
  25.         if val < min or val > max:
  26.             val = 2 * min - val if val < min else 2 * max - val
  27.             delta *= -1
  28.             if callback:
  29.                 callback()
  30.  
  31. class Randomchroma(object):
  32.     def __init__(self): self.picknew()
  33.     def picknew(self):  self.val = uniform(-0.5, 0.5), uniform(-0.5, 0.5)
  34.  
  35. frame = numpy.zeros((3, height, width), dtype=numpy.uint8)
  36. chroma = Randomchroma()
  37. luma = bouncing(0.15, 0.9, 0.033)
  38. x = bouncing(0, width - boxsize, uniform(height / 200, height / 20), lambda: chroma.picknew())
  39. y = bouncing(0, height - boxsize, uniform(height / 200, height / 20), lambda: chroma.picknew())
  40.  
  41. ffmpeg = Popen('ffmpeg -y -f yuv4mpegpipe -i - -vcodec libx264 -profile high -level 5 -x264opts crf={0} {1}'
  42.                .format(quality, outfilename).split(), stdin=PIPE, stdout=open(devnull), stderr=open(devnull))
  43. ffmpeg.stdin.write('YUV4MPEG2 W%d H%d F%s Ip A0:0 C444\n' % (width, height, framerate))
  44.  
  45. for f in xrange(numframes):
  46.     rectangle(frame, 0, 0, width, height, 0, (0, 0))
  47.     rectangle(frame, next(y), next(x), boxsize, boxsize, next(luma), chroma.val)
  48.     ffmpeg.stdin.write('FRAME\n')
  49.     ffmpeg.stdin.write(frame.tostring())
  50.  
  51. ffmpeg.stdin.close()
  52. ffmpeg.communicate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement