supporting both ffmpeg 1.x and libav, fix #159

This commit is contained in:
Mort Yao 2013-04-17 14:38:13 +02:00
parent bbd50c4e6c
commit 2e033fb75d

View File

@ -3,21 +3,25 @@
import os.path import os.path
import subprocess import subprocess
def has_ffmpeg_installed(): def get_usable_ffmpeg(cmd):
try: try:
p = subprocess.Popen(['ffmpeg', '-version'], stdout=subprocess.PIPE) p = subprocess.Popen([cmd, '-version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate() out, err = p.communicate()
import re vers = str(out, 'utf-8').split('\n')[0].split(' ')
assert re.search('Libav', str(out, 'utf-8').split('\n')[0]) is None assert (vers[0] == 'ffmpeg' and vers[2][0] > '0') or (vers[0] == 'avconv')
return cmd
except: except:
return False return None
else:
return True FFMPEG = get_usable_ffmpeg('ffmpeg') or get_usable_ffmpeg('avconv')
def has_ffmpeg_installed():
return FFMPEG is not None
def ffmpeg_convert_ts_to_mkv(files, output = 'output.mkv'): def ffmpeg_convert_ts_to_mkv(files, output = 'output.mkv'):
for file in files: for file in files:
if os.path.isfile(file): if os.path.isfile(file):
params = ['ffmpeg', '-i'] params = [FFMPEG, '-i']
params.append(file) params.append(file)
params.append(output) params.append(output)
subprocess.call(params) subprocess.call(params)
@ -27,7 +31,7 @@ def ffmpeg_convert_ts_to_mkv(files, output = 'output.mkv'):
def ffmpeg_concat_mp4_to_mpg(files, output = 'output.mpg'): def ffmpeg_concat_mp4_to_mpg(files, output = 'output.mpg'):
for file in files: for file in files:
if os.path.isfile(file): if os.path.isfile(file):
params = ['ffmpeg', '-i'] params = [FFMPEG, '-i']
params.append(file) params.append(file)
params.append(file + '.mpg') params.append(file + '.mpg')
subprocess.call(params) subprocess.call(params)
@ -37,7 +41,7 @@ def ffmpeg_concat_mp4_to_mpg(files, output = 'output.mpg'):
for input in inputs: for input in inputs:
o.write(input.read()) o.write(input.read())
params = ['ffmpeg', '-i'] params = [FFMPEG, '-i']
params.append(output + '.mpg') params.append(output + '.mpg')
params += ['-vcodec', 'copy', '-acodec', 'copy'] params += ['-vcodec', 'copy', '-acodec', 'copy']
params.append(output) params.append(output)
@ -50,7 +54,7 @@ def ffmpeg_concat_mp4_to_mpg(files, output = 'output.mpg'):
return return
def ffmpeg_concat_ts_to_mkv(files, output = 'output.mkv'): def ffmpeg_concat_ts_to_mkv(files, output = 'output.mkv'):
params = ['ffmpeg', '-isync', '-i'] params = [FFMPEG, '-isync', '-i']
params.append('concat:') params.append('concat:')
for file in files: for file in files:
if os.path.isfile(file): if os.path.isfile(file):
@ -68,19 +72,22 @@ def ffmpeg_concat_ts_to_mkv(files, output = 'output.mkv'):
def ffmpeg_concat_flv_to_mp4(files, output = 'output.mp4'): def ffmpeg_concat_flv_to_mp4(files, output = 'output.mp4'):
for file in files: for file in files:
if os.path.isfile(file): if os.path.isfile(file):
params = ['ffmpeg', '-i'] params = [FFMPEG, '-i']
params.append(file) params.append(file)
params += ['-map', '0', '-c', 'copy', '-f', 'mpegts', '-bsf:v', 'h264_mp4toannexb'] params += ['-map', '0', '-c', 'copy', '-f', 'mpegts', '-bsf:v', 'h264_mp4toannexb']
params.append(file + '.ts') params.append(file + '.ts')
subprocess.call(params) subprocess.call(params)
params = ['ffmpeg', '-i'] params = [FFMPEG, '-i']
params.append('concat:') params.append('concat:')
for file in files: for file in files:
f = file + '.ts' f = file + '.ts'
if os.path.isfile(f): if os.path.isfile(f):
params[-1] += f + '|' params[-1] += f + '|'
if FFMPEG == 'avconv':
params += ['-c', 'copy', output]
else:
params += ['-c', 'copy', '-absf', 'aac_adtstoasc', output] params += ['-c', 'copy', '-absf', 'aac_adtstoasc', output]
if subprocess.call(params) == 0: if subprocess.call(params) == 0: