forked from vi/mkvparse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml2mkv
executable file
·636 lines (585 loc) · 26.2 KB
/
xml2mkv
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
#!/usr/bin/env python
# Read mkv2xml's output and convert it back to matroska file (not verbatim). License=MIT, 2012, Vitaly "_Vi" Shukela
# There are special rules for "SimpleBlock"/"Block" and "block" elements:
# "SimpleBlock" / "Block" should be in corresponding <Cluster>s / <BlockGroup>s respectively
# <duration> is ignored, <timecode> should be somewhere near Cluster's Timecode
# (note that Cluster's Timecode and this timecode have different formats:
# <Timecode>560000</Timecode> is just copied verbatim to matroska file as number, but
# <timecode>0.56</timecode> is converted to nanoseconds,
# scaled according to TimecodeScale and then stored into mkv.
# "block" should be in <Segment> and xml2mkv creates <Cluster> and/or <BlockGroup> automatically for them
# , using <duration> element.
# <timecode>s in blocks are absolute, unlike in actual matroska files
# Both
# cat somefile.mkv | ./mkv2xml | ./xml2mkv | mplayer -
# and
# cat somefile.mkv | ./mkv2xml -C | ./xml2mkv | mplayer -
# should work.
# You can edit mkv file on the fly with chain like
# ./mkv2xml -C < somefile.mkv | xml2 | sed 's!duration=.*!duration=2!' | 2xml | ./xml2mkv | mplayer -
import sys
from xml.sax import make_parser, handler
from struct import pack
import binascii
if sys.version < '3':
reload(sys)
sys.setdefaultencoding('utf-8') # hack to stop failing on Unicodish subtitles
maybe_encode=lambda x:x
range = xrange
maybe_encode_utf8=lambda x:x
else:
def maybe_encode(x):
if type(x) == str:
return x.encode("ascii")
else:
return x
chr=lambda x:bytes((x,))
maybe_encode_utf8=lambda x:x.encode("UTF-8")
def big_endian_number(number, signed=False):
if number<0:
r=0x100
while number + r < r/2:
r<<=8
number+=r
if number<0x100:
return chr(number)
signed=False
elif (number<0x100 and not signed) or number < 0x80:
return chr(number)
return big_endian_number(number>>8, signed) + 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")
class EbmlElementType:
VOID=0
MASTER=1 # read all subelements and return tree. Don't use this too large things like Segment
UNSIGNED=2
SIGNED=3
TEXTA=4
TEXTU=5
BINARY=6
FLOAT=7
DATE=8
JUST_GO_ON=10 # For "Segment".
# Actually MASTER, but don't build the tree for all subelements,
# interpreting all child elements as if they were top-level elements
EET=EbmlElementType
# lynx -width=10000 -dump http://matroska.org/technical/specs/index.html sed 's/not 0/not0/g; s/> 0/>0/g; s/Sampling Frequency/SamplingFrequency/g' | awk '{print $1 " " $3 " " $8}' | grep '\[..\]' | perl -ne '/(\S+) (\S+) (.)/; $name=$1; $id=$2; $type=$3; $id=~s/\[|\]//g; %types = (m=>"EET.MASTER", u=>"EET.UNSIGNED", i=>"EET.SIGNED", 8=>"EET.TEXTU", s=>"EET.TEXTA", b=>"EET.BINARY", f=>"EET.FLOAT", d=>"EET.DATE"); $t=$types{$type}; next unless $t; $id=~s/../\\x$&/g; $t="EET.JUST_GO_ON" if $name eq "Segment" or $name eq "Cluster"; print "\t\"$name\": (\"$id\", $t),\n";'
element_types_names = {
"EBML": (b"\x1A\x45\xDF\xA3", EET.MASTER),
"EBMLVersion": (b"\x42\x86", EET.UNSIGNED),
"EBMLReadVersion": (b"\x42\xF7", EET.UNSIGNED),
"EBMLMaxIDLength": (b"\x42\xF2", EET.UNSIGNED),
"EBMLMaxSizeLength": (b"\x42\xF3", EET.UNSIGNED),
"DocType": (b"\x42\x82", EET.TEXTA),
"DocTypeVersion": (b"\x42\x87", EET.UNSIGNED),
"DocTypeReadVersion": (b"\x42\x85", EET.UNSIGNED),
"Void": (b"\xEC", EET.BINARY),
"CRC-32": (b"\xBF", EET.BINARY),
"SignatureSlot": (b"\x1B\x53\x86\x67", EET.MASTER),
"SignatureAlgo": (b"\x7E\x8A", EET.UNSIGNED),
"SignatureHash": (b"\x7E\x9A", EET.UNSIGNED),
"SignaturePublicKey": (b"\x7E\xA5", EET.BINARY),
"Signature": (b"\x7E\xB5", EET.BINARY),
"SignatureElements": (b"\x7E\x5B", EET.MASTER),
"SignatureElementList": (b"\x7E\x7B", EET.MASTER),
"SignedElement": (b"\x65\x32", EET.BINARY),
"Segment": (b"\x18\x53\x80\x67", EET.JUST_GO_ON),
"SeekHead": (b"\x11\x4D\x9B\x74", EET.MASTER),
"Seek": (b"\x4D\xBB", EET.MASTER),
"SeekID": (b"\x53\xAB", EET.BINARY),
"SeekPosition": (b"\x53\xAC", EET.UNSIGNED),
"Info": (b"\x15\x49\xA9\x66", EET.MASTER),
"SegmentUID": (b"\x73\xA4", EET.BINARY),
"SegmentFilename": (b"\x73\x84", EET.TEXTU),
"PrevUID": (b"\x3C\xB9\x23", EET.BINARY),
"PrevFilename": (b"\x3C\x83\xAB", EET.TEXTU),
"NextUID": (b"\x3E\xB9\x23", EET.BINARY),
"NextFilename": (b"\x3E\x83\xBB", EET.TEXTU),
"SegmentFamily": (b"\x44\x44", EET.BINARY),
"ChapterTranslate": (b"\x69\x24", EET.MASTER),
"ChapterTranslateEditionUID": (b"\x69\xFC", EET.UNSIGNED),
"ChapterTranslateCodec": (b"\x69\xBF", EET.UNSIGNED),
"ChapterTranslateID": (b"\x69\xA5", EET.BINARY),
"TimecodeScale": (b"\x2A\xD7\xB1", EET.UNSIGNED),
"Duration": (b"\x44\x89", EET.FLOAT),
"DateUTC": (b"\x44\x61", EET.DATE),
"Title": (b"\x7B\xA9", EET.TEXTU),
"MuxingApp": (b"\x4D\x80", EET.TEXTU),
"WritingApp": (b"\x57\x41", EET.TEXTU),
"Cluster": (b"\x1F\x43\xB6\x75", EET.JUST_GO_ON),
"Timecode": (b"\xE7", EET.UNSIGNED),
"SilentTracks": (b"\x58\x54", EET.MASTER),
"SilentTrackNumber": (b"\x58\xD7", EET.UNSIGNED),
"Position": (b"\xA7", EET.UNSIGNED),
"PrevSize": (b"\xAB", EET.UNSIGNED),
"SimpleBlock": (b"\xA3", EET.BINARY),
"BlockGroup": (b"\xA0", EET.MASTER),
"Block": (b"\xA1", EET.BINARY),
"BlockVirtual": (b"\xA2", EET.BINARY),
"BlockAdditions": (b"\x75\xA1", EET.MASTER),
"BlockMore": (b"\xA6", EET.MASTER),
"BlockAddID": (b"\xEE", EET.UNSIGNED),
"BlockAdditional": (b"\xA5", EET.BINARY),
"BlockDuration": (b"\x9B", EET.UNSIGNED),
"ReferencePriority": (b"\xFA", EET.UNSIGNED),
"ReferenceBlock": (b"\xFB", EET.SIGNED),
"ReferenceVirtual": (b"\xFD", EET.SIGNED),
"CodecState": (b"\xA4", EET.BINARY),
"Slices": (b"\x8E", EET.MASTER),
"TimeSlice": (b"\xE8", EET.MASTER),
"LaceNumber": (b"\xCC", EET.UNSIGNED),
"FrameNumber": (b"\xCD", EET.UNSIGNED),
"BlockAdditionID": (b"\xCB", EET.UNSIGNED),
"Delay": (b"\xCE", EET.UNSIGNED),
"SliceDuration": (b"\xCF", EET.UNSIGNED),
"ReferenceFrame": (b"\xC8", EET.MASTER),
"ReferenceOffset": (b"\xC9", EET.UNSIGNED),
"ReferenceTimeCode": (b"\xCA", EET.UNSIGNED),
"EncryptedBlock": (b"\xAF", EET.BINARY),
"Tracks": (b"\x16\x54\xAE\x6B", EET.MASTER),
"TrackEntry": (b"\xAE", EET.MASTER),
"TrackNumber": (b"\xD7", EET.UNSIGNED),
"TrackUID": (b"\x73\xC5", EET.UNSIGNED),
"TrackType": (b"\x83", EET.UNSIGNED),
"FlagEnabled": (b"\xB9", EET.UNSIGNED),
"FlagDefault": (b"\x88", EET.UNSIGNED),
"FlagForced": (b"\x55\xAA", EET.UNSIGNED),
"FlagLacing": (b"\x9C", EET.UNSIGNED),
"MinCache": (b"\x6D\xE7", EET.UNSIGNED),
"MaxCache": (b"\x6D\xF8", EET.UNSIGNED),
"DefaultDuration": (b"\x23\xE3\x83", EET.UNSIGNED),
"TrackTimecodeScale": (b"\x23\x31\x4F", EET.FLOAT),
"TrackOffset": (b"\x53\x7F", EET.SIGNED),
"MaxBlockAdditionID": (b"\x55\xEE", EET.UNSIGNED),
"Name": (b"\x53\x6E", EET.TEXTU),
"Language": (b"\x22\xB5\x9C", EET.TEXTA),
"CodecID": (b"\x86", EET.TEXTA),
"CodecPrivate": (b"\x63\xA2", EET.BINARY),
"CodecName": (b"\x25\x86\x88", EET.TEXTU),
"AttachmentLink": (b"\x74\x46", EET.UNSIGNED),
"CodecSettings": (b"\x3A\x96\x97", EET.TEXTU),
"CodecInfoURL": (b"\x3B\x40\x40", EET.TEXTA),
"CodecDownloadURL": (b"\x26\xB2\x40", EET.TEXTA),
"CodecDecodeAll": (b"\xAA", EET.UNSIGNED),
"TrackOverlay": (b"\x6F\xAB", EET.UNSIGNED),
"TrackTranslate": (b"\x66\x24", EET.MASTER),
"TrackTranslateEditionUID": (b"\x66\xFC", EET.UNSIGNED),
"TrackTranslateCodec": (b"\x66\xBF", EET.UNSIGNED),
"TrackTranslateTrackID": (b"\x66\xA5", EET.BINARY),
"Video": (b"\xE0", EET.MASTER),
"FlagInterlaced": (b"\x9A", EET.UNSIGNED),
"StereoMode": (b"\x53\xB8", EET.UNSIGNED),
"OldStereoMode": (b"\x53\xB9", EET.UNSIGNED),
"PixelWidth": (b"\xB0", EET.UNSIGNED),
"PixelHeight": (b"\xBA", EET.UNSIGNED),
"PixelCropBottom": (b"\x54\xAA", EET.UNSIGNED),
"PixelCropTop": (b"\x54\xBB", EET.UNSIGNED),
"PixelCropLeft": (b"\x54\xCC", EET.UNSIGNED),
"PixelCropRight": (b"\x54\xDD", EET.UNSIGNED),
"DisplayWidth": (b"\x54\xB0", EET.UNSIGNED),
"DisplayHeight": (b"\x54\xBA", EET.UNSIGNED),
"DisplayUnit": (b"\x54\xB2", EET.UNSIGNED),
"AspectRatioType": (b"\x54\xB3", EET.UNSIGNED),
"ColourSpace": (b"\x2E\xB5\x24", EET.BINARY),
"GammaValue": (b"\x2F\xB5\x23", EET.FLOAT),
"FrameRate": (b"\x23\x83\xE3", EET.FLOAT),
"Audio": (b"\xE1", EET.MASTER),
"SamplingFrequency": (b"\xB5", EET.FLOAT),
"OutputSamplingFrequency": (b"\x78\xB5", EET.FLOAT),
"Channels": (b"\x9F", EET.UNSIGNED),
"ChannelPositions": (b"\x7D\x7B", EET.BINARY),
"BitDepth": (b"\x62\x64", EET.UNSIGNED),
"TrackOperation": (b"\xE2", EET.MASTER),
"TrackCombinePlanes": (b"\xE3", EET.MASTER),
"TrackPlane": (b"\xE4", EET.MASTER),
"TrackPlaneUID": (b"\xE5", EET.UNSIGNED),
"TrackPlaneType": (b"\xE6", EET.UNSIGNED),
"TrackJoinBlocks": (b"\xE9", EET.MASTER),
"TrackJoinUID": (b"\xED", EET.UNSIGNED),
"TrickTrackUID": (b"\xC0", EET.UNSIGNED),
"TrickTrackSegmentUID": (b"\xC1", EET.BINARY),
"TrickTrackFlag": (b"\xC6", EET.UNSIGNED),
"TrickMasterTrackUID": (b"\xC7", EET.UNSIGNED),
"TrickMasterTrackSegmentUID": (b"\xC4", EET.BINARY),
"ContentEncodings": (b"\x6D\x80", EET.MASTER),
"ContentEncoding": (b"\x62\x40", EET.MASTER),
"ContentEncodingOrder": (b"\x50\x31", EET.UNSIGNED),
"ContentEncodingScope": (b"\x50\x32", EET.UNSIGNED),
"ContentEncodingType": (b"\x50\x33", EET.UNSIGNED),
"ContentCompression": (b"\x50\x34", EET.MASTER),
"ContentCompAlgo": (b"\x42\x54", EET.UNSIGNED),
"ContentCompSettings": (b"\x42\x55", EET.BINARY),
"ContentEncryption": (b"\x50\x35", EET.MASTER),
"ContentEncAlgo": (b"\x47\xE1", EET.UNSIGNED),
"ContentEncKeyID": (b"\x47\xE2", EET.BINARY),
"ContentSignature": (b"\x47\xE3", EET.BINARY),
"ContentSigKeyID": (b"\x47\xE4", EET.BINARY),
"ContentSigAlgo": (b"\x47\xE5", EET.UNSIGNED),
"ContentSigHashAlgo": (b"\x47\xE6", EET.UNSIGNED),
"Cues": (b"\x1C\x53\xBB\x6B", EET.MASTER),
"CuePoint": (b"\xBB", EET.MASTER),
"CueTime": (b"\xB3", EET.UNSIGNED),
"CueTrackPositions": (b"\xB7", EET.MASTER),
"CueTrack": (b"\xF7", EET.UNSIGNED),
"CueClusterPosition": (b"\xF1", EET.UNSIGNED),
"CueBlockNumber": (b"\x53\x78", EET.UNSIGNED),
"CueCodecState": (b"\xEA", EET.UNSIGNED),
"CueReference": (b"\xDB", EET.MASTER),
"CueRefTime": (b"\x96", EET.UNSIGNED),
"CueRefCluster": (b"\x97", EET.UNSIGNED),
"CueRefNumber": (b"\x53\x5F", EET.UNSIGNED),
"CueRefCodecState": (b"\xEB", EET.UNSIGNED),
"Attachments": (b"\x19\x41\xA4\x69", EET.MASTER),
"AttachedFile": (b"\x61\xA7", EET.MASTER),
"FileDescription": (b"\x46\x7E", EET.TEXTU),
"FileName": (b"\x46\x6E", EET.TEXTU),
"FileMimeType": (b"\x46\x60", EET.TEXTA),
"FileData": (b"\x46\x5C", EET.BINARY),
"FileUID": (b"\x46\xAE", EET.UNSIGNED),
"FileReferral": (b"\x46\x75", EET.BINARY),
"FileUsedStartTime": (b"\x46\x61", EET.UNSIGNED),
"FileUsedEndTime": (b"\x46\x62", EET.UNSIGNED),
"Chapters": (b"\x10\x43\xA7\x70", EET.MASTER),
"EditionEntry": (b"\x45\xB9", EET.MASTER),
"EditionUID": (b"\x45\xBC", EET.UNSIGNED),
"EditionFlagHidden": (b"\x45\xBD", EET.UNSIGNED),
"EditionFlagDefault": (b"\x45\xDB", EET.UNSIGNED),
"EditionFlagOrdered": (b"\x45\xDD", EET.UNSIGNED),
"ChapterAtom": (b"\xB6", EET.MASTER),
"ChapterUID": (b"\x73\xC4", EET.UNSIGNED),
"ChapterTimeStart": (b"\x91", EET.UNSIGNED),
"ChapterTimeEnd": (b"\x92", EET.UNSIGNED),
"ChapterFlagHidden": (b"\x98", EET.UNSIGNED),
"ChapterFlagEnabled": (b"\x45\x98", EET.UNSIGNED),
"ChapterSegmentUID": (b"\x6E\x67", EET.BINARY),
"ChapterSegmentEditionUID": (b"\x6E\xBC", EET.UNSIGNED),
"ChapterPhysicalEquiv": (b"\x63\xC3", EET.UNSIGNED),
"ChapterTrack": (b"\x8F", EET.MASTER),
"ChapterTrackNumber": (b"\x89", EET.UNSIGNED),
"ChapterDisplay": (b"\x80", EET.MASTER),
"ChapString": (b"\x85", EET.TEXTU),
"ChapLanguage": (b"\x43\x7C", EET.TEXTA),
"ChapCountry": (b"\x43\x7E", EET.TEXTA),
"ChapProcess": (b"\x69\x44", EET.MASTER),
"ChapProcessCodecID": (b"\x69\x55", EET.UNSIGNED),
"ChapProcessPrivate": (b"\x45\x0D", EET.BINARY),
"ChapProcessCommand": (b"\x69\x11", EET.MASTER),
"ChapProcessTime": (b"\x69\x22", EET.UNSIGNED),
"ChapProcessData": (b"\x69\x33", EET.BINARY),
"Tags": (b"\x12\x54\xC3\x67", EET.MASTER),
"Tag": (b"\x73\x73", EET.MASTER),
"Targets": (b"\x63\xC0", EET.MASTER),
"TargetTypeValue": (b"\x68\xCA", EET.UNSIGNED),
"TargetType": (b"\x63\xCA", EET.TEXTA),
"TagTrackUID": (b"\x63\xC5", EET.UNSIGNED),
"TagEditionUID": (b"\x63\xC9", EET.UNSIGNED),
"TagChapterUID": (b"\x63\xC4", EET.UNSIGNED),
"TagAttachmentUID": (b"\x63\xC6", EET.UNSIGNED),
"SimpleTag": (b"\x67\xC8", EET.MASTER),
"TagName": (b"\x45\xA3", EET.TEXTU),
"TagLanguage": (b"\x44\x7A", EET.TEXTA),
"TagDefault": (b"\x44\x84", EET.UNSIGNED),
"TagString": (b"\x44\x87", EET.TEXTU),
"TagBinary": (b"\x44\x85", EET.BINARY),
"CodecDelay": (b"\x56\xAA", EET.UNSIGNED),
"SeekPreRoll": (b"\x56\xBB", EET.UNSIGNED),
"CueRelativePosition": (b"\xF0", EET.UNSIGNED),
"AlphaMode": (b"\x53\xC0", EET.UNSIGNED),
"BitsPerChannel": (b"\x55\xB2", EET.UNSIGNED),
"CbSubsamplingHorz": (b"\x55\xB5", EET.UNSIGNED),
"CbSubsamplingVert": (b"\x55\xB6", EET.UNSIGNED),
"ChapterStringUID": (b"\x56\x54", EET.TEXTU),
"ChromaSitingHorz": (b"\x55\xB7", EET.UNSIGNED),
"ChromaSitingVert": (b"\x55\xB8", EET.UNSIGNED),
"ChromaSubsamplingHorz": (b"\x55\xB3", EET.UNSIGNED),
"ChromaSubsamplingVert": (b"\x55\xB4", EET.UNSIGNED),
"Colour": (b"\x55\xB0", EET.MASTER),
"DefaultDecodedFieldDuration": (b"\x23\x4E\x7A", EET.UNSIGNED),
"DiscardPadding": (b"\x75\xA2", EET.SIGNED),
"FieldOrder": (b"\x9D", EET.UNSIGNED),
"LuminanceMax": (b"\x55\xD9", EET.FLOAT),
"LuminanceMin": (b"\x55\xDA", EET.FLOAT),
"MasteringMetadata": (b"\x55\xD0", EET.MASTER),
"MatrixCoefficients": (b"\x55\xB1", EET.UNSIGNED),
"MaxCLL": (b"\x55\xBC", EET.UNSIGNED),
"MaxFALL": (b"\x55\xBD", EET.UNSIGNED),
"Primaries": (b"\x55\xBB", EET.UNSIGNED),
"PrimaryBChromaticityX": (b"\x55\xD5", EET.FLOAT),
"PrimaryBChromaticityY": (b"\x55\xD6", EET.FLOAT),
"PrimaryGChromaticityX": (b"\x55\xD3", EET.FLOAT),
"PrimaryGChromaticityY": (b"\x55\xD4", EET.FLOAT),
"PrimaryRChromaticityX": (b"\x55\xD1", EET.FLOAT),
"PrimaryRChromaticityY": (b"\x55\xD2", EET.FLOAT),
"Range": (b"\x55\xB9", EET.UNSIGNED),
"TransferCharacteristics": (b"\x55\xBA", EET.UNSIGNED),
"WhitePointChromaticityX": (b"\x55\xD7", EET.FLOAT),
"WhitePointChromaticityY": (b"\x55\xD8", EET.FLOAT),
}
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
class Xml2Mkv(handler.ContentHandler):
def __init__(self):
self.stack = []
self.bufstack = []
self.timecode_scale = 1000000
self.naive_duration_editing_checker_duration = None
self.last_block_duration = 0
self.header_removal_compression = {} # track number -> buffer
self.tracks_current_track = None
self.tracks_current_ContentCompAlgo = None
self.tracks_current_ContentCompSettings = None
pass
def naive_duration_editing_checker(self, duration):
if not self.naive_duration_editing_checker_duration:
self.naive_duration_editing_checker_duration = duration
else:
if duration != self.naive_duration_editing_checker_duration:
sys.stderr.write("Looks like you edited <duration>, but we are ignoring it\n")
sys.stderr.write("<duration> is meaningful in <block>s that are used in\n")
sys.stderr.write("\"nocluster\" mode (mkv2xml -C)\n")
def startElement(self, name, attrs):
self.curname = name
self.chacter_data=[]
if name == "mkv2xml":
pass
elif name == "Segment":
sys.stdout.write(b"\x18\x53\x80\x67"+b"\xFF")
sys.stdout.write(b"\xEC\x40\x80" + (b"\x00" * 128)) # for convenience of other mkv tools
pass;
elif name == "track" or \
name == "duration" or \
name == "timecode":
pass
elif name == "data":
self.frame_text_content = False
if "encoding" in attrs:
if attrs["encoding"] == "text":
self.frame_text_content = True
elif name == "discardable":
self.frame_discardable = True
elif name == "keyframe":
self.frame_keyframe = True
elif name == "invisible":
self.frame_invisible = True
else:
if name in element_types_names:
(id_, type_) = element_types_names[name]
self.curtype = type_
self.stack.append((name, attrs));
self.bufstack.append(b"")
if "encoding" in attrs:
if attrs["encoding"] == "text":
self.curtype = EET.TEXTA
if name == "SimpleBlock" or \
name == "Block" or \
name == "block":
self.frame_discardable = False
self.frame_invisible = False
self.frame_keyframe = False
self.frame_curbuffer = ""
self.frame_buffers = []
self.frame_duration = None
self.curtype = None # prevent usual binary processing
if name == "BlockGroup":
self.naive_duration_editing_checker_duration = None
def characters(self, content):
self.chacter_data.append(content);
def characters_old(self, content):
if self.curname == "data":
self.frame_curbuffer+=content
return
if not content.isspace():
buf=""
if self.curname == "track":
self.frame_track = int(content)
return;
elif self.curname == "timecode":
self.frame_timecode = float(content)
return;
elif self.curname == "duration":
self.frame_duration = float(content)
return;
elif self.curtype == EET.TEXTA:
buf = str(content).encode("ascii")
elif self.curtype == EET.TEXTU:
buf = str(content).encode("UTF-8")
elif self.curtype == EET.UNSIGNED:
buf = big_endian_number(int(content))
if self.curname == "TimecodeScale":
self.timecode_scale = int(content)
elif self.curname == "Timecode":
self.last_cluster_timecode = int(content)
elif self.curname == "BlockDuration":
self.naive_duration_editing_checker(int(content))
elif self.curtype == EET.SIGNED:
buf = big_endian_number(int(content), True)
elif self.curtype == EET.BINARY:
content = content.replace("\n","").replace("\r","").replace(" ","");
buf = binascii.unhexlify(maybe_encode(content))
pass
elif self.curtype == EET.DATE:
d = float(content) # UNIX time
d-=978300000 # 2001-01-01T00:00:00,000000000
d*=1000000000.0;
buf = big_endian_number(int(d), True)
elif self.curtype == EET.FLOAT:
buf = pack(">d", float(content))
self.bufstack[-1]+=buf
def endElement(self, name):
content = "".join(self.chacter_data)
if content:
self.characters_old(content)
self.chacter_data=[]
if name == "track" or \
name == "timecode" or \
name == "discardable" or \
name == "keyframe" or \
name == "invisible" or \
name == "duration":
return
elif name == "data":
if self.frame_text_content:
self.frame_buffers.append(maybe_encode_utf8(str(self.frame_curbuffer)))
else:
text = self.frame_curbuffer.replace("\n", "").replace("\t", "").replace(" ", "")
self.frame_buffers.append(binascii.unhexlify(maybe_encode(text)))
self.frame_curbuffer=""
return
if name != "block":
if not name in element_types_names:
if not name == "mkv2xml":
sys.stderr.write("Unknown element %s\n"%name)
return
if name=="TrackEntry":
if self.tracks_current_ContentCompAlgo == 3:
self.header_removal_compression[self.tracks_current_track] = \
binascii.unhexlify(maybe_encode(self.tracks_current_ContentCompSettings))
self.tracks_current_ContentCompSettings = None
self.tracks_current_ContentCompAlgo = None
sys.stderr.write("Using header compression for track "+str(self.tracks_current_track)+"\n");
if name=="TrackNumber":
self.tracks_current_track = int(content)
if name=="ContentCompAlgo":
self.tracks_current_ContentCompAlgo = int(content)
if name=="ContentCompSettings":
self.tracks_current_ContentCompSettings = content
if name=="Segment":
return
(id_, type_) = (None, None)
if name != "block":
(id_, type_) = element_types_names[name];
if name == "SimpleBlock" or name == "Block" or name == "block":
absolute_timecode = int(self.frame_timecode * 1000000000 / self.timecode_scale)
relative_timecode = 0
if name != "block":
relative_timecode = absolute_timecode - self.last_cluster_timecode
if self.frame_duration:
scaled_duration = int(self.frame_duration * 1000000000 / self.timecode_scale)
self.naive_duration_editing_checker(scaled_duration)
if relative_timecode < -0x8000 or relative_timecode > 0x7FFF:
sys.stderr.write("Block timecode is too far from outer Cluster's timecode\n")
sys.stderr.write("Use no-cluster mode (mkv2xml -C) with <block> elements\n")
relative_timecode = 0;
if relative_timecode<0:
relative_timecode+=0x10000
flags = 0x00
if self.frame_keyframe: flags|=0x80
if self.frame_discardable: flags|=0x01
if self.frame_invisible: flags|=0x08
xiph_lace_lengths=[]
content = b""
header = None
if self.frame_track in self.header_removal_compression:
header = self.header_removal_compression[self.frame_track]
for i in self.frame_buffers:
if header is not None:
# apply header removal compression
if i[0:len(header)] != header:
sys.stderr.write("Unable to apply header compression here\n")
else:
i=i[len(header):]
content += i
xiph_lace_lengths.append(len(i))
xiph_lace=b""
xiph_lace_count = len(xiph_lace_lengths)-1
for i in range(0,xiph_lace_count):
l = xiph_lace_lengths[i]
while l>=255:
xiph_lace+=b"\xFF"
l-=255
xiph_lace+=chr(l)
frame = None
if len(xiph_lace_lengths) <= 1:
frame = ebml_encode_number(self.frame_track) + \
chr(relative_timecode>>8) + chr(relative_timecode&0xFF) + \
chr(flags) + \
content
else:
flags |= 0x02 # Xiph lacing
frame = ebml_encode_number(self.frame_track) + \
chr(relative_timecode>>8) + chr(relative_timecode&0xFF) + \
chr(flags) + \
chr(xiph_lace_count) + xiph_lace + \
content
if name == "block":
if not self.frame_duration:
cluster = ebml_element(0x1F43B675, b"" # Cluster
+ ebml_element(0xE7, big_endian_number(absolute_timecode)) # Timecode
+ ebml_element(0xA3, frame)) # SimpleBlock
else:
scaled_duration = int(self.frame_duration * 1000000000 / self.timecode_scale)
cluster = ebml_element(0x1F43B675, b"" # Cluster
+ ebml_element(0xE7, big_endian_number(absolute_timecode)) # Timecode
+ ebml_element(0xA0, b"" # BlockGroup
+ ebml_element(0x9B, ben(scaled_duration)) # BlockDuration
+ ebml_element(0xA1, frame))) # Block
sys.stdout.write(cluster)
return
else:
self.bufstack[-1] = frame
if not len(self.stack):
return
(_, attrs) = self.stack.pop();
buf = self.bufstack.pop();
if name == "WritingApp" or name == "MuxingApp":
if buf.find(b"xml2mkv") == -1:
buf+=b"; xml2mkv"
if len(self.bufstack):
self.bufstack[-1] += id_ + ebml_encode_number(len(buf)) + buf;
else:
sys.stdout.write(id_)
sys.stdout.write(ebml_encode_number(len(buf)))
sys.stdout.write(buf)
pass
if sys.version >= '3':
sys.stdout = sys.stdout.detach()
parser = make_parser()
parser.setContentHandler(Xml2Mkv())
parser.parse(sys.stdin)