-
-
Save antiface/c0f09038973cdd5399dc079368cf2940 to your computer and use it in GitHub Desktop.
A simple idiom to gather bitmaps and make a movie using python and ffmpeg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import glob | |
import shutil | |
import subprocess | |
#ffmepg parameters | |
# file sequence format | |
dstform = u'a{seq:08d}.png' # python style | |
dstform_c = u'a%08d.png' # C and ffmpeg style | |
# base frame rate per picture (frames) | |
frpp = 5 | |
# video framerate (fps) | |
vfr = 25 | |
# video bandwidth | |
vbw = '2000k' | |
# output video | |
output = u'video.mp4' | |
def src_yield(): | |
# source | |
srces = sorted(glob.glob('../p????????.png')) | |
first = srces[0] | |
last = srces[-1] | |
leading_count = 5 | |
# prologue | |
for i in range(leading_count): | |
yield first | |
for i in srces: | |
yield i | |
# epilogue | |
for i in range(leading_count): | |
yield last | |
for i, s in enumerate(src_yield(), 1): | |
d = dstform.format(seq=i) | |
print(s, '->', d) | |
shutil.copy(s, d) | |
# ffmpeg command line | |
cmdline = ['ffmpeg', | |
'-r', '{frpp:d}'.format(frpp=frpp), | |
'-i', '{dstform_c:s}'.format(dstform_c=dstform_c), | |
'-b:v', '{vbw:s}'.format(vbw=vbw), | |
'-r', '{vfr:d}'.format(vfr=vfr), | |
'-vf', '"scale=trunc(iw/2)*2:trunc(ih/2)*2"', # if pix is odd, trancate it (http://stackoverflow.com/questions/20847674/ffmpeg-libx264-height-not-divisible-by-2) | |
'-pix_fmt', 'yuv420p', # for compatibility to windows media player; discussion in http://ffmpeg.org/pipermail/ffmpeg-user/2014-March/020455.html | |
'{output:s}'.format(output=output)] | |
try: | |
retcode = subprocess.call(cmdline) | |
if retcode < 0: | |
print('process is aborted by signal', -retcode) | |
else: | |
print('process is normaly terminated', retcode) | |
except OSError as e: | |
print('os error', e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment