-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathm3u8.py
More file actions
60 lines (53 loc) · 1.71 KB
/
Copy pathm3u8.py
File metadata and controls
60 lines (53 loc) · 1.71 KB
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
# -*- coding:utf-8 -*-
import os
import sys
'''
rename all the files in the folder under windows
`>ren * *.ts`
'''
def get_index(user_path):
'''list all the file of m3u8 file and sort the file with 0 1 2..
make sure all the files in the path are video file
'''
dirlist = os.listdir(user_path)
numNames = []
extStr = ''
if len(dirlist) > 0 and dirlist[0].endswith('.ts'):
extStr = '.ts'
for one in dirlist:
fileName,_ = os.path.splitext(os.path.basename(one))
numNames.append(fileName)
else:
numNames = dirlist
index = [ int(num) for num in numNames]
index = sorted(index)
sortedNames = [ str(idx)+extStr for idx in index]
return sortedNames
def convert_m3u8(index, output):
'''use the copy /b file1+file2+...+fileN outputfile to merge the video file'''
tmp = [item for item in index]
cmd_str = '+'.join(tmp)
exec_str = "copy /b " + cmd_str + " " + output
print(exec_str)
os.system(exec_str)
def work(user_path, output):
index = get_index(user_path)
os.chdir(user_path)
# the cmd cant be too long, so merge part of the file and then merge the parts
total = len(index)
max_file_num = 1000
part = int(total/max_file_num) + 1
tmpOutput = []
for x in range(0, part):
tmp = str('part'+str(x))
tmpOutput.append(tmp)
convert_m3u8(index[x*max_file_num : (x+1)*max_file_num], tmp)
print(tmpOutput)
convert_m3u8(tmpOutput, output)
# delete the tmp part files
[ os.remove(tmpfile) for tmpfile in tmpOutput]
if __name__ == '__main__':
argn = len(sys.argv)
if argn == 3:
path, outputfile = sys.argv[1:]
work(path, outputfile)