Skip to content

Instantly share code, notes, and snippets.

@Tatsh
Last active December 18, 2015 05:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tatsh/5737088 to your computer and use it in GitHub Desktop.
Save Tatsh/5737088 to your computer and use it in GitHub Desktop.
import glob
import subprocess as sp
from os import remove as rm
DIVX2PASS_GLOB = '*2pass*.log*'
def clean_up():
files = glob.glob(DIVX2PASS_GLOB)
files.extend(['audio.aac', 'output.x264'])
for filename in files:
try:
rm(filename)
except OSError as e:
print('%s: %s' % (e.filename, e.strerror))
def shell_exec(command_line):
print(command_line)
return sp.call(command_line, shell=True)
def encode_to_aac(input_file, bitrate='128k', output_file='audio.aac'):
shell_exec('ffmpeg -loglevel warning -y -i "%s" -ab "%s" "%s"' % (input_file, bitrate, output_file))
def make_mp4(video_file, audio_file, output_file, fps=30):
fps = (float(fps) * 1000) / 1001
shell_exec('MP4Box -inter 500 -new -add "%s:fps=%6f" -add "%s#audio" "%s"' % (video_file, fps, audio_file, output_file))
def mencoder_x264(input_file, sws=9, fps=30, filters=[], output_file='/dev/null', enc_opts=[]):
fps *= 1000
command_line = ('mencoder -sws %d -nosound -nosub -nosubcc -ovc x264 -of rawvideo -ofps %d/1001' % (sws, fps))
if enc_opts:
command_line += ' -x264encopts ' + ':'.join(enc_opts)
if filters:
command_line += ' -vf ' + ','.join(filters)
command_line += (' -o %s %s' % (output_file, input_file))
shell_exec(command_line)
def ffmpeg_x264(input_file, sws=9, fps=30, filters=[], output_file='/dev/null', enc_opts=[], pass_no=1, bitrate=1100):
fps *= 1000
command_line = ('ffmpeg-9999 -y -i "%s" -an -sn -vcodec libx264 -b:v %dk -bt %dk -pass %d -threads 0 -f rawvideo' % (input_file, bitrate, bitrate, pass_no))
if enc_opts:
command_line += ' -x264opts ' + ':'.join(enc_opts)
if filters:
command_line += ' -vf ' + ','.join(filters)
command_line += (' "%s"' % (output_file))
shell_exec(command_line)
def encoder_do_all(vob_filename, final_output_file, output_video_file='output.x264', filters=[], pass_1_enc_opts=[], pass_2_enc_opts=[], fps=30):
try:
encode_to_aac(vob_filename)
ffmpeg_x264(vob_filename, filters=filters, enc_opts=pass_1_enc_opts, fps=fps, pass_no=1)
ffmpeg_x264(vob_filename, output_file=output_video_file, filters=filters, enc_opts=pass_2_enc_opts, fps=fps, pass_no=2)
make_mp4(output_video_file, 'audio.aac', final_output_file, fps=fps)
except KeyboardInterrupt:
pass
clean_up()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment