Skip to content

Instantly share code, notes, and snippets.

@takaakiaoki
Last active August 6, 2016 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save takaakiaoki/7784952 to your computer and use it in GitHub Desktop.
Save takaakiaoki/7784952 to your computer and use it in GitHub Desktop.
A simple idiom to gather bitmaps and make a movie using python and ffmpeg
#!/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