Matroska.pm 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. #------------------------------------------------------------------------------
  2. # File: Matroska.pm
  3. #
  4. # Description: Read meta information from Matroska multimedia files
  5. #
  6. # Revisions: 05/26/2010 - P. Harvey Created
  7. #
  8. # References: 1) http://www.matroska.org/technical/specs/index.html
  9. #------------------------------------------------------------------------------
  10. package Image::ExifTool::Matroska;
  11. use strict;
  12. use vars qw($VERSION);
  13. use Image::ExifTool qw(:DataAccess :Utils);
  14. $VERSION = '1.07';
  15. my %noYes = ( 0 => 'No', 1 => 'Yes' );
  16. # Matroska tags
  17. # Note: The tag ID's in the Matroska documentation include the length designation
  18. # (the upper bits), which is not included in the tag ID's below
  19. %Image::ExifTool::Matroska::Main = (
  20. GROUPS => { 2 => 'Video' },
  21. VARS => { NO_LOOKUP => 1 }, # omit tags from lookup
  22. NOTES => q{
  23. The following tags are extracted from Matroska multimedia container files.
  24. This container format is used by file types such as MKA, MKV, MKS and WEBM.
  25. For speed, ExifTool extracts tags only up to the first Cluster unless the
  26. Verbose (-v) or Unknown = 2 (-U) option is used. See
  27. L<http://www.matroska.org/technical/specs/index.html> for the official
  28. Matroska specification.
  29. },
  30. # supported Format's: signed, unsigned, float, date, string, utf8
  31. # (or undef by default)
  32. #
  33. # EBML Header
  34. #
  35. 0xa45dfa3 => {
  36. Name => 'EBMLHeader',
  37. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  38. },
  39. 0x286 => { Name => 'EBMLVersion', Format => 'unsigned' },
  40. 0x2f7 => { Name => 'EBMLReadVersion', Format => 'unsigned' },
  41. 0x2f2 => { Name => 'EBMLMaxIDLength', Format => 'unsigned', Unknown => 1 },
  42. 0x2f3 => { Name => 'EBMLMaxSizeLength', Format => 'unsigned', Unknown => 1 },
  43. 0x282 => {
  44. Name => 'DocType',
  45. Format => 'string',
  46. # override FileType for "webm" files
  47. RawConv => '$self->OverrideFileType("WEBM") if $val eq "webm"; $val',
  48. },
  49. 0x287 => { Name => 'DocTypeVersion', Format => 'unsigned' },
  50. 0x285 => { Name => 'DocTypeReadVersion',Format => 'unsigned' },
  51. #
  52. # General
  53. #
  54. 0x3f => { Name => 'CRC-32', Binary => 1, Unknown => 1 },
  55. 0x6c => { Name => 'Void', NoSave => 1, Unknown => 1 },
  56. #
  57. # Signature
  58. #
  59. 0xb538667 => {
  60. Name => 'SignatureSlot',
  61. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  62. },
  63. 0x3e8a => { Name => 'SignatureAlgo', Format => 'unsigned' },
  64. 0x3e9a => { Name => 'SignatureHash', Format => 'unsigned' },
  65. 0x3ea5 => { Name =>'SignaturePublicKey',Binary => 1, Unknown => 1 },
  66. 0x3eb5 => { Name => 'Signature', Binary => 1, Unknown => 1 },
  67. 0x3e5b => {
  68. Name => 'SignatureElements',
  69. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  70. },
  71. 0x3e7b => {
  72. Name => 'SignatureElementList',
  73. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  74. },
  75. 0x2532 => { Name => 'SignedElement', Binary => 1, Unknown => 1 },
  76. #
  77. # Segment
  78. #
  79. 0x8538067 => {
  80. Name => 'SegmentHeader',
  81. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  82. },
  83. 0x14d9b74 => {
  84. Name => 'SeekHead',
  85. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  86. },
  87. 0xdbb => {
  88. Name => 'Seek',
  89. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  90. },
  91. 0x13ab => { Name => 'SeekID', Binary => 1, Unknown => 1 },
  92. 0x13ac => { Name => 'SeekPosition', Format => 'unsigned', Unknown => 1 },
  93. #
  94. # Segment Info
  95. #
  96. 0x549a966 => {
  97. Name => 'Info',
  98. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  99. },
  100. 0x33a4 => { Name => 'SegmentUID', Binary => 1, Unknown => 1 },
  101. 0x3384 => { Name => 'SegmentFileName', Format => 'utf8' },
  102. 0x1cb923 => { Name => 'PrevUID', Binary => 1, Unknown => 1 },
  103. 0x1c83ab => { Name => 'PrevFileName', Format => 'utf8' },
  104. 0x1eb923 => { Name => 'NextUID', Binary => 1, Unknown => 1 },
  105. 0x1e83bb => { Name => 'NextFileName', Format => 'utf8' },
  106. 0x0444 => { Name => 'SegmentFamily', Binary => 1, Unknown => 1 },
  107. 0x2924 => {
  108. Name => 'ChapterTranslate',
  109. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  110. },
  111. 0x29fc => { Name => 'ChapterTranslateEditionUID',Format => 'unsigned', Unknown => 1 },
  112. 0x29bf => {
  113. Name => 'ChapterTranslateCodec',
  114. Format => 'unsigned',
  115. PrintConv => { 0 => 'Matroska Script', 1 => 'DVD Menu' },
  116. },
  117. 0x29a5 => { Name => 'ChapterTranslateID',Binary => 1, Unknown => 1 },
  118. 0xad7b1 => {
  119. Name => 'TimecodeScale',
  120. Format => 'unsigned',
  121. RawConv => '$$self{TimecodeScale} = $val',
  122. ValueConv => '$val / 1e9',
  123. PrintConv => '($val * 1000) . " ms"',
  124. },
  125. 0x489 => {
  126. Name => 'Duration',
  127. Format => 'float',
  128. ValueConv => '$$self{TimecodeScale} ? $val * $$self{TimecodeScale} / 1e9 : $val',
  129. PrintConv => '$$self{TimecodeScale} ? ConvertDuration($val) : $val',
  130. },
  131. 0x461 => {
  132. Name => 'DateTimeOriginal', # called "DateUTC" by the spec
  133. Description => 'Date/Time Original',
  134. Groups => { 2 => 'Time' },
  135. Format => 'date',
  136. PrintConv => '$self->ConvertDateTime($val)',
  137. },
  138. 0x3ba9 => { Name => 'Title', Format => 'utf8' },
  139. 0xd80 => { Name => 'MuxingApp', Format => 'utf8' },
  140. 0x1741 => { Name => 'WritingApp', Format => 'utf8' },
  141. #
  142. # Cluster
  143. #
  144. 0xf43b675 => {
  145. Name => 'Cluster',
  146. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  147. },
  148. 0x67 => {
  149. Name => 'TimeCode',
  150. Format => 'unsigned',
  151. Unknown => 1,
  152. ValueConv => '$$self{TimecodeScale} ? $val * $$self{TimecodeScale} / 1e9 : $val',
  153. PrintConv => '$$self{TimecodeScale} ? ConvertDuration($val) : $val',
  154. },
  155. 0x1854 => {
  156. Name => 'SilentTracks',
  157. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  158. },
  159. 0x18d7 => { Name => 'SilentTrackNumber',Format => 'unsigned' },
  160. 0x27 => { Name => 'Position', Format => 'unsigned' },
  161. 0x2b => { Name => 'PrevSize', Format => 'unsigned' },
  162. 0x23 => { Name => 'SimpleBlock', NoSave => 1, Unknown => 1 },
  163. 0x20 => {
  164. Name => 'BlockGroup',
  165. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  166. },
  167. 0x21 => { Name => 'Block', NoSave => 1, Unknown => 1 },
  168. 0x22 => { Name => 'BlockVirtual', NoSave => 1, Unknown => 1 },
  169. 0x35a1 => {
  170. Name => 'BlockAdditions',
  171. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  172. },
  173. 0x26 => {
  174. Name => 'BlockMore',
  175. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  176. },
  177. 0x6e => { Name => 'BlockAddID', Format => 'unsigned', Unknown => 1 },
  178. 0x25 => { Name => 'BlockAdditional', NoSave => 1, Unknown => 1 },
  179. 0x1b => {
  180. Name => 'BlockDuration',
  181. Format => 'unsigned',
  182. Unknown => 1,
  183. ValueConv => '$$self{TimecodeScale} ? $val * $$self{TimecodeScale} / 1e9 : $val',
  184. PrintConv => '$$self{TimecodeScale} ? "$val s" : $val',
  185. },
  186. 0x7a => { Name => 'ReferencePriority',Format => 'unsigned', Unknown => 1 },
  187. 0x7b => {
  188. Name => 'ReferenceBlock',
  189. Format => 'signed',
  190. Unknown => 1,
  191. ValueConv => '$$self{TimecodeScale} ? $val * $$self{TimecodeScale} / 1e9 : $val',
  192. PrintConv => '$$self{TimecodeScale} ? "$val s" : $val',
  193. },
  194. 0x7d => { Name => 'ReferenceVirtual', Format => 'signed', Unknown => 1 },
  195. 0x24 => { Name => 'CodecState', Binary => 1, Unknown => 1 },
  196. 0x0e => {
  197. Name => 'Slices',
  198. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  199. },
  200. 0x68 => {
  201. Name => 'TimeSlice',
  202. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  203. },
  204. 0x4c => { Name => 'LaceNumber', Format => 'unsigned', Unknown => 1 },
  205. 0x4d => { Name => 'FrameNumber', Format => 'unsigned', Unknown => 1 },
  206. 0x4b => { Name => 'BlockAdditionalID',Format => 'unsigned', Unknown => 1 },
  207. 0x4e => { Name => 'Delay', Format => 'unsigned', Unknown => 1 },
  208. 0x4f => { Name => 'ClusterDuration', Format => 'unsigned', Unknown => 1 },
  209. 0x2f => { Name => 'EncryptedBlock', NoSave => 1, Unknown => 1 },
  210. #
  211. # Tracks
  212. #
  213. 0x654ae6b => {
  214. Name => 'Tracks',
  215. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  216. },
  217. 0x2e => {
  218. Name => 'TrackEntry',
  219. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  220. },
  221. 0x57 => { Name => 'TrackNumber', Format => 'unsigned' },
  222. 0x33c5 => { Name => 'TrackUID', Format => 'unsigned', Unknown => 1 },
  223. 0x03 => {
  224. Name => 'TrackType',
  225. Format => 'unsigned',
  226. PrintHex => 1,
  227. # remember types of all tracks encountered, as well as the current track type
  228. RawConv => '$$self{TrackTypes}{$val} = 1; $$self{TrackType} = $val',
  229. PrintConv => {
  230. 0x01 => 'Video',
  231. 0x02 => 'Audio',
  232. 0x03 => 'Complex', # (audio+video)
  233. 0x10 => 'Logo',
  234. 0x11 => 'Subtitle',
  235. 0x12 => 'Buttons',
  236. 0x20 => 'Control',
  237. },
  238. },
  239. 0x39 => { Name => 'TrackUsed', Format => 'unsigned', PrintConv => \%noYes },
  240. 0x08 => { Name => 'TrackDefault', Format => 'unsigned', PrintConv => \%noYes },
  241. 0x15aa => { Name => 'TrackForced', Format => 'unsigned', PrintConv => \%noYes },
  242. 0x1c => {
  243. Name => 'TrackLacing',
  244. Format => 'unsigned',
  245. Unknown => 1,
  246. PrintConv => \%noYes,
  247. },
  248. 0x2de7 => { Name => 'MinCache', Format => 'unsigned', Unknown => 1 },
  249. 0x2df8 => { Name => 'MaxCache', Format => 'unsigned', Unknown => 1 },
  250. 0x3e383 => [
  251. {
  252. Name => 'VideoFrameRate',
  253. Condition => '$$self{TrackType} and $$self{TrackType} == 0x01',
  254. Format => 'unsigned',
  255. ValueConv => '$val ? 1e9 / $val : 0',
  256. PrintConv => 'int($val * 1000 + 0.5) / 1000',
  257. },{
  258. Name => 'DefaultDuration',
  259. Format => 'unsigned',
  260. ValueConv => '$val / 1e9',
  261. PrintConv => '($val * 1000) . " ms"',
  262. }
  263. ],
  264. 0x3314f => { Name => 'TrackTimecodeScale',Format => 'float' },
  265. 0x137f => { Name => 'TrackOffset', Format => 'signed', Unknown => 1 },
  266. 0x15ee => { Name => 'MaxBlockAdditionID',Format => 'unsigned', Unknown => 1 },
  267. 0x136e => { Name => 'TrackName', Format => 'utf8' },
  268. 0x2b59c => { Name => 'TrackLanguage', Format => 'string' },
  269. 0x06 => [
  270. {
  271. Name => 'VideoCodecID',
  272. Condition => '$$self{TrackType} and $$self{TrackType} == 0x01',
  273. Format => 'string',
  274. },{
  275. Name => 'AudioCodecID',
  276. Condition => '$$self{TrackType} and $$self{TrackType} == 0x02',
  277. Format => 'string',
  278. },{
  279. Name => 'CodecID',
  280. Format => 'string',
  281. }
  282. ],
  283. 0x23a2 => { Name => 'CodecPrivate', Binary => 1, Unknown => 1 },
  284. 0x58688 => [
  285. {
  286. Name => 'VideoCodecName',
  287. Condition => '$$self{TrackType} and $$self{TrackType} == 0x01',
  288. Format => 'utf8',
  289. },{
  290. Name => 'AudioCodecName',
  291. Condition => '$$self{TrackType} and $$self{TrackType} == 0x02',
  292. Format => 'utf8',
  293. },{
  294. Name => 'CodecName',
  295. Format => 'utf8',
  296. }
  297. ],
  298. 0x3446 => { Name => 'TrackAttachmentUID',Format => 'unsigned' },
  299. 0x1a9697=>{ Name => 'CodecSettings', Format => 'utf8' },
  300. 0x1b4040=>{ Name => 'CodecInfoURL', Format => 'string' },
  301. 0x6b240 =>{ Name => 'CodecDownloadURL', Format => 'string' },
  302. 0x2a => { Name => 'CodecDecodeAll', Format => 'unsigned', PrintConv => \%noYes },
  303. 0x2fab => { Name => 'TrackOverlay', Format => 'unsigned', Unknown => 1 },
  304. 0x2624 => {
  305. Name => 'TrackTranslate',
  306. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  307. },
  308. 0x26fc => { Name => 'TrackTranslateEditionUID',Format => 'unsigned', Unknown => 1 },
  309. 0x26bf => {
  310. Name => 'TrackTranslateCodec',
  311. Format => 'unsigned',
  312. PrintConv => { 0 => 'Matroska Script', 1 => 'DVD Menu' },
  313. },
  314. 0x26a5 => { Name => 'TrackTranslateTrackID', Binary => 1, Unknown => 1 },
  315. #
  316. # Video
  317. #
  318. 0x60 => {
  319. Name => 'Video',
  320. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  321. },
  322. 0x1a => {
  323. Name => 'VideoScanType',
  324. Format => 'unsigned',
  325. PrintConv => {
  326. 0 => 'Progressive',
  327. 1 => 'Interlaced',
  328. },
  329. },
  330. 0x13b8 => {
  331. Name => 'Stereo3DMode',
  332. Format => 'unsigned',
  333. Printconv => {
  334. 0 => 'Mono',
  335. 1 => 'Right Eye',
  336. 2 => 'Left Eye',
  337. 3 => 'Both Eyes',
  338. },
  339. },
  340. 0x30 => { Name => 'ImageWidth', Format => 'unsigned' },
  341. 0x3a => { Name => 'ImageHeight', Format => 'unsigned' },
  342. 0x14aa => { Name => 'CropBottom', Format => 'unsigned' },
  343. 0x14bb => { Name => 'CropTop', Format => 'unsigned' },
  344. 0x14cc => { Name => 'CropLeft', Format => 'unsigned' },
  345. 0x14dd => { Name => 'CropRight', Format => 'unsigned' },
  346. 0x14b0 => { Name => 'DisplayWidth', Format => 'unsigned' },
  347. 0x14ba => { Name => 'DisplayHeight', Format => 'unsigned' },
  348. 0x14b2 => {
  349. Name => 'DisplayUnit',
  350. Format => 'unsigned',
  351. PrintConv => {
  352. 0 => 'Pixels',
  353. 1 => 'cm',
  354. 2 => 'inches',
  355. },
  356. },
  357. 0x14b3 => {
  358. Name => 'AspectRatioType',
  359. Format => 'unsigned',
  360. PrintConv => {
  361. 0 => 'Free Resizing',
  362. 1 => 'Keep Aspect Ratio',
  363. 2 => 'Fixed',
  364. },
  365. },
  366. 0xeb524 => { Name => 'ColorSpace', Binary => 1, Unknown => 1 },
  367. 0xfb523 => { Name => 'Gamma', Format => 'float' },
  368. 0x383e3 => { Name => 'FrameRate', Format => 'float' },
  369. #
  370. # Audio
  371. #
  372. 0x61 => {
  373. Name => 'Audio',
  374. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  375. },
  376. 0x35 => { Name => 'AudioSampleRate', Format => 'float', Groups => { 2 => 'Audio' } },
  377. 0x38b5 => { Name => 'OutputAudioSampleRate',Format => 'float', Groups => { 2 => 'Audio' } },
  378. 0x1f => { Name => 'AudioChannels', Format => 'unsigned', Groups => { 2 => 'Audio' } },
  379. 0x3d7b => {
  380. Name => 'ChannelPositions',
  381. Binary => 1,
  382. Unknown => 1,
  383. Groups => { 2 => 'Audio' },
  384. },
  385. 0x2264 => { Name => 'AudioBitsPerSample', Format => 'unsigned', Groups => { 2 => 'Audio' } },
  386. #
  387. # Content Encoding
  388. #
  389. 0x2d80 => {
  390. Name => 'ContentEncodings',
  391. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  392. },
  393. 0x2240 => {
  394. Name => 'ContentEncoding',
  395. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  396. },
  397. 0x1031 => { Name => 'ContentEncodingOrder', Format => 'unsigned', Unknown => 1 },
  398. 0x1032 => { Name => 'ContentEncodingScope', Format => 'unsigned', Unknown => 1 },
  399. 0x1033 => {
  400. Name => 'ContentEncodingType',
  401. Format => 'unsigned',
  402. PrintConv => { 0 => 'Compression', 1 => 'Encryption' },
  403. },
  404. 0x1034 => {
  405. Name => 'ContentCompression',
  406. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  407. },
  408. 0x254 => {
  409. Name => 'ContentCompressionAlgorithm',
  410. Format => 'unsigned',
  411. PrintConv => {
  412. 0 => 'zlib',
  413. 1 => 'bzlib',
  414. 2 => 'lzo1x',
  415. 3 => 'Header Stripping',
  416. },
  417. },
  418. 0x255 => { Name => 'ContentCompressionSettings',Binary => 1, Unknown => 1 },
  419. 0x1035 => {
  420. Name => 'ContentEncryption',
  421. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  422. },
  423. 0x7e1 => {
  424. Name => 'ContentEncryptionAlgorithm',
  425. Format => 'unsigned',
  426. PrintConv => {
  427. 0 => 'Not Encrypted',
  428. 1 => 'DES',
  429. 2 => '3DES',
  430. 3 => 'Twofish',
  431. 4 => 'Blowfish',
  432. 5 => 'AES',
  433. },
  434. },
  435. 0x7e2 => { Name => 'ContentEncryptionKeyID',Binary => 1, Unknown => 1 },
  436. 0x7e3 => { Name => 'ContentSignature', Binary => 1, Unknown => 1 },
  437. 0x7e4 => { Name => 'ContentSignatureKeyID', Binary => 1, Unknown => 1 },
  438. 0x7e5 => {
  439. Name => 'ContentSignatureAlgorithm',
  440. Format => 'unsigned',
  441. PrintConv => {
  442. 0 => 'Not Signed',
  443. 1 => 'RSA',
  444. },
  445. },
  446. 0x7e6 => {
  447. Name => 'ContentSignatureHashAlgorithm',
  448. Format => 'unsigned',
  449. PrintConv => {
  450. 0 => 'Not Signed',
  451. 1 => 'SHA1-160',
  452. 2 => 'MD5',
  453. },
  454. },
  455. #
  456. # Cues
  457. #
  458. 0xc53bb6b => {
  459. Name => 'Cues',
  460. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  461. },
  462. 0x3b => {
  463. Name => 'CuePoint',
  464. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  465. },
  466. 0x33 => {
  467. Name => 'CueTime',
  468. Format => 'unsigned',
  469. Unknown => 1,
  470. ValueConv => '$$self{TimecodeScale} ? $val * $$self{TimecodeScale} / 1e9 : $val',
  471. PrintConv => '$$self{TimecodeScale} ? ConvertDuration($val) : $val',
  472. },
  473. 0x37 => {
  474. Name => 'CueTrackPositions',
  475. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  476. },
  477. 0x77 => { Name => 'CueTrack', Format => 'unsigned', Unknown => 1 },
  478. 0x71 => { Name => 'CueClusterPosition',Format => 'unsigned', Unknown => 1 },
  479. 0x1378 => { Name => 'CueBlockNumber', Format => 'unsigned', Unknown => 1 },
  480. 0x6a => { Name => 'CueCodecState', Format => 'unsigned', Unknown => 1 },
  481. 0x5b => {
  482. Name => 'CueReference',
  483. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  484. },
  485. 0x16 => {
  486. Name => 'CueRefTime',
  487. Format => 'unsigned',
  488. Unknown => 1,
  489. ValueConv => '$$self{TimecodeScale} ? $val * $$self{TimecodeScale} / 1e9 : $val',
  490. PrintConv => '$$self{TimecodeScale} ? ConvertDuration($val) : $val',
  491. },
  492. 0x17 => { Name => 'CueRefCluster', Format => 'unsigned', Unknown => 1 },
  493. 0x135f=> { Name => 'CueRefNumber', Format => 'unsigned', Unknown => 1 },
  494. 0x6b => { Name => 'CueRefCodecState', Format => 'unsigned', Unknown => 1 },
  495. #
  496. # Attachments
  497. #
  498. 0x941a469 => {
  499. Name => 'Attachments',
  500. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  501. },
  502. 0x21a7 => {
  503. Name => 'AttachedFile',
  504. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  505. },
  506. 0x67e => { Name => 'AttachedFileDescription',Format => 'utf8' },
  507. 0x66e => { Name => 'AttachedFileName', Format => 'utf8' },
  508. 0x660 => { Name => 'AttachedFileMIMEType', Format => 'string' },
  509. 0x65c => { Name => 'AttachedFileData', Binary => 1 },
  510. 0x6ae => { Name => 'AttachedFileUID', Format => 'unsigned' },
  511. 0x675 => { Name => 'AttachedFileReferral', Binary => 1, Unknown => 1 },
  512. #
  513. # Chapters
  514. #
  515. 0x43a770 => {
  516. Name => 'Chapters',
  517. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  518. },
  519. 0x5b9 => {
  520. Name => 'EditionEntry',
  521. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  522. },
  523. 0x5bc => { Name => 'EditionUID', Format => 'unsigned', Unknown => 1 },
  524. 0x5bd => { Name => 'EditionFlagHidden', Format => 'unsigned', Unknown => 1 },
  525. 0x5db => { Name => 'EditionFlagDefault',Format => 'unsigned', Unknown => 1 },
  526. 0x5dd => { Name => 'EditionFlagOrdered',Format => 'unsigned', Unknown => 1 },
  527. 0x36 => {
  528. Name => 'ChapterAtom',
  529. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  530. },
  531. 0x33c4 => { Name => 'ChapterUID', Format => 'unsigned', Unknown => 1 },
  532. 0x11 => {
  533. Name => 'ChapterTimeStart',
  534. Groups => { 1 => 'Chapter#' },
  535. Format => 'unsigned',
  536. ValueConv => '$val / 1e9',
  537. PrintConv => 'ConvertDuration($val)',
  538. },
  539. 0x12 => {
  540. Name => 'ChapterTimeEnd',
  541. Format => 'unsigned',
  542. ValueConv => '$val / 1e9',
  543. PrintConv => 'ConvertDuration($val)',
  544. },
  545. 0x18 => { Name => 'ChapterFlagHidden', Format => 'unsigned', Unknown => 1 },
  546. 0x598 => { Name => 'ChapterFlagEnabled',Format => 'unsigned', Unknown => 1 },
  547. 0x2e67=> { Name => 'ChapterSegmentUID', Binary => 1, Unknown => 1 },
  548. 0x2ebc=> { Name => 'ChapterSegmentEditionUID', Binary => 1, Unknown => 1 },
  549. 0x23c3 => {
  550. Name => 'ChapterPhysicalEquivalent',
  551. Format => 'unsigned',
  552. PrintConv => {
  553. 10 => 'Index',
  554. 20 => 'Track',
  555. 30 => 'Session',
  556. 40 => 'Layer',
  557. 50 => 'Side',
  558. 60 => 'CD / DVD',
  559. 70 => 'Set / Package',
  560. },
  561. },
  562. 0x0f => {
  563. Name => 'ChapterTrack',
  564. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  565. },
  566. 0x09 => { Name => 'ChapterTrackNumber', Format => 'unsigned', Unknown => 1 },
  567. 0x00 => {
  568. Name => 'ChapterDisplay',
  569. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  570. },
  571. 0x05 => { Name => 'ChapterString', Format => 'utf8' },
  572. 0x37c => { Name => 'ChapterLanguage', Format => 'string' },
  573. 0x37e => { Name => 'ChapterCountry', Format => 'string' },
  574. 0x2944 => {
  575. Name => 'ChapterProcess',
  576. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  577. },
  578. 0x2955 => {
  579. Name => 'ChapterProcessCodecID',
  580. Format => 'unsigned',
  581. Unknown => 1,
  582. PrintConv => { 0 => 'Matroska', 1 => 'DVD' },
  583. },
  584. 0x50d => { Name => 'ChapterProcessPrivate', Binary => 1, Unknown => 1 },
  585. 0x2911 => {
  586. Name => 'ChapterProcessCommand',
  587. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  588. },
  589. 0x2922 => {
  590. Name => 'ChapterProcessTime',
  591. Format => 'unsigned',
  592. Unknown => 1,
  593. PrintConv => {
  594. 0 => 'For Duration of Chapter',
  595. 1 => 'Before Chapter',
  596. 2 => 'After Chapter',
  597. },
  598. },
  599. 0x2933 => { Name => 'ChapterProcessData', Binary => 1, Unknown => 1 },
  600. #
  601. # Tags
  602. #
  603. 0x254c367 => {
  604. Name => 'Tags',
  605. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  606. },
  607. 0x3373 => {
  608. Name => 'Tag',
  609. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  610. },
  611. 0x23c0 => {
  612. Name => 'Targets',
  613. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  614. },
  615. 0x28ca => { Name => 'TargetTypeValue', Format => 'unsigned' },
  616. 0x23ca => { Name => 'TargetType', Format => 'string' },
  617. 0x23c5 => { Name => 'TagTrackUID', Format => 'unsigned', Unknown => 1 },
  618. 0x23c9 => { Name => 'TagEditionUID', Format => 'unsigned', Unknown => 1 },
  619. 0x23c4 => { Name => 'TagChapterUID', Format => 'unsigned', Unknown => 1 },
  620. 0x23c6 => { Name => 'TagAttachmentUID', Format => 'unsigned', Unknown => 1 },
  621. 0x27c8 => {
  622. Name => 'SimpleTag',
  623. SubDirectory => { TagTable => 'Image::ExifTool::Matroska::Main' },
  624. },
  625. 0x5a3 => { Name => 'TagName', Format => 'utf8' },
  626. 0x47a => { Name => 'TagLanguage', Format => 'string' },
  627. 0x484 => { Name => 'TagDefault', Format => 'unsigned', PrintConv => \%noYes },
  628. 0x487 => { Name => 'TagString', Format => 'utf8' },
  629. 0x485 => { Name => 'TagBinary', Binary => 1 },
  630. );
  631. #------------------------------------------------------------------------------
  632. # Get variable-length Matroska integer
  633. # Inputs: 0) data buffer, 1) position in data
  634. # Returns: integer value and updates position, -1 for unknown/reserved value,
  635. # or undef if no data left
  636. sub GetVInt($$)
  637. {
  638. return undef if $_[1] >= length $_[0];
  639. my $val = ord(substr($_[0], $_[1]++));
  640. my $num = 0;
  641. unless ($val) {
  642. return undef if $_[1] >= length $_[0];
  643. $val = ord(substr($_[0], $_[1]++));
  644. return undef unless $val; # can't be this large!
  645. $num += 7; # 7 more bytes to read (we just read one)
  646. }
  647. my $mask = 0x7f;
  648. while ($val == ($val & $mask)) {
  649. $mask >>= 1;
  650. ++$num;
  651. }
  652. $val = ($val & $mask);
  653. my $unknown = ($val == $mask);
  654. return undef if $_[1] + $num > length $_[0];
  655. while ($num) {
  656. my $b = ord(substr($_[0], $_[1]++));
  657. $unknown = 0 if $b != 0xff;
  658. $val = $val * 256 + $b;
  659. --$num;
  660. }
  661. return $unknown ? -1 : $val;
  662. }
  663. #------------------------------------------------------------------------------
  664. # Read information from a Matroska multimedia file (MKV, MKA, MKS)
  665. # Inputs: 0) ExifTool object reference, 1) Directory information reference
  666. # Returns: 1 on success, 0 if this wasn't a valid Matroska file
  667. sub ProcessMKV($$)
  668. {
  669. my ($et, $dirInfo) = @_;
  670. my $raf = $$dirInfo{RAF};
  671. my ($buff, $buf2, @dirEnd, $trackIndent, %trackTypes);
  672. $raf->Read($buff, 4) == 4 or return 0;
  673. return 0 unless $buff =~ /^\x1a\x45\xdf\xa3/;
  674. # read in 64kB blocks (already read 4 bytes)
  675. $raf->Read($buff, 65532) or return 0;
  676. my $dataLen = length $buff;
  677. my ($pos, $dataPos) = (0, 4);
  678. # verify header length
  679. my $hlen = GetVInt($buff, $pos);
  680. return 0 unless $hlen and $hlen > 0;
  681. $pos + $hlen > $dataLen and $et->Warn('Truncated Matroska header'), return 1;
  682. $et->SetFileType();
  683. SetByteOrder('MM');
  684. my $tagTablePtr = GetTagTable('Image::ExifTool::Matroska::Main');
  685. # set flag to process entire file (otherwise we stop at the first Cluster)
  686. my $verbose = $et->Options('Verbose');
  687. my $processAll = ($verbose or $et->Options('Unknown') > 1);
  688. $$et{TrackTypes} = \%trackTypes; # store Track types reference
  689. my $oldIndent = $$et{INDENT};
  690. my $chapterNum = 0;
  691. # loop over all Matroska elements
  692. for (;;) {
  693. while (@dirEnd and $pos + $dataPos >= $dirEnd[-1][0]) {
  694. pop @dirEnd;
  695. # use INDENT to decide whether or not we are done this Track element
  696. delete $$et{SET_GROUP1} if $trackIndent and $trackIndent eq $$et{INDENT};
  697. $$et{INDENT} = substr($$et{INDENT}, 0, -2);
  698. }
  699. # read more if we are getting close to the end of our buffer
  700. # (24 more bytes should be enough to read this element header)
  701. if ($pos + 24 > $dataLen and $raf->Read($buf2, 65536)) {
  702. $buff = substr($buff, $pos) . $buf2;
  703. undef $buf2;
  704. $dataPos += $pos;
  705. $dataLen = length $buff;
  706. $pos = 0;
  707. }
  708. my $tag = GetVInt($buff, $pos);
  709. last unless defined $tag and $tag >= 0;
  710. my $size = GetVInt($buff, $pos);
  711. last unless defined $size;
  712. my $unknownSize;
  713. $size < 0 and $unknownSize = 1, $size = 1e20;
  714. if (@dirEnd and $pos + $dataPos + $size > $dirEnd[-1][0]) {
  715. $et->Warn("Invalid or corrupted $dirEnd[-1][1] master element");
  716. $pos = $dirEnd[-1][0] - $dataPos;
  717. if ($pos < 0 or $pos > $dataLen) {
  718. $buff = '';
  719. $dataPos += $pos;
  720. $dataLen = 0;
  721. $pos = 0;
  722. $raf->Seek($dataPos, 0) or last;
  723. }
  724. next;
  725. }
  726. my $tagInfo = $et->GetTagInfo($tagTablePtr, $tag);
  727. # just fall through into the contained EBML elements
  728. if ($tagInfo and $$tagInfo{SubDirectory}) {
  729. # stop processing at first cluster unless we are in verbose mode
  730. last if $$tagInfo{Name} eq 'Cluster' and not $processAll;
  731. $$et{INDENT} .= '| ';
  732. $et->VerboseDir($$tagTablePtr{$tag}{Name}, undef, $size);
  733. push @dirEnd, [ $pos + $dataPos + $size, $$tagInfo{Name} ];
  734. if ($$tagInfo{Name} eq 'ChapterAtom') {
  735. $$et{SET_GROUP1} = 'Chapter' . (++$chapterNum);
  736. $trackIndent = $$et{INDENT};
  737. }
  738. next;
  739. }
  740. last if $unknownSize;
  741. if ($pos + $size > $dataLen) {
  742. # how much more do we need to read?
  743. my $more = $pos + $size - $dataLen;
  744. # just skip unknown and large data blocks
  745. if (not $tagInfo or $more > 10000000) {
  746. # don't try to skip very large blocks unless LargeFileSupport is enabled
  747. last if $more > 0x80000000 and not $et->Options('LargeFileSupport');
  748. $raf->Seek($more, 1) or last;
  749. $buff = '';
  750. $dataPos += $dataLen + $more;
  751. $dataLen = 0;
  752. $pos = 0;
  753. next;
  754. } else {
  755. # read data in multiples of 64kB
  756. $more = (int($more / 65536) + 1) * 65536;
  757. if ($raf->Read($buf2, $more)) {
  758. $buff = substr($buff, $pos) . $buf2;
  759. undef $buf2;
  760. $dataPos += $pos;
  761. $dataLen = length $buff;
  762. $pos = 0;
  763. }
  764. last if $pos + $size > $dataLen;
  765. }
  766. }
  767. unless ($tagInfo) {
  768. # ignore the element
  769. $pos += $size;
  770. next;
  771. }
  772. my $val;
  773. if ($$tagInfo{Format}) {
  774. my $fmt = $$tagInfo{Format};
  775. if ($fmt eq 'string' or $fmt eq 'utf8') {
  776. ($val = substr($buff, $pos, $size)) =~ s/\0.*//s;
  777. $val = $et->Decode($val, 'UTF8') if $fmt eq 'utf8';
  778. } elsif ($fmt eq 'float') {
  779. if ($size == 4) {
  780. $val = GetFloat(\$buff, $pos);
  781. } elsif ($size == 8) {
  782. $val = GetDouble(\$buff, $pos);
  783. } else {
  784. $et->Warn("Illegal float size ($size)");
  785. }
  786. } else {
  787. my @vals = unpack("x${pos}C$size", $buff);
  788. $val = 0;
  789. if ($fmt eq 'signed' or $fmt eq 'date') {
  790. my $over = 1;
  791. foreach (@vals) {
  792. $val = $val * 256 + $_;
  793. $over *= 256;
  794. }
  795. # interpret negative numbers
  796. $val -= $over if $vals[0] & 0x80;
  797. # convert dates (nanoseconds since 2001:01:01)
  798. if ($fmt eq 'date') {
  799. my $t = $val / 1e9;
  800. my $f = $t - int($t); # fractional seconds
  801. $f =~ s/^\d+//; # remove leading zero
  802. # (8 leap days between 1970 and 2001)
  803. $t += (((2001-1970)*365+8)*24*3600);
  804. $val = Image::ExifTool::ConvertUnixTime($t) . $f . 'Z';
  805. }
  806. } else { # must be unsigned
  807. $val = $val * 256 + $_ foreach @vals;
  808. }
  809. }
  810. # set group1 to Track/Chapter number
  811. if ($$tagInfo{Name} eq 'TrackNumber') {
  812. $$et{SET_GROUP1} = 'Track' . $val;
  813. $trackIndent = $$et{INDENT};
  814. }
  815. }
  816. my %parms = (
  817. DataPt => \$buff,
  818. DataPos => $dataPos,
  819. Start => $pos,
  820. Size => $size,
  821. );
  822. if ($$tagInfo{NoSave}) {
  823. $et->VerboseInfo($tag, $tagInfo, Value => $val, %parms) if $verbose;
  824. } else {
  825. $et->HandleTag($tagTablePtr, $tag, $val, %parms);
  826. }
  827. $pos += $size; # step to next element
  828. }
  829. $$et{INDENT} = $oldIndent;
  830. delete $$et{SET_GROUP1};
  831. # override file type if necessary based on existing track types
  832. unless ($trackTypes{0x01} or $trackTypes{0x03}) { # video or complex?
  833. if ($trackTypes{0x02}) { # audio?
  834. $et->OverrideFileType('MKA');
  835. } elsif ($trackTypes{0x11}) { # subtitle?
  836. $et->OverrideFileType('MKS');
  837. }
  838. }
  839. return 1;
  840. }
  841. 1; # end
  842. __END__
  843. =head1 NAME
  844. Image::ExifTool::Matroska - Read meta information from Matroska files
  845. =head1 SYNOPSIS
  846. This module is used by Image::ExifTool
  847. =head1 DESCRIPTION
  848. This module contains definitions required by Image::ExifTool to read meta
  849. information from Matroska multimedia files (MKA, MKV, MKS and WEBM).
  850. =head1 AUTHOR
  851. Copyright 2003-2016, Phil Harvey (phil at owl.phy.queensu.ca)
  852. This library is free software; you can redistribute it and/or modify it
  853. under the same terms as Perl itself.
  854. =head1 REFERENCES
  855. =over 4
  856. =item L<http://www.matroska.org/technical/specs/index.html>
  857. =back
  858. =head1 SEE ALSO
  859. L<Image::ExifTool::TagNames/Matroska Tags>,
  860. L<Image::ExifTool(3pm)|Image::ExifTool>
  861. =cut