I'm working with media source extensions to play a video in the browser.
My JavaScript program divides a video file in fragments of 1200 bytes.
The video is encoded in the following codec: video/mp4; codecs="avc1.42E01E, mp4a.40.2".
Giving a time X in seconds I want to find the corresponding first fragment that is needed to play the video at position X (Or a fragment that is near to but not after this position).
More concrete, I want to find out which fragment belongs to the time in the currentTime property of HTMLMediaElement.
The video file can be analysed beforehand.
There is a multitude of ways for the duration->fragment mapping to be specified in the mp4 file.
subsegment index
(sidx
for short) box defined in ISO/IEC 14496-12 associates bytes ranges from the input file with the corresponding durations.Here is a snippet of a sidx box content:
subsegment 0 size 726463 dur 450450
subsegment 1 size 723260 dur 450450
subsegment 2 size 713278 dur 450450
subsegment 3 size 441644 dur 450450
... and so on ...
Finding the fragment for given time X
is trivial.
mp4
files don't have a sidx
box but rather a stsz
and stts
boxes that map a series of samples (video or audio frames) to a duration + size. Here is and example:stsz box:
sample 0 size 105222
sample 1 size 1554
sample 2 size 2153
stts box:
3 sample each with dur 512
The stts
box give the duration for the samples defined in stsz
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With