DTS says when to decode a packet; PTS says when to play or display it. The ZIP’s av.mp4 contains a test pattern and a tone.
dotnet add package RecursiveAI.OneMediaRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneMedia; using var file = Media.Open("av.mp4"); foreach (var track in file.Tracks) { using var p = file.Packets(track).First(); Console.WriteLine(track.Kind); Console.WriteLine(p.Info.Dts); Console.WriteLine(p.Info.Pts); }
The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.
The snippet prints the first packet’s track kind, DTS and PTS on separate lines:
Video 0/10240 4096/10240 Audio 0/48000 0/48000
The full program
using RecursiveAI.OneMedia; using var file = Media.Open("av.mp4"); var reader = file.Demuxer!; var counts = new int[file.Tracks.Count]; while (reader.TryReadPacket(out var packet)) { var info = packet.Info; var index = info.TrackIndex; if (counts[index]++ >= 3) continue; var kind = file.Tracks[index].Kind; Console.WriteLine($"{kind}: DTS {info.Dts}, PTS {info.Pts}"); }
Video: DTS 0/10240, PTS 4096/10240 Video: DTS 2048/10240, PTS 10240/10240 Audio: DTS 0/48000, PTS 0/48000 Video: DTS 4096/10240, PTS 6144/10240 Audio: DTS 1024/48000, PTS 1024/48000 Audio: DTS 2048/48000, PTS 2048/48000
The full program reads both tracks in one pass and prints the first three packets of each. A timestamp the reader can’t determine prints as unknown, not zero. The snippet at the top assumes each track has a packet; the loop in the full program also works on empty tracks.
Variations
To report DTS in seconds, replace the Console.WriteLine(p.Info.Dts); line at the top with this. The cast prevents integer division.
var dts = p.Info.Dts; if (dts.IsKnown) Console.WriteLine((double)dts.Ticks / dts.Timescale);
Related recipes
The short answer
Read PacketInfo.Dts for decode time and PacketInfo.Pts for presentation time. Open the file with Media.Open, then use file.Packets(track) for a single track or the synchronous demuxer for an interleaved pass. Neither path decodes the audio or video. Packets gives you owned packets; dispose each one. TryReadPacket gives you a view whose payload is valid only until the next call on that reader.
What is the difference between PTS and DTS?
DTS is when a decoder needs the compressed data. PTS is when the decoded result belongs on the playback timeline. A video frame may refer to a picture that appears later on screen, so that reference picture has to arrive first. In the output, the second video packet has a PTS of 10240/10240, but the next packet has a PTS of 6144/10240. The PTS goes backward while the DTS goes forward. Don’t sort the packets by PTS before sending them to a muxer that expects decode order.
Those audio packets, by contrast, have matching DTS and PTS. The packet contract describes DTS as equal to PTS when there’s no reordering and unknown when it isn’t stated. An unknown DTS isn’t evidence that the packet should be decoded at the start.
How are timestamps written in MP4?
For a regular MP4 track, the stts table gives durations in decode order. Add those durations to get decode times on the media timeline. The ctts table gives the offset between decode and composition times. An edit list can map the media timeline to a different place in the movie, for example to account for audio priming. See how OneMedia writes time, edits and priming.
OneMedia prints MediaTime as ticks/timescale. The two tracks don’t have to share a clock: 2048/10240 is 0.2 seconds, while 2048/48000 is about 0.043 seconds. Compare the whole time, not the bare tick counts. For remuxing, keep the original MediaTime values instead of rounding them to decimal seconds for display: the muxer handles rescaling and reports rounding.