Skip to content

Instantly share code, notes, and snippets.

@twocity
Created September 4, 2017 14:09
Show Gist options
  • Save twocity/84bbe6e39f7344a835098b487a66f7f5 to your computer and use it in GitHub Desktop.
Save twocity/84bbe6e39f7344a835098b487a66f7f5 to your computer and use it in GitHub Desktop.
compress video through ffmpeg
#!/usr/bin/env python
import sys
import subprocess
import os
from os.path import basename
file = sys.argv[1]
command = ['ffmpeg', '-y']
command.extend(['-i', file])
# encode as x264
command.extend(['-vcodec', 'libx264'])
command.extend(['-profile:v', 'main', '-level', '4.0'])
command.extend(['-preset', 'slower'])
command.extend(['-crf', '25'])
# opt for web
command.extend(['-movflags', '+faststart'])
# remove audio track
command.extend(['-an'])
out_file = os.path.splitext(file)[0] + '_.mp4'
command.extend([out_file])
print 'Running commmand:\n%s' % ' '.join(command)
FNULL = open(os.devnull, 'w')
subprocess.call(command,stdout=FNULL, stderr=subprocess.STDOUT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment