I have a Windows application I would like to add the ability to retrieve the DateTimeOriginal information from the H264 video stream of MTS files.
https://exiftool.org/TagNames/H264.html#MDPM
I have been searching the internet for information on how the MPEG2 Transport Stream is encoded in an attempt to understand and then retrieve this information from the file. I see that the ExifTool does this quite well. I was hoping someone might be able to point me in the right direction on how to do this or possibly even provide a simple example?
Any help would be much appreciated!
The lib/Image/ExifTool/H264.pm source code (available in the full ExifTool distribution, or online here (http://cpansearch.perl.org/src/EXIFTOOL/Image-ExifTool-8.90/lib/Image/ExifTool/H264.pm)) contains what you requested, including all of the references I could locate. You will also need to look at the M2TS.pm source code to figure out how to unpack the container format.
But beware, this won't be easy. Read my commentary (https://exiftool.org/commentary.html#AVCHD) on the M2TS/H264 formats.
Also, you may find the exiftool -v3 option very useful for helping to figure this out:
> exiftool t/images/M2TS.mts -v3
ExifToolVersion = 8.93
FileName = M2TS.mts
Directory = t/images
FileSize = 1344
FileModifyDate = 1247075848
FilePermissions = 33188
FileType = M2TS
MIMEType = video/m2ts
Transport packet 0:
00c0: 00 00 00 00 47 40 00 10 00 00 b0 11 00 00 c1 00 [....G@..........]
00d0: 00 00 01 e1 00 00 00 e0 1f e9 62 e7 63 ff ff ff [..........b.c...]
00e0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
00f0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
0100: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
0110: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
0120: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
0130: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
0140: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
0150: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
0160: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
0170: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
Timecode: 0x0000
Packet ID: 0x0000 (Program Association Table)
Start Flag: Yes
Program Association Table length: 17
Stream ID: 0x0
Section No: 0
Last Sect.: 0
PID(0x0100) --> Program 1 Map
PID(0x001f) --> Program 0 Map
Transport packet 1:
0180: 00 00 4d 05 47 41 00 10 00 02 b0 39 00 01 c1 00 [..M.GA.....9....]
0190: 00 f0 01 f0 0c 05 04 48 44 4d 56 88 04 0f ff fc [.......HDMV.....]
01a0: fc 1b f0 11 f0 0a 05 08 48 44 4d 56 ff 1b 43 3f [........HDMV..C?]
01b0: 81 f1 00 f0 0c 05 04 41 43 2d 33 81 04 04 30 04 [.......AC-3...0.]
01c0: 00 d7 fe f5 36 ff ff ff ff ff ff ff ff ff ff ff [....6...........]
01d0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
01e0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
01f0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
0200: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
0210: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
0220: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
0230: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
Timecode: 0x0000
Packet ID: 0x0100 (Program 1 Map)
Start Flag: Yes
Program Map Table length: 57
Program No: 1
Section No: 0
Last Sect.: 0
PID(0x1001) --> Program 1 Clock Reference
Program Descriptor: Type=0x05 "HDMV"
Program Descriptor: Type=0x88 "\x0f\xff\xfc\xfc"
PID(0x1011) --> Program 1 H.264 Video (0x1b)
VideoStreamType = 27
ES Descriptor: Type=0x05 "HDMV\xff\x1bC?"
PID(0x1100) --> Program 1 A52/AC-3 Audio (0x81)
AudioStreamType = 129
ES Descriptor: Type=0x05 "AC-3"
ES Descriptor: Type=0x81 "\x040\x04\x00"
AudioBitrate = 12
SurroundMode = 0
AudioChannels = 2
Transport packet 2:
0240: 00 00 55 d5 47 40 1f 10 00 7f f0 19 ff ff c1 00 [..U.G@..........]
0250: 00 f0 0a 63 08 c0 af c8 ff ff ff ff ff 00 01 80 [...c............]
0260: 00 ae b4 3a 35 ff ff ff ff ff ff ff ff ff ff ff [...:5...........]
0270: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
0280: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
0290: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
02a0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
02b0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
02c0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
02d0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
02e0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
02f0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
Timecode: 0x0000
Packet ID: 0x001f (Program 0 Map)
Start Flag: Yes
(skipping Selection Information Table)
Transport packet 3:
0300: 00 00 5e a5 47 10 01 20 b7 10 00 00 00 28 fe 2d [..^.G.. .....(.-]
0310: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
0320: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
0330: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
0340: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
0350: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
0360: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
0370: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
0380: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
0390: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
03a0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
03b0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff [................]
Timecode: 0x0000
Packet ID: 0x1001 (Program 1 Clock Reference)
Start Flag: No
Transport packet 4:
03c0: 00 00 81 e5 47 70 11 10 00 00 01 e0 00 00 81 c0 [....Gp..........]
03d0: 0a 31 00 03 a7 c5 11 00 03 53 65 00 00 00 01 09 [.1.......Se.....]
03e0: 10 00 00 00 01 27 64 00 28 ad 00 ec 07 80 44 7d [.....'d.(.....D}]
03f0: e0 22 00 00 03 00 02 00 00 03 00 65 d1 40 01 e8 [.".........e.@..]
0400: 48 00 3d 09 3e f7 06 8a 00 0f 42 40 01 e8 49 f7 [H.=.>.....B@..I.]
0410: b8 14 00 00 00 01 28 ea 40 13 72 c0 00 00 00 01 [......(.@.r.....]
0420: 06 00 09 d7 e4 00 00 57 e4 00 00 40 05 38 17 ee [.......W...@.8..]
0430: 8c 60 f8 4d 11 d9 8c d6 08 00 20 0c 9a 66 4d 44 [.`.M...... ..fMD]
0440: 50 4d 07 18 02 20 08 12 19 31 23 16 24 70 ca f4 [PM... ...1#.$p..]
0450: ff ff 71 ff ff ff ff 7f 00 00 65 84 e0 10 11 30 [..q.......e....0]
0460: 00 e1 06 ff ff ff 01 07 00 00 03 00 00 03 00 06 [................]
0470: 14 80 00 00 00 01 25 88 85 03 12 27 ce 9e 10 00 [......%....'....]
Timecode: 0x0000
Packet ID: 0x1011 (Program 1 H.264 Video (0x1b))
Start Flag: Yes
Stream ID: 0xe0
Packet Len: 0
Transport packet 5:
0480: 00 00 8a b5 47 30 11 11 5f e1 97 ff f3 f5 9d 5e [....G0.._......^]
0490: 8b 3f a1 79 83 44 6e 73 47 0e 67 b0 d4 e1 fb 61 [.?.y.DnsG.g....a]
04a0: 68 b4 74 ba 36 e9 59 77 3b 07 ea 2c 78 59 b0 7d [h.t.6.Yw;..,xY.}]
04b0: d4 3a 61 a9 53 92 6d 0c 2d 39 cc 02 93 9a 48 f1 [.:a.S.m.-9....H.]
04c0: 72 83 40 14 56 84 5d 4a 3b 55 79 56 c6 ec 94 74 [r.@.V.]J;UyV...t]
04d0: e3 2f dc ea c9 fa 30 76 a3 54 b5 dd 09 89 54 37 [./....0v.T....T7]
04e0: 9a 7d 3e dc 79 b2 0f f5 d4 81 41 eb 51 a2 0d d4 [.}>.y.....A.Q...]
04f0: c0 1c a0 5c 85 94 d3 1f 87 d4 b5 20 b8 16 47 2b [...\....... ..G+]
0500: 6e 35 04 fd 82 40 85 6b 4a 75 c6 98 8f de 6e f0 [n5...@.kJu....n.]
0510: a8 4b f9 7f 13 d6 3f f4 2e 87 2a fd 94 91 db ce [.K....?...*.....]
0520: bc 85 63 64 c5 73 3d e2 a3 d8 5a 8c d4 b2 e2 4e [..cd.s=...Z....N]
0530: 50 62 0b 23 74 75 f0 f9 01 3c 17 cb 71 d8 50 1f [Pb.#tu...<..q.P.]
Timecode: 0x0000
Packet ID: 0x1011 (Program 1 H.264 Video (0x1b))
Start Flag: No
Transport packet 6:
0540: 00 09 63 85 47 71 00 10 00 00 01 fd 04 0b 81 81 [..c.Gq..........]
0550: 08 21 00 03 6f 85 0f 81 71 0b 77 16 9d 18 20 43 [.!..o...q.w... C]
0560: fe 21 0c a0 f8 60 91 55 70 55 5d f3 aa ea d3 26 [.!...`.UpU]....&]
0570: 54 f9 0d 77 ca 61 43 85 4a 9a a7 b4 9f d1 ae f9 [T..w.aC.J.......]
0580: 4b f8 4f 69 be 7c f9 f3 e7 d0 a1 3f 79 ae 3a 98 [K.Oi.|.....?y.:.]
0590: 6f 9d 3d 86 f9 fa 57 d0 d2 d7 4c e5 fc e8 4e 5f [o.=...W...L...N_]
05a0: 3f 7b 0a bb db f3 97 59 09 fa a7 cf d4 da a6 f6 [?{.....Y........]
05b0: c2 5a 4f d2 d6 87 65 bc f8 91 00 ff e2 8f 83 06 [.ZO...e.........]
05c0: 0e 2e bc 97 5a 23 5f eb d0 ad 38 32 49 0b 48 a8 [....Z#_...82I.H.]
05d0: b0 8e 86 91 3b b1 2b c6 0f 94 ed a0 b3 04 8b ad [....;.+.........]
05e0: ac 84 ce 35 26 57 06 44 e1 87 55 b6 d4 a0 27 b3 [...5&W.D..U...'.]
05f0: 7b 20 eb 51 81 2a 64 c9 3b ca 86 75 a6 b5 d1 80 [{ .Q.*d.;..u....]
Timecode: 0x0000
Packet ID: 0x1100 (Program 1 A52/AC-3 Audio (0x81))
Start Flag: Yes
Stream ID: 0xfd
Packet Len: 1035
End of file. Missing PID(s): 0x1011 0x1100
Parsing stream 0x1011 (Program 1 H.264 Video (0x1b))
0000: 00 00 00 01 09 10 00 00 00 01 27 64 00 28 ad 00 [..........'d.(..]
0010: ec 07 80 44 7d e0 22 00 00 03 00 02 00 00 03 00 [...D}.".........]
0020: 65 d1 40 01 e8 48 00 3d 09 3e f7 06 8a 00 0f 42 [e.@..H.=.>.....B]
0030: 40 01 e8 49 f7 b8 14 00 00 00 01 28 ea 40 13 72 [@..I.......(.@.r]
0040: c0 00 00 00 01 06 00 09 d7 e4 00 00 57 e4 00 00 [............W...]
[snip 269 bytes]
NAL Unit Type: 0x9 (1 bytes)
0000: 10 [.]
NAL Unit Type: 0x7 (42 bytes)
0000: 64 00 28 ad 00 ec 07 80 44 7d e0 22 00 00 00 02 [d.(.....D}."....]
0010: 00 00 00 65 d1 40 01 e8 48 00 3d 09 3e f7 06 8a [...e.@..H.=.>...]
0020: 00 0f 42 40 01 e8 49 f7 b8 14 [..B@..I...]
ImageWidth = 1920
ImageHeight = 1080
NAL Unit Type: 0x8 (5 bytes)
0000: ea 40 13 72 c0 [.@.r.]
NAL Unit Type: 0x6 (79 bytes)
0000: 00 09 d7 e4 00 00 57 e4 00 00 40 05 38 17 ee 8c [......W...@.8...]
0010: 60 f8 4d 11 d9 8c d6 08 00 20 0c 9a 66 4d 44 50 [`.M...... ..fMDP]
0020: 4d 07 18 02 20 08 12 19 31 23 16 24 70 ca f4 ff [M... ...1#.$p...]
0030: ff 71 ff ff ff ff 7f 00 00 65 84 e0 10 11 30 00 [.q.......e....0.]
0040: e1 06 ff ff ff 01 07 00 00 00 00 00 06 14 80 [...............]
+ [MDPM directory with 7 entries]
| 0-1) DateTimeOriginal = . ..1#.$
| - Tag 0x0018 (8 bytes):
| 0000: 02 20 08 12 31 23 16 24 [. ..1#.$]
| 2) Camera1 (SubDirectory) -->
| - Tag 0x0070 (4 bytes):
| 0000: ca f4 ff ff [....]
| + [BinaryData directory, 4 bytes]
| | ApertureSetting = 202
| | - Tag 0x0000 (1 bytes, int8u[1]):
| | 0000: ca [.]
| | Gain = 4
| | - Tag 0x0001, mask 0x0f (1 bytes, int8u[1]):
| | 0001: f4 [.]
| | ExposureProgram = 240
| | - Tag '1.1', mask 0xf0 (1 bytes, int8u[1]):
| | 0001: f4 [.]
| | WhiteBalance = 224
| | - Tag '2.1', mask 0xe0 (1 bytes, int8u[1]):
| | 0002: ff [.]
| | Focus = 255
| | - Tag 0x0003 (1 bytes, int8u[1]):
| | 0003: ff [.]
| 3) Camera2 (SubDirectory) -->
| - Tag 0x0071 (4 bytes):
| 0000: ff ff ff ff [....]
| + [BinaryData directory, 4 bytes]
| | ImageStabilization = 255
| | - Tag 0x0001 (1 bytes, int8u[1]):
| | 0001: ff [.]
| 4) Shutter (SubDirectory) -->
| - Tag 0x007f (4 bytes):
| 0000: 00 00 65 84 [..e.]
| + [BinaryData directory, 4 bytes]
| | ExposureTime = 1125
| | - Tag '1.1', mask 0x7fff (2 bytes, int16u[1]):
| | 0002: 65 84 [e.]
| 5) MakeModel (SubDirectory) -->
| - Tag 0x00e0 (4 bytes):
| 0000: 10 11 30 00 [..0.]
| + [BinaryData directory, 4 bytes]
| | Make = 4113
| | - Tag 0x0000 (2 bytes, int16u[1]):
| | 0000: 10 11 [..]
| 6) RecInfo (SubDirectory) -->
| - Tag 0x00e1 (4 bytes):
| 0000: 06 ff ff ff [....]
| + [BinaryData directory, 4 bytes]
| | RecordingMode = 6
| | - Tag 0x0000 (1 bytes, int8u[1]):
| | 0000: 06 [.]
NAL Unit Type: 0x5 (193 bytes)
0000: 88 85 03 12 27 ce 9e 10 00 5f e1 97 ff f3 f5 9d [....'...._......]
0010: 5e 8b 3f a1 79 83 44 6e 73 47 0e 67 b0 d4 e1 fb [^.?.y.DnsG.g....]
0020: 61 68 b4 74 ba 36 e9 59 77 3b 07 ea 2c 78 59 b0 [ah.t.6.Yw;..,xY.]
0030: 7d d4 3a 61 a9 53 92 6d 0c 2d 39 cc 02 93 9a 48 [}.:a.S.m.-9....H]
0040: f1 72 83 40 14 56 84 5d 4a 3b 55 79 56 c6 ec 94 [.r.@.V.]J;UyV...]
[snip 113 bytes]
Parsing stream 0x1100 (Program 1 A52/AC-3 Audio (0x81))
0000: 0b 77 16 9d 18 20 43 fe 21 0c a0 f8 60 91 55 70 [.w... C.!...`.Up]
0010: 55 5d f3 aa ea d3 26 54 f9 0d 77 ca 61 43 85 4a [U]....&T..w.aC.J]
0020: 9a a7 b4 9f d1 ae f9 4b f8 4f 69 be 7c f9 f3 e7 [.......K.Oi.|...]
0030: d0 a1 3f 79 ae 3a 98 6f 9d 3d 86 f9 fa 57 d0 d2 [..?y.:.o.=...W..]
0040: d7 4c e5 fc e8 4e 5f 3f 7b 0a bb db f3 97 59 09 [.L...N_?{.....Y.]
[snip 87 bytes]
AudioSampleRate = 0
- Phil
Edit: Fixed URL
Hi Phil,
This is great information. Thanks for the help!
I reviewed the information you provided and got it working!
Thanks again for your help!
Excellent! Glad to help.
- Phil