-
Notifications
You must be signed in to change notification settings - Fork 0
/
h264EncodeInfoEditor.py
executable file
·130 lines (109 loc) · 3.37 KB
/
h264EncodeInfoEditor.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
#!/usr/bin/env python3
import sys
from argparse import ArgumentParser
uuid = b'\xDC\x45\xE9\xBD\xE6\xD9\x48\xB7\x96\x2C\xD8\x20\xD9\x23\xEE\xEF'
sei_user_data_header = b'\x00\x00\x00\x01\x06\x05'
def enc_sei_user_data(_uuid: bytes, _data: str) -> bytes:
data_byte = _data.encode()
data_length = len(data_byte) + len(_uuid) + 1
sei_length = b''
for _ in range(data_length // 255):
sei_length += b'\xff'
sei_length += int.to_bytes(data_length % 255, byteorder="little", length=1)
return sei_user_data_header + sei_length + _uuid + data_byte + b'\x00\x80'
def pipe_v3(_input, _output, _str):
_flag = 0
'''
0
1 => 00
2 => 00 00
3 => 00 00 00
6 => 00 00 01
7 => 00 00 00 01
'''
buf = b''
_output.write(enc_sei_user_data(uuid, _str))
while 1:
b = _input.read(1)
if len(b) == 0:
# EOF
_output.write(buf)
break
buf += b
if b == b'\x00':
if _flag in [0, 1, 2]:
_flag += 1
else:
_flag = 0
_output.write(buf)
buf = b''
continue
elif b == b'\x01':
if _flag == 2:
_flag = 6
elif _flag == 3:
_flag = 7
else:
_flag = 0
_output.write(buf)
buf = b''
continue
else:
_flag = 0
_output.write(buf)
buf = b''
continue
if _flag in [6, 7]:
# find NALU
b = _input.read(2)
buf += b
if len(b) < 2:
# EOF
_output.write(buf)
break
if b != b'\x06\x05':
_flag = 0
_output.write(buf)
continue
length = 0
while 1:
b = _input.read(1)
if b != b'\xff':
length += int.from_bytes(b, byteorder="little")
break
else:
length += 255
print("[Original Writing library]: {}".format(_input.read(length + 1)[16:-2].decode()), file=sys.stderr)
_flag = 0
buf = b''
break
while 1:
b = _input.readlines()
if len(b) == 0:
break
_output.writelines(b)
_input.close()
_output.close()
def _open_input(s):
if s == '-':
return sys.stdin.buffer
else:
return open(s, 'rb')
def _open_output(s):
if s == '-':
return sys.stdout.buffer
else:
return open(s, 'wb')
def parse_args():
parser = ArgumentParser(prog='h264EncodeInfoEditor', description='A script to edit H.264 encoder information')
parser.add_argument('-i', '--input', required=True, type=_open_input,
help='input h264 bit stream, \' - \' will read stream from stdin')
parser.add_argument('-o', '--output', required=True, type=_open_output,
help='output h264 bit stream, \' - \' will write stream from stdout')
parser.add_argument('-s', '--string', required=True, type=str, help='info what you want write')
return parser.parse_args()
def main():
args = parse_args()
pipe_v3(args.input, args.output, args.string)
if __name__ == '__main__':
main()