-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
91 lines (79 loc) · 2.57 KB
/
main.py
File metadata and controls
91 lines (79 loc) · 2.57 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
#!/usr/local/bin/python3
import sys
import os.path
HighProfile = bytes.fromhex('64001f') # High Profile 3.1
MainProfile = bytes.fromhex('4d401f') # Main Profile 3.1
MagicCode = 'avcC'.encode('ascii')
def avcLevelEditor(filePath):
try:
currentProfile = None
with open(filePath, 'r+b') as f:
t = f.read(8192)
index = t.index(MagicCode)
assert(index)
currentProfile = t[index+5 : index + 8]
if currentProfile == MainProfile or currentProfile == HighProfile:
print('skip ' + filePath)
return # It is already supported AVC Level
if currentProfile[0] == HighProfile[0]:
toProfile = HighProfile
print("change to High Profile")
if currentProfile[0] == MainProfile[0]:
toProfile = MainProfile
print("change to Main Profile")
print(list(map(hex, currentProfile)))
t = t.replace(currentProfile, toProfile)
f.seek(0)
f.write(t)
# write log file
with open(filePath+'.log', 'w') as f:
f.write(currentProfile.decode('utf8'))
f.write('\nindex: ' + str(index))
t = list(map(hex, currentProfile))
f.write('\n' + ' '.join(t))
print('finished')
except ValueError as e:
print('Sorry, I don\'t support this file\n')
def avcLevelRestore(filePath):
try:
with open(filePath + '.log', 'rb') as f:
level = f.read(3)
with open(filePath, 'r+b') as f:
t = f.read(8192)
index = t.index(MagicCode)
assert(index)
currentProfile = t[index+5 : index + 8]
t = t.replace(currentProfile, level)
f.seek(0)
f.write(t)
print("AVC Level restore to {}".format([hex(e) for e in level]))
except IOError as e:
print('Opps… This file has not a restore log')
if __name__ == '__main__':
if sys.argv[1] == '-r': # restore mode
avcLevelRestore(os.path.abspath(sys.argv[2]))
else:
for e in sys.argv[1:]:
avcLevelEditor(os.path.abspath(e))
#
# if sys.argv[1] == '-b':
# # Batch mode
# for e in sys.argv[2:]:
# avcLevelEditor(os.path.abspath(e))
# else:
# #print('Single Mode')
# avcLevelEditor(os.path.abspath(sys.argv[1]))
def seekSymbol(handle, symbol):
f = lambda h, x: h.read(1) == x.encode('utf8')
if f(handle, 'a'):
if f(handle, 'v'):
if f(handle, 'c'):
if f(handle, 'C'):
return
def h264LevelModify(handle, position):
handle.seek(position + 1) # skip 'avcC' followed one byte
currentLevel = handle.read(3) # read 3 byte profile
handle.seek(position + 1)
handle.write(HighProfile)
# find another >currentLevel<
# replace it to >HighProfile<