hasenbanck / matroska-demuxer

A demuxer written in Rust that can demux Matroska and WebM container files.
Apache License 2.0
11 stars 6 forks source link

Add a `duration` field to `Frame` #7

Open koute opened 2 years ago

koute commented 2 years ago

The Frame struct only contains a timestamp without a duration. This is problematic if you'd want to e.g. demux ASS subtitles since the raw subtitles are mangled and muxed into the .mkv without the timestamps.

For example, this is a raw line from an external .ass sub:

Dialogue: 0,0:00:12.36,0:00:12.40,Style,,0,0,0,,Insert text here

And if you'd mux that into an .mkv this is how it's going to look like:

14,0,Style,,0,0,0,,Insert text here

As you can see the timestamps are gone. The idea here is that you're supposed to recover them from the block timestamp + duration, as documented here.

For your reference, the symphonia_format_mkv crate does include the duration when demuxing.

hasenbanck commented 2 years ago

The duration is not a mandatory field in the EBML block (BlockDuration, 0x9B). Simple Blocks don't contain it at all.

So documentation says this about how to extract the duration from the EBML document:

The duration of the Block (based on TimestampScale). The BlockDuration Element can be useful at the end of a Track to define the duration of the last frame (as there is no subsequent Block available), or when there is a break in a track like for subtitle tracks. When not written and with no DefaultDuration, the value is assumed to be the difference between the timestamp of this Block and the timestamp of the next Block in "display" order (not coding order). BlockDuration **MUST** be set if the associated TrackEntry stores a DefaultDuration value.

This would need to be implemented by taking the default duration of the track (already exposed), the "BlockDuration" of a block (not exposed) or the time difference between the current and next frame into account (already exposed).

Not sure if this should be handled by the application or by the library though. PRs are welcome.