mirror of
https://github.com/soimort/you-get.git
synced 2025-02-03 00:33:58 +03:00
use concat demuxer on FFmpeg >= 1.1, fix #324
This commit is contained in:
parent
1c3c586e4d
commit
f8f9b7778b
@ -9,58 +9,78 @@ def get_usable_ffmpeg(cmd):
|
|||||||
out, err = p.communicate()
|
out, err = p.communicate()
|
||||||
vers = str(out, 'utf-8').split('\n')[0].split(' ')
|
vers = str(out, 'utf-8').split('\n')[0].split(' ')
|
||||||
assert (vers[0] == 'ffmpeg' and vers[2][0] > '0') or (vers[0] == 'avconv')
|
assert (vers[0] == 'ffmpeg' and vers[2][0] > '0') or (vers[0] == 'avconv')
|
||||||
return cmd
|
return cmd, [int(i) for i in vers[2].split('.')]
|
||||||
except:
|
except:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
FFMPEG = get_usable_ffmpeg('ffmpeg') or get_usable_ffmpeg('avconv')
|
FFMPEG, FFMPEG_VERSION = get_usable_ffmpeg('ffmpeg') or get_usable_ffmpeg('avconv')
|
||||||
|
|
||||||
def has_ffmpeg_installed():
|
def has_ffmpeg_installed():
|
||||||
return FFMPEG is not None
|
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)
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
def ffmpeg_concat_mp4_to_mpg(files, output = 'output.mpg'):
|
def ffmpeg_concat_mp4_to_mpg(files, output='output.mpg'):
|
||||||
|
# Use concat demuxer on FFmpeg >= 1.1
|
||||||
|
if FFMPEG == 'ffmpeg' and (FFMPEG_VERSION[0] >= 2 or (FFMPEG_VERSION[0] == 1 and FFMPEG_VERSION[1] >= 1)):
|
||||||
|
concat_list = open(output + '.txt', 'w')
|
||||||
|
for file in files:
|
||||||
|
if os.path.isfile(file):
|
||||||
|
concat_list.write("file '%s'\n" % file)
|
||||||
|
concat_list.close()
|
||||||
|
|
||||||
|
params = [FFMPEG, '-f', 'concat', '-i']
|
||||||
|
params.append(output + '.txt')
|
||||||
|
params += ['-c', 'copy', output]
|
||||||
|
|
||||||
|
if subprocess.call(params) == 0:
|
||||||
|
os.remove(output + '.txt')
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
raise
|
||||||
|
|
||||||
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)
|
||||||
|
|
||||||
inputs = [open(file + '.mpg', 'rb') for file in files]
|
inputs = [open(file + '.mpg', 'rb') for file in files]
|
||||||
with open(output + '.mpg', 'wb') as o:
|
with open(output + '.mpg', 'wb') as o:
|
||||||
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)
|
||||||
subprocess.call(params)
|
subprocess.call(params)
|
||||||
|
|
||||||
for file in files:
|
|
||||||
os.remove(file + '.mpg')
|
|
||||||
os.remove(output + '.mpg')
|
|
||||||
|
|
||||||
return
|
|
||||||
|
|
||||||
def ffmpeg_concat_ts_to_mkv(files, output = 'output.mkv'):
|
if subprocess.call(params) == 0:
|
||||||
|
for file in files:
|
||||||
|
os.remove(file + '.mpg')
|
||||||
|
os.remove(output + '.mpg')
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
raise
|
||||||
|
|
||||||
|
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):
|
||||||
params[-1] += file + '|'
|
params[-1] += file + '|'
|
||||||
params += ['-f', 'matroska', '-c', 'copy', output]
|
params += ['-f', 'matroska', '-c', 'copy', output]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if subprocess.call(params) == 0:
|
if subprocess.call(params) == 0:
|
||||||
return True
|
return True
|
||||||
@ -69,16 +89,34 @@ def ffmpeg_concat_ts_to_mkv(files, output = 'output.mkv'):
|
|||||||
except:
|
except:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def ffmpeg_concat_flv_to_mp4(files, output = 'output.mp4'):
|
def ffmpeg_concat_flv_to_mp4(files, output='output.mp4'):
|
||||||
|
# Use concat demuxer on FFmpeg >= 1.1
|
||||||
|
if FFMPEG == 'ffmpeg' and (FFMPEG_VERSION[0] >= 2 or (FFMPEG_VERSION[0] == 1 and FFMPEG_VERSION[1] >= 1)):
|
||||||
|
concat_list = open(output + '.txt', 'w')
|
||||||
|
for file in files:
|
||||||
|
if os.path.isfile(file):
|
||||||
|
concat_list.write("file '%s'\n" % file)
|
||||||
|
concat_list.close()
|
||||||
|
|
||||||
|
params = [FFMPEG, '-f', 'concat', '-i']
|
||||||
|
params.append(output + '.txt')
|
||||||
|
params += ['-c', 'copy', output]
|
||||||
|
|
||||||
|
if subprocess.call(params) == 0:
|
||||||
|
os.remove(output + '.txt')
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
raise
|
||||||
|
|
||||||
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:
|
||||||
@ -89,7 +127,7 @@ def ffmpeg_concat_flv_to_mp4(files, output = 'output.mp4'):
|
|||||||
params += ['-c', 'copy', output]
|
params += ['-c', 'copy', output]
|
||||||
else:
|
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:
|
||||||
for file in files:
|
for file in files:
|
||||||
os.remove(file + '.ts')
|
os.remove(file + '.ts')
|
||||||
@ -97,16 +135,34 @@ def ffmpeg_concat_flv_to_mp4(files, output = 'output.mp4'):
|
|||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
def ffmpeg_concat_mp4_to_mp4(files, output = 'output.mp4'):
|
def ffmpeg_concat_mp4_to_mp4(files, output='output.mp4'):
|
||||||
|
# Use concat demuxer on FFmpeg >= 1.1
|
||||||
|
if FFMPEG == 'ffmpeg' and (FFMPEG_VERSION[0] >= 2 or (FFMPEG_VERSION[0] == 1 and FFMPEG_VERSION[1] >= 1)):
|
||||||
|
concat_list = open(output + '.txt', 'w')
|
||||||
|
for file in files:
|
||||||
|
if os.path.isfile(file):
|
||||||
|
concat_list.write("file '%s'\n" % file)
|
||||||
|
concat_list.close()
|
||||||
|
|
||||||
|
params = [FFMPEG, '-f', 'concat', '-i']
|
||||||
|
params.append(output + '.txt')
|
||||||
|
params += ['-c', 'copy', output]
|
||||||
|
|
||||||
|
if subprocess.call(params) == 0:
|
||||||
|
os.remove(output + '.txt')
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
raise
|
||||||
|
|
||||||
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 += ['-c', 'copy', '-f', 'mpegts', '-bsf:v', 'h264_mp4toannexb']
|
params += ['-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:
|
||||||
@ -117,7 +173,7 @@ def ffmpeg_concat_mp4_to_mp4(files, output = 'output.mp4'):
|
|||||||
params += ['-c', 'copy', output]
|
params += ['-c', 'copy', output]
|
||||||
else:
|
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:
|
||||||
for file in files:
|
for file in files:
|
||||||
os.remove(file + '.ts')
|
os.remove(file + '.ts')
|
||||||
|
Loading…
Reference in New Issue
Block a user