add join support for MPEG-TS files

This commit is contained in:
Mort Yao 2012-09-16 20:55:31 +02:00
parent 4f68afef91
commit 3cf9327b0c
6 changed files with 92 additions and 21 deletions

1
.gitignore vendored
View File

@ -10,4 +10,5 @@ _*/
*.3gp
*.flv
*.mp4
*.ts
*.webm

View File

@ -119,6 +119,7 @@ def url_info(url, faker = False):
'video/3gpp': '3gp',
'video/f4v': 'flv',
'video/mp4': 'mp4',
'video/mp2t': 'ts',
'video/webm': 'webm',
'video/x-flv': 'flv'
}
@ -288,7 +289,7 @@ class DummyProgressBar:
def download_urls(urls, title, ext, total_size, output_dir = '.', refer = None, merge = True, faker = False):
assert urls
assert ext in ('3gp', 'flv', 'mp4', 'webm')
assert ext in ('3gp', 'flv', 'mp4', 'ts', 'webm')
if not total_size:
try:
total_size = urls_size(urls)
@ -315,12 +316,12 @@ def download_urls(urls, title, ext, total_size, output_dir = '.', refer = None,
url_save(url, filepath, bar, refer = refer, faker = faker)
bar.done()
else:
flvs = []
parts = []
print('Downloading %s.%s ...' % (tr(title), ext))
for i, url in enumerate(urls):
filename = '%s[%02d].%s' % (title, i, ext)
filepath = os.path.join(output_dir, filename)
flvs.append(filepath)
parts.append(filepath)
#print 'Downloading %s [%s/%s]...' % (tr(filename), i + 1, len(urls))
bar.update_piece(i + 1)
url_save(url, filepath, bar, refer = refer, is_part = True, faker = faker)
@ -329,15 +330,20 @@ def download_urls(urls, title, ext, total_size, output_dir = '.', refer = None,
print()
return
if ext == 'flv':
from .processor.merge_flv import concat_flvs
concat_flvs(flvs, os.path.join(output_dir, title + '.flv'))
for flv in flvs:
os.remove(flv)
from .processor.join_flv import concat_flv
concat_flv(parts, os.path.join(output_dir, title + '.flv'))
for part in parts:
os.remove(part)
elif ext == 'mp4':
from .processor.merge_mp4 import concat_mp4s
concat_mp4s(flvs, os.path.join(output_dir, title + '.mp4'))
for flv in flvs:
os.remove(flv)
from .processor.join_mp4 import concat_mp4
concat_mp4(parts, os.path.join(output_dir, title + '.mp4'))
for part in parts:
os.remove(part)
elif ext == 'ts':
from .processor.join_ts import concat_ts
concat_ts(parts, os.path.join(output_dir, title + '.ts'))
for part in parts:
os.remove(part)
else:
print("Can't merge %s files" % ext)
@ -355,6 +361,8 @@ def print_info(site_info, title, type, size):
type = 'video/x-flv'
elif type in ['mp4']:
type = 'video/mp4'
elif type in ['ts']:
type = 'video/mp2t'
elif type in ['webm']:
type = 'video/webm'
@ -364,6 +372,8 @@ def print_info(site_info, title, type, size):
type_info = "Flash video (%s)" % type
elif type in ['video/mp4', 'video/x-m4v']:
type_info = "MPEG-4 video (%s)" % type
elif type in ['video/mp2t']:
type_info = "MPEG-2 transport stream (%s)" % type
elif type in ['video/webm']:
type_info = "WebM video (%s)" % type
#elif type in ['video/ogg']:

View File

@ -1,6 +1,7 @@
#!/usr/bin/env python
__all__ = ['concat_flvs', 'concat_mp4s']
__all__ = ['concat_flv', 'concat_mp4', 'concat_ts']
from .merge_flv import concat_flvs
from .merge_mp4 import concat_mp4s
from .join_flv import concat_flv
from .join_mp4 import concat_mp4
from .join_ts import concat_ts

View File

@ -292,7 +292,7 @@ def guess_output(inputs):
return inputs[0][:i] + '.flv'
return 'output.flv'
def concat_flvs(flvs, output = None):
def concat_flv(flvs, output = None):
assert flvs, 'no flv file found'
import os.path
if not output:
@ -336,7 +336,7 @@ def concat_flvs(flvs, output = None):
return output
def usage():
print('Usage: [python3] merge_flv.py --output TARGET.flv flv...')
print('Usage: [python3] join_flv.py --output TARGET.flv flv...')
def main():
import sys, getopt
@ -359,7 +359,7 @@ def main():
usage()
sys.exit(1)
concat_flvs(args, output)
concat_flv(args, output)
if __name__ == '__main__':
main()

View File

@ -853,7 +853,7 @@ def merge_mp4s(files, output):
# main
##################################################
# TODO: FIXME: duplicate of merge_flv
# TODO: FIXME: duplicate of join_flv
def guess_output(inputs):
import os.path
@ -864,7 +864,7 @@ def guess_output(inputs):
return inputs[0][:i] + '.mp4'
return 'output.mp4'
def concat_mp4s(mp4s, output = None):
def concat_mp4(mp4s, output = None):
assert mp4s, 'no mp4 file found'
import os.path
if not output:
@ -878,7 +878,7 @@ def concat_mp4s(mp4s, output = None):
return output
def usage():
print('Usage: [python3] merge_mp4.py --output TARGET.mp4 mp4...')
print('Usage: [python3] join_mp4.py --output TARGET.mp4 mp4...')
def main():
import sys, getopt
@ -901,7 +901,7 @@ def main():
usage()
sys.exit(1)
concat_mp4s(args, output)
concat_mp4(args, output)
if __name__ == '__main__':
main()

59
you_get/processor/join_ts.py Executable file
View File

@ -0,0 +1,59 @@
#!/usr/bin/env python
##################################################
# main
##################################################
def guess_output(inputs):
import os.path
inputs = map(os.path.basename, inputs)
n = min(map(len, inputs))
for i in reversed(range(1, n)):
if len(set(s[:i] for s in inputs)) == 1:
return inputs[0][:i] + '.ts'
return 'output.ts'
def concat_ts(tss, output = None):
assert tss, 'no ts file found'
import os.path
if not output:
output = guess_output(tss)
elif os.path.isdir(output):
output = os.path.join(output, guess_output(tss))
print('Merging video parts...')
ins = [open(ts, 'rb') for ts in tss]
with open(output, 'wb') as output:
for i in ins:
output.write(i.read())
return output
def usage():
print('Usage: [python3] join_ts.py --output TARGET.ts ts...')
def main():
import sys, getopt
try:
opts, args = getopt.getopt(sys.argv[1:], "ho:", ["help", "output="])
except getopt.GetoptError as err:
usage()
sys.exit(1)
output = None
for o, a in opts:
if o in ("-h", "--help"):
usage()
sys.exit()
elif o in ("-o", "--output"):
output = a
else:
usage()
sys.exit(1)
if not args:
usage()
sys.exit(1)
concat_ts(args, output)
if __name__ == '__main__':
main()