Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy
- from os import devnull
- from random import randrange, uniform
- from subprocess import Popen, PIPE
- width = 640
- height = 360
- boxsize = height / 8
- quality = 18.0 # 0 - 51, smaller = higher bitrate
- framerate = '30000:1001' # 29.97 fps (NTSC)
- numframes = 900
- outfilename = '20120419.mp4'
- def rectangle(frame, top, left, w, h, Y, P):
- top, left, bot, right = map(lambda x: int(round(x)), (top, left, top + h, left + w))
- frame[0, top:bot, left:right] = round(Y * 219 + 16) # Y' [0, 1] -> Y' [16, 235]
- frame[1, top:bot, left:right] = round(P[0] * 224 + 128) # Pb [-0.5, 0.5] -> Cb [16, 240]
- frame[2, top:bot, left:right] = round(P[1] * 224 + 128) # Pr [-0.5, 0.5] -> Cr [16, 240]
- def bouncing(min, max, delta, callback=None):
- val = uniform(min, max)
- while True:
- yield val
- val += delta
- if val < min or val > max:
- val = 2 * min - val if val < min else 2 * max - val
- delta *= -1
- if callback:
- callback()
- class Randomchroma(object):
- def __init__(self): self.picknew()
- def picknew(self): self.val = uniform(-0.5, 0.5), uniform(-0.5, 0.5)
- frame = numpy.zeros((3, height, width), dtype=numpy.uint8)
- chroma = Randomchroma()
- luma = bouncing(0.15, 0.9, 0.033)
- x = bouncing(0, width - boxsize, uniform(height / 200, height / 20), lambda: chroma.picknew())
- y = bouncing(0, height - boxsize, uniform(height / 200, height / 20), lambda: chroma.picknew())
- ffmpeg = Popen('ffmpeg -y -f yuv4mpegpipe -i - -vcodec libx264 -profile high -level 5 -x264opts crf={0} {1}'
- .format(quality, outfilename).split(), stdin=PIPE, stdout=open(devnull), stderr=open(devnull))
- ffmpeg.stdin.write('YUV4MPEG2 W%d H%d F%s Ip A0:0 C444\n' % (width, height, framerate))
- for f in xrange(numframes):
- rectangle(frame, 0, 0, width, height, 0, (0, 0))
- rectangle(frame, next(y), next(x), boxsize, boxsize, next(luma), chroma.val)
- ffmpeg.stdin.write('FRAME\n')
- ffmpeg.stdin.write(frame.tostring())
- ffmpeg.stdin.close()
- ffmpeg.communicate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement