-
Notifications
You must be signed in to change notification settings - Fork 0
/
transcoder.py
executable file
·134 lines (108 loc) · 3.25 KB
/
transcoder.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
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
#!/usr/bin/env python3
# -*- encoding:utf-8 -*-
__author__ = 'Adrian Wang'
import ffmpy
import os
import sys
# magic = 0x79706762
head_len = 10
par_head_len = 12
flv_header = b'\x46\x4C\x56\x01\x05\x00\x00\x00\x09\x00\x00\x00\x00'
flv_ext = '.flv'
def trans(filename):
if not os.path.exists(filename):
print('Invalid file ' + filename + ': not exists!')
return 1
size = os.path.getsize(filename)
if size <= 10:
print('Invalid file ' + filename + ': too small!')
return 1
f = open(filename, 'rb')
if not validate_head(f):
print('Invalid file ' + filename + ': unrecognized format!')
return 1
print('Start to transcode ' + filename + ', file size: ' + str(size))
simple_name = handle_name(filename)
num = do_trans(f, size, simple_name)
return do_merge(simple_name, num)
def handle_name(filename):
names = filename.split('.')
len_names = len(names)
position = 0 if len_names < 2 else len_names - 2
simple_name = names[position]
assert len(simple_name) > 0, 'empty name'
return simple_name
def do_merge(name, num):
if num == 1:
final_name = name + flv_ext
os.rename(temp_file_name(name, 1), final_name)
print('Output: ' + final_name)
return 0
else:
print('Need further merge ' + num + ' files currently!')
# TODO
pass
return 0
def seg_name(idx):
return '{0:03d}'.format(idx)
def temp_file_name(outfile, idx):
return outfile + seg_name(idx) + flv_ext
def do_trans(f, size, out_file):
pos = seek_next(f, head_len, size)
seg_cnt = 1
out = open(temp_file_name(out_file, seg_cnt), 'wb')
out.write(flv_header)
while pos != 0:
seg_len = seg_size(f, pos)
f.seek(pos)
# print('Segment ' + seg_name() + ' length ' + str(seg_len))
out.write(f.read(seg_len))
# print(str(seg_len) + ' bytes written.')
mark = f.read(1)
pos += seg_len
if mark == b'':
break
elif mark == '0':
out.close()
# for next temp file
seg_cnt += 1
print("writing another temp file")
out = open(temp_file_name(out_file, seg_cnt), 'wb')
out.write(flv_header)
pos = seek_next(f, pos, size)
out.close()
f.close()
return seg_cnt
def seg_size(f, offset):
f.seek(offset)
data = f.read(4)
ssize = (data[1] << 16) | (data[2] << 8) | (data[3])
return ssize + 15
def seek_next(f, offset, size):
while offset + par_head_len < size:
f.seek(offset)
par_head = f.read(par_head_len)
if not par_head[0] == 9:
offset += 1
elif par_head[1:4] == 0:
offset += 4
elif not par_head[4:11] == b'\x00\x00\x00\x00\x00\x00\x00':
offset += 1
elif par_head[11] == 0:
offset += 1
else:
print('... found segment header at: ' + str(offset))
return offset
return 0
def validate_head(f):
# print(f.read(head_len))
f.seek(0)
return f.read(head_len) == b'QIYI VIDEO'
def main():
for arg in sys.argv:
if arg == sys.argv[0]:
pass
else:
trans(arg)
if __name__ == '__main__':
main()