-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathts2mp4.py
More file actions
142 lines (114 loc) · 2.87 KB
/
Copy pathts2mp4.py
File metadata and controls
142 lines (114 loc) · 2.87 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#coding: utf-8
import os
import subprocess;
# split files based on m3u8 index file
def split_file(dir, m3u8):
file=open(m3u8, encoding='UTF-8')
seg_map=dict()
try:
while True:
line=file.readline()
if line:
line=line.strip('\n')
if line.startswith("#EXT-X-BYTERANGE"):
s=line.split(":")[1].strip().split("@")
seg=[]
seg.append(int(s[0]))
seg.append(int(s[1]))
line=file.readline().strip()
if line.find("/") >= 0:
line=line[line.rindex("/")+1:]
if line in seg_map:
segs=seg_map[line]
else:
segs=[]
seg_map[line]=segs
segs.append(seg)
else:
print("file EOF:", m3u8)
break
finally:
file.close()
for key, value in seg_map.items():
# print(key, value)
f=open(dir+"/"+key, 'rb')
try:
for i in range(len(value)):
seg=value[i]
f.seek(seg[1])
dst=open(dir+"/"+key+"_"+str(i)+".ts", 'wb')
read_len=0
next_len=1024
try:
while next_len > 0:
chunk = f.read(next_len)
if chunk:
read_len+=next_len
dst.write(chunk)
if read_len+next_len>=seg[0]:
next_len=seg[0]-read_len
else:
break
finally:
dst.close()
finally:
f.close()
return 0
def c(s):
s=s.split('.')[0]
a=s.split('_')
score=100*float(a[1])+float(a[2])
return score
# root dir
root='D:/大长今国韩双语版.2005/.%s'
# dirs=[root]
# dirs=['']
dirs=[]
start=9
end=40
r=range(start, end)
for i in r:
j=i
if i < 10:
j='0%s' % (i)
dirs.append(root % (j))
for dir in dirs:
os.chdir(dir)
print(os.getcwd())
files=os.listdir(dir)
m3u8=''
for x in files:
if x.endswith('m3u8'):
m3u8=x
break
if m3u8=='':
print('no m3u8 file found in dir:', dir)
continue
m3u8_file=dir+'/'+m3u8
# java_exec='C:/Java/jdk1.8.0_112/bin/java'
# split_result=subprocess.call([java_exec, '-cp', 'd:/', 'FileSegmentation', m3u8_file])
split_result = split_file(dir, m3u8_file)
if split_result!=0:
print('failed to segment ts files in dir:', dir)
continue
files=os.listdir(dir)
tss=[]
for x in files:
if x.endswith('.ts'):
tss.append(x)
tss.sort(key=c)
dst_file=open('list.txt', 'w')
for x in tss:
dst_file.write('file %s%s' % (dir+'/'+x,'\r\n'))
dst_file.close()
# cmd='D:/Programs/ffmpeg/ffmpeg -f concat %s -c copy -absf aac_adtstoasc %s.mp4' % ('-i list.txt', dir[dir.rindex('/')+1:])
# print(cmd)
cmd='D:/Programs/ffmpeg/ffmpeg'
mp4_file=dir[dir.rindex('/')+1:]+'.mp4'
if mp4_file.index('.')==0:
mp4_file=mp4_file[1:]
mp4_file='../'+mp4_file
# exec_result=os.execv(cmd,['ffmpeg','-f concat','-i list.txt','-c copy','-absf','aac_adtstoasc', mp4_file])
exec_result=subprocess.call([cmd, '-f', 'concat', '-safe', '0', '-i', 'list.txt', '-c', 'copy', mp4_file])
# exec_result=os.system('%s -f concat -i list.txt -c copy -absf aac_adtstoasc %s' % (cmd, mp4_file))
print(dir, 'exec_result', exec_result)