Skip to content

Instantly share code, notes, and snippets.

@dnicolson
Created April 29, 2014 03:57
Show Gist options
  • Save dnicolson/11390332 to your computer and use it in GitHub Desktop.
Save dnicolson/11390332 to your computer and use it in GitHub Desktop.
HTML5 Video Batch
"""
Dependencies:
brew remove ffmpeg
brew install --with-libvpx --with-theora --with-libvorbis ffmpeg
Usage:
python web-video.py /path/to/videos
"""
import sys, os
import glob, re, subprocess, json
args = {'mp4': '-c:v libx264 -movflags +faststart',
'ogv': '-codec:v libtheora -qscale:v 7 -codec:a libvorbis -qscale:a 5',
'webm': '-c:v libvpx -qmin 0 -qmax 50 -crf 5 -b:v 1M -c:a libvorbis',
'jpg': '-ss 0 -vframes 1 -vcodec mjpeg -f image2'}
def get_file_list():
file_list = glob.glob(sys.argv[1] + '/*') + glob.glob(sys.argv[1] + '/*/**')
video_list = []
for file_path in file_list:
if re.match('.*\.mov|.*\.mp4',file_path,re.IGNORECASE):
video_list.append(file_path)
return video_list
def is_video_upside_down(video):
output = subprocess.check_output(['ffprobe','-v','quiet','-print_format','json','-show_streams','-select_streams','v'] + [video])
try:
return json.loads(output)['streams'][0]['tags']['rotate'] == '180'
except:
pass
return False
def encode_video(video):
extra_args = []
if (is_video_upside_down(video)):
print 'Rotating %s...' % video
extra_args = ['-metadata:s:v','rotate=0','-vf','hflip,vflip']
for arg in args:
output = os.path.splitext(os.path.split(video)[1])[0] + '.' + arg
print 'Encoding %s...' % output
subprocess.call(['ffmpeg','-v','quiet','-i'] + [video] + extra_args + args[arg].split(' ') + [output])
if __name__ == '__main__':
for video in get_file_list():
encode_video(video)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment