This repository has been archived by the owner on Sep 20, 2024. It is now read-only.
forked from vi/mkvparse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkvgen.py
executable file
·188 lines (157 loc) · 6.96 KB
/
mkvgen.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env python
import sys
import random
import math
# Simple hacky Matroska generator
# Reads mp3 file "q.mp3" and jpeg images from img/0.jpg, img/1.jpg and so on and
# writes Matroska file with mjpeg and mp3 to stdout
# License=MIT
# unsigned
def big_endian_number(number):
if(number<0x100):
return chr(number)
return big_endian_number(number>>8) + chr(number&0xFF)
ben=big_endian_number
def ebml_encode_number(number):
def trailing_bits(rest_of_number, number_of_bits):
# like big_endian_number, but can do padding zeroes
if number_of_bits==8:
return chr(rest_of_number&0xFF);
else:
return trailing_bits(rest_of_number>>8, number_of_bits-8) + chr(rest_of_number&0xFF)
if number == -1:
return chr(0xFF)
if number < 2**7 - 1:
return chr(number|0x80)
if number < 2**14 - 1:
return chr(0x40 | (number>>8)) + trailing_bits(number, 8)
if number < 2**21 - 1:
return chr(0x20 | (number>>16)) + trailing_bits(number, 16)
if number < 2**28 - 1:
return chr(0x10 | (number>>24)) + trailing_bits(number, 24)
if number < 2**35 - 1:
return chr(0x08 | (number>>32)) + trailing_bits(number, 32)
if number < 2**42 - 1:
return chr(0x04 | (number>>40)) + trailing_bits(number, 40)
if number < 2**49 - 1:
return chr(0x02 | (number>>48)) + trailing_bits(number, 48)
if number < 2**56 - 1:
return chr(0x01) + trailing_bits(number, 56)
raise Exception("NUMBER TOO BIG")
def ebml_element(element_id, data, length=None):
if length==None:
length = len(data)
return big_endian_number(element_id) + ebml_encode_number(length) + data
def write_ebml_header(f, content_type, version, read_version):
f.write(
ebml_element(0x1A45DFA3, "" # EBML
+ ebml_element(0x4286, ben(1)) # EBMLVersion
+ ebml_element(0x42F7, ben(1)) # EBMLReadVersion
+ ebml_element(0x42F2, ben(4)) # EBMLMaxIDLength
+ ebml_element(0x42F3, ben(8)) # EBMLMaxSizeLength
+ ebml_element(0x4282, content_type) # DocType
+ ebml_element(0x4287, ben(version)) # DocTypeVersion
+ ebml_element(0x4285, ben(read_version)) # DocTypeReadVersion
))
def write_infinite_segment_header(f):
# write segment element header
f.write(ebml_element(0x18538067,"",-1)) # Segment (unknown length)
def random_uid():
def rint():
return int(random.random()*(0x100**4))
return ben(rint()) + ben(rint()) + ben(rint()) + ben(rint())
def example():
write_ebml_header(sys.stdout, "matroska", 2, 2)
write_infinite_segment_header(sys.stdout)
# write segment info (optional)
sys.stdout.write(ebml_element(0x1549A966, "" # SegmentInfo
+ ebml_element(0x73A4, random_uid()) # SegmentUID
+ ebml_element(0x7BA9, "mkvgen.py test") # Title
+ ebml_element(0x4D80, "mkvgen.py") # MuxingApp
+ ebml_element(0x5741, "mkvgen.py") # WritingApp
))
# write trans data (codecs etc.)
sys.stdout.write(ebml_element(0x1654AE6B, "" # Tracks
+ ebml_element(0xAE, "" # TrackEntry
+ ebml_element(0xD7, ben(1)) # TrackNumber
+ ebml_element(0x73C5, ben(0x77)) # TrackUID
+ ebml_element(0x83, ben(0x01)) # TrackType
#0x01 track is a video track
#0x02 track is an audio track
#0x03 track is a complex track, i.e. a combined video and audio track
#0x10 track is a logo track
#0x11 track is a subtitle track
#0x12 track is a button track
#0x20 track is a control track
+ ebml_element(0x536E, "mjpeg data") # Name
+ ebml_element(0x86, "V_MJPEG") # CodecID
#+ ebml_element(0x23E383, ben(100000000)) # DefaultDuration (opt.), nanoseconds
#+ ebml_element(0x6DE7, ben(100)) # MinCache
+ ebml_element(0xE0, "" # Video
+ ebml_element(0xB0, ben(640)) # PixelWidth
+ ebml_element(0xBA, ben(480)) # PixelHeight
)
)
+ ebml_element(0xAE, "" # TrackEntry
+ ebml_element(0xD7, ben(2)) # TrackNumber
+ ebml_element(0x73C5, ben(0x78)) # TrackUID
+ ebml_element(0x83, ben(0x02)) # TrackType
#0x01 track is a video track
#0x02 track is an audio track
#0x03 track is a complex track, i.e. a combined video and audio track
#0x10 track is a logo track
#0x11 track is a subtitle track
#0x12 track is a button track
#0x20 track is a control track
+ ebml_element(0x536E, "content of mp3 file") # Name
#+ ebml_element(0x6DE7, ben(100)) # MinCache
+ ebml_element(0x86, "A_MPEG/L3") # CodecID
#+ ebml_element(0xE1, "") # Audio
)
))
mp3file = open("q.mp3", "rb")
mp3file.read(500000);
def mp3framesgenerator(f):
debt=""
while True:
for i in xrange(0,len(debt)+1):
if i >= len(debt)-1:
debt = debt + f.read(8192)
break
#sys.stderr.write("i="+str(i)+" len="+str(len(debt))+"\n")
if ord(debt[i])==0xFF and (ord(debt[i+1]) & 0xF0)==0XF0 and i>700:
if i>0:
yield debt[0:i]
# sys.stderr.write("len="+str(i)+"\n")
debt = debt[i:]
break
mp3 = mp3framesgenerator(mp3file)
mp3.next()
for i in xrange(0,530):
framefile = open("img/"+str(i)+".jpg", "rb")
framedata = framefile.read()
framefile.close()
# write cluster (actual video data)
if random.random()<1:
sys.stdout.write(ebml_element(0x1F43B675, "" # Cluster
+ ebml_element(0xE7, ben(int(i*26*4))) # TimeCode, uint, milliseconds
# + ebml_element(0xA7, ben(0)) # Position, uint
+ ebml_element(0xA3, "" # SimpleBlock
+ ebml_encode_number(1) # track number
+ chr(0x00) + chr(0x00) # timecode, relative to Cluster timecode, sint16, in milliseconds
+ chr(0x00) # flags
+ framedata
)))
for u in xrange(0,4):
mp3f=mp3.next()
if random.random()<1:
sys.stdout.write(ebml_element(0x1F43B675, "" # Cluster
+ ebml_element(0xE7, ben(i*26*4+u*26)) # TimeCode, uint, milliseconds
+ ebml_element(0xA3, "" # SimpleBlock
+ ebml_encode_number(2) # track number
+ chr(0x00) + chr(0x00) # timecode, relative to Cluster timecode, sint16, in milliseconds
+ chr(0x00) # flags
+ mp3f
)))
if __name__ == '__main__':
example()