scripts/encodeVP9.py
2015-04-25 19:16:45 +03:00

94 lines
2.7 KiB
Python
Executable File

#!/usr/bin/python
import os
import re
import sys
import getopt
from subprocess import call
__author__ = 'lanxu <jukka.lankinen@gmail.com>'
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
def run_command(command):
try:
return_value = call(command)
except (RuntimeError, TypeError, NameError, OSError) as error:
raise
return return_value
def main(argv):
ffmpeg_available = is_exe("/usr/bin/ffmpeg")
inputFile = ''
outputFile = ''
loglevel = 'error'
try:
opts, args = getopt.getopt(argv,"hi:o:v",["ifile=","ofile="])
except getopt.GetoptError:
print('test.py -i <inputfile> -o <outputfile>')
sys.exit(1)
for opt, arg in opts:
if opt == '-h':
print('encodeVP9.py -i <inputfile> -o <outputfile> [-v]')
sys.exit()
elif opt in ("-v", "--verbose"):
loglevel = 'verbose'
elif opt in ("-i", "--ifile"):
inputFile = arg
elif opt in ("-o", "--ofile"):
outputFile = arg
if ffmpeg_available:
"""
Settings from:
http://wiki.webmproject.org/ffmpeg/vp9-encoding-guide
`VOD Recommended Settings`
"""
# Disable colors
os.environ['AV_LOG_FORCE_NOCOLOR'] = '1'
# Define commands
command_pass1 = ['ffmpeg', '-i', inputFile, '-loglevel',loglevel,'-c:v', 'libvpx-vp9', '-pass', '1', '-b:v', '1000K', '-threads', '8', '-speed', '4', '-tile-columns', '6', '-frame-parallel', '1', '-auto-alt-ref', '1', '-lag-in-frames', '25', '-an', '-f', 'webm', '-y', '/dev/null']
command_pass2 = ['ffmpeg', '-i', inputFile, '-loglevel',loglevel,'-c:v', 'libvpx-vp9', '-pass', '2', '-b:v', '1000K', '-threads', '8', '-speed', '1', '-tile-columns', '6', '-frame-parallel', '1', '-auto-alt-ref', '1', '-lag-in-frames', '25', '-c:a', 'libopus', '-b:a', '64k', '-f', 'webm', outputFile]
# Run commands
print('Input file is "'+inputFile+'"')
print('Output file is "'+outputFile+'"')
print('Running pass 1...')
try:
val = run_command(command_pass1)
# ffmpeg returns 0 if success
if val > 0:
print('Encoding failed')
sys.exit(2)
except:
# TODO Remove temp files here
print('Encoding failed!')
sys.exit(2)
print('Running pass 2..')
try:
run_command(command_pass2)
# ffmpeg returns 0 if success
if val > 0:
print('Encoding failed')
sys.exit(2)
except:
# TODO Remove temp files here (or backup pass 1 files for next run)
print('Encoding failed!')
sys.exit(2)
print('Done.')
if __name__ == "__main__":
main(sys.argv[1:])