scripts/tools/encode-vp9.py

84 lines
2.4 KiB
Python
Executable File

#!/usr/bin/python
import os
import re
import sys
import getopt
import lib.helpers as helpers
from subprocess import call
__author__ = 'lanxu <jukka.lankinen@gmail.com>'
def main(argv):
ffmpeg_available = helpers.is_exe("/usr/bin/ffmpeg")
inputFile = ''
outputFile = ''
loglevel = 'error'
#bitrate = '2000k'
bitrate = '0'
crf = '15'
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', bitrate, '-crf', crf, '-threads','16', '-slices','16','-cpu-used','-4', '-f', 'null', '-y', '/dev/null']
command_pass2 = ['ffmpeg', '-i', inputFile, '-loglevel',loglevel,'-c:v', 'libvpx-vp9', '-pass', '2', '-b:v', bitrate, '-crf', crf, '-threads','16', '-slices','16','-cpu-used','-4', outputFile]
# Run commands
print('Input file is "'+inputFile+'"')
print('Output file is "'+outputFile+'"')
print('Running pass 1...')
try:
val = helpers.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:
val = helpers.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:])