forked from Smyumwmke/ihatei
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoin_ts.py
65 lines (55 loc) · 1.59 KB
/
join_ts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
import struct
from io import BytesIO
##################################################
# 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(ts_parts, output = None):
assert ts_parts, 'no ts files found'
import os.path
if not output:
output = guess_output(ts_parts)
elif os.path.isdir(output):
output = os.path.join(output, guess_output(ts_parts))
print('Merging video parts...')
ts_out_file = open(output, "wb")
for ts_in in ts_parts:
ts_in_file = open(ts_in, "rb")
ts_in_data = ts_in_file.read()
ts_in_file.close()
ts_out_file.write(ts_in_data)
ts_out_file.close()
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()