mirror of
https://github.com/soimort/you-get.git
synced 2025-02-02 16:24:00 +03:00
add join support for MPEG-TS files
This commit is contained in:
parent
4f68afef91
commit
3cf9327b0c
1
.gitignore
vendored
1
.gitignore
vendored
@ -10,4 +10,5 @@ _*/
|
|||||||
*.3gp
|
*.3gp
|
||||||
*.flv
|
*.flv
|
||||||
*.mp4
|
*.mp4
|
||||||
|
*.ts
|
||||||
*.webm
|
*.webm
|
||||||
|
@ -119,6 +119,7 @@ def url_info(url, faker = False):
|
|||||||
'video/3gpp': '3gp',
|
'video/3gpp': '3gp',
|
||||||
'video/f4v': 'flv',
|
'video/f4v': 'flv',
|
||||||
'video/mp4': 'mp4',
|
'video/mp4': 'mp4',
|
||||||
|
'video/mp2t': 'ts',
|
||||||
'video/webm': 'webm',
|
'video/webm': 'webm',
|
||||||
'video/x-flv': 'flv'
|
'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):
|
def download_urls(urls, title, ext, total_size, output_dir = '.', refer = None, merge = True, faker = False):
|
||||||
assert urls
|
assert urls
|
||||||
assert ext in ('3gp', 'flv', 'mp4', 'webm')
|
assert ext in ('3gp', 'flv', 'mp4', 'ts', 'webm')
|
||||||
if not total_size:
|
if not total_size:
|
||||||
try:
|
try:
|
||||||
total_size = urls_size(urls)
|
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)
|
url_save(url, filepath, bar, refer = refer, faker = faker)
|
||||||
bar.done()
|
bar.done()
|
||||||
else:
|
else:
|
||||||
flvs = []
|
parts = []
|
||||||
print('Downloading %s.%s ...' % (tr(title), ext))
|
print('Downloading %s.%s ...' % (tr(title), ext))
|
||||||
for i, url in enumerate(urls):
|
for i, url in enumerate(urls):
|
||||||
filename = '%s[%02d].%s' % (title, i, ext)
|
filename = '%s[%02d].%s' % (title, i, ext)
|
||||||
filepath = os.path.join(output_dir, filename)
|
filepath = os.path.join(output_dir, filename)
|
||||||
flvs.append(filepath)
|
parts.append(filepath)
|
||||||
#print 'Downloading %s [%s/%s]...' % (tr(filename), i + 1, len(urls))
|
#print 'Downloading %s [%s/%s]...' % (tr(filename), i + 1, len(urls))
|
||||||
bar.update_piece(i + 1)
|
bar.update_piece(i + 1)
|
||||||
url_save(url, filepath, bar, refer = refer, is_part = True, faker = faker)
|
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()
|
print()
|
||||||
return
|
return
|
||||||
if ext == 'flv':
|
if ext == 'flv':
|
||||||
from .processor.merge_flv import concat_flvs
|
from .processor.join_flv import concat_flv
|
||||||
concat_flvs(flvs, os.path.join(output_dir, title + '.flv'))
|
concat_flv(parts, os.path.join(output_dir, title + '.flv'))
|
||||||
for flv in flvs:
|
for part in parts:
|
||||||
os.remove(flv)
|
os.remove(part)
|
||||||
elif ext == 'mp4':
|
elif ext == 'mp4':
|
||||||
from .processor.merge_mp4 import concat_mp4s
|
from .processor.join_mp4 import concat_mp4
|
||||||
concat_mp4s(flvs, os.path.join(output_dir, title + '.mp4'))
|
concat_mp4(parts, os.path.join(output_dir, title + '.mp4'))
|
||||||
for flv in flvs:
|
for part in parts:
|
||||||
os.remove(flv)
|
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:
|
else:
|
||||||
print("Can't merge %s files" % ext)
|
print("Can't merge %s files" % ext)
|
||||||
|
|
||||||
@ -355,6 +361,8 @@ def print_info(site_info, title, type, size):
|
|||||||
type = 'video/x-flv'
|
type = 'video/x-flv'
|
||||||
elif type in ['mp4']:
|
elif type in ['mp4']:
|
||||||
type = 'video/mp4'
|
type = 'video/mp4'
|
||||||
|
elif type in ['ts']:
|
||||||
|
type = 'video/mp2t'
|
||||||
elif type in ['webm']:
|
elif type in ['webm']:
|
||||||
type = 'video/webm'
|
type = 'video/webm'
|
||||||
|
|
||||||
@ -364,6 +372,8 @@ def print_info(site_info, title, type, size):
|
|||||||
type_info = "Flash video (%s)" % type
|
type_info = "Flash video (%s)" % type
|
||||||
elif type in ['video/mp4', 'video/x-m4v']:
|
elif type in ['video/mp4', 'video/x-m4v']:
|
||||||
type_info = "MPEG-4 video (%s)" % type
|
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']:
|
elif type in ['video/webm']:
|
||||||
type_info = "WebM video (%s)" % type
|
type_info = "WebM video (%s)" % type
|
||||||
#elif type in ['video/ogg']:
|
#elif type in ['video/ogg']:
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
__all__ = ['concat_flvs', 'concat_mp4s']
|
__all__ = ['concat_flv', 'concat_mp4', 'concat_ts']
|
||||||
|
|
||||||
from .merge_flv import concat_flvs
|
from .join_flv import concat_flv
|
||||||
from .merge_mp4 import concat_mp4s
|
from .join_mp4 import concat_mp4
|
||||||
|
from .join_ts import concat_ts
|
||||||
|
@ -292,7 +292,7 @@ def guess_output(inputs):
|
|||||||
return inputs[0][:i] + '.flv'
|
return inputs[0][:i] + '.flv'
|
||||||
return 'output.flv'
|
return 'output.flv'
|
||||||
|
|
||||||
def concat_flvs(flvs, output = None):
|
def concat_flv(flvs, output = None):
|
||||||
assert flvs, 'no flv file found'
|
assert flvs, 'no flv file found'
|
||||||
import os.path
|
import os.path
|
||||||
if not output:
|
if not output:
|
||||||
@ -336,7 +336,7 @@ def concat_flvs(flvs, output = None):
|
|||||||
return output
|
return output
|
||||||
|
|
||||||
def usage():
|
def usage():
|
||||||
print('Usage: [python3] merge_flv.py --output TARGET.flv flv...')
|
print('Usage: [python3] join_flv.py --output TARGET.flv flv...')
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
import sys, getopt
|
import sys, getopt
|
||||||
@ -359,7 +359,7 @@ def main():
|
|||||||
usage()
|
usage()
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
concat_flvs(args, output)
|
concat_flv(args, output)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
@ -853,7 +853,7 @@ def merge_mp4s(files, output):
|
|||||||
# main
|
# main
|
||||||
##################################################
|
##################################################
|
||||||
|
|
||||||
# TODO: FIXME: duplicate of merge_flv
|
# TODO: FIXME: duplicate of join_flv
|
||||||
|
|
||||||
def guess_output(inputs):
|
def guess_output(inputs):
|
||||||
import os.path
|
import os.path
|
||||||
@ -864,7 +864,7 @@ def guess_output(inputs):
|
|||||||
return inputs[0][:i] + '.mp4'
|
return inputs[0][:i] + '.mp4'
|
||||||
return 'output.mp4'
|
return 'output.mp4'
|
||||||
|
|
||||||
def concat_mp4s(mp4s, output = None):
|
def concat_mp4(mp4s, output = None):
|
||||||
assert mp4s, 'no mp4 file found'
|
assert mp4s, 'no mp4 file found'
|
||||||
import os.path
|
import os.path
|
||||||
if not output:
|
if not output:
|
||||||
@ -878,7 +878,7 @@ def concat_mp4s(mp4s, output = None):
|
|||||||
return output
|
return output
|
||||||
|
|
||||||
def usage():
|
def usage():
|
||||||
print('Usage: [python3] merge_mp4.py --output TARGET.mp4 mp4...')
|
print('Usage: [python3] join_mp4.py --output TARGET.mp4 mp4...')
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
import sys, getopt
|
import sys, getopt
|
||||||
@ -901,7 +901,7 @@ def main():
|
|||||||
usage()
|
usage()
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
concat_mp4s(args, output)
|
concat_mp4(args, output)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
59
you_get/processor/join_ts.py
Executable file
59
you_get/processor/join_ts.py
Executable 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()
|
Loading…
Reference in New Issue
Block a user