问题
I have created fixed length segments for a long MP4 video using Mp4Box. Mp4Box creates a meta info file mv_init.mp4
and segments like mv_1.m4s
, mv_2.m4s
, … I stream the video using HTML5 Media Source Extensions and the streaming is working properly.
The problem is that I am unable to utilize time seeking feature of my HTML5 player. When a user uses the seekbar to seek to another time point, I need to fetch the correct segment file (mv_{number}.m4s
) for that currentTime
.
For example:
- video duration: 2 hours
- segment size: 10 seconds
- user seeks to time: 25 minutes
25 minutes = 25 × 60 seconds = 1500 seconds. As each segment is of 10 seconds, I need segment number 1500 / 10 = 150. The needed segment file is mv_150.m4s
.
The calculation apparently seems correct, but the HTML5 player then downloads many more files after mv_150.m4s
before it continues with the streaming.
How to correctly calculate segment file number, so that after seeking, the streaming runs smoothly, without downloading any extra files?
To create the segments of the MP4 video, I used the following command:
MP4Box -dash 10000 -out video.mpd -dash-profile live -segment-name mv_ -rap video.mp4
回答1:
I have researched on the matter and found the real cause. The caluclation for loading the segments was correct. The issue was with the key frame interval in video file. A keyframe is a frame in video from which the video can subsequently load and run from that point. So in my case I need to insert keyframe at the start of each segment. Therefore when we seek through the video in different time positions the next segment that is loaded contains a keyframe at the start of it.
The keyframes in video file can be setup using FFMPEG. So for example if we have a video having segments of 5 seconds then we must create keyframe at 5 seconds interval using ffmpeg. Another important point while setting up keyframes is to look into FRAME RATE of video. The video must have a fixed frame rate so we can precisely calculate the position of keyframe.
Example:
Video File: gladiator.mp4
Segment Size: 5 seconds
No we set FRAME RATE and KEY FRAME INTERVAL USING FFMPEG
ffmpeg -i gladiator.mp4 -x264-params keyint=120:min-keyint=120:no-scenecut=1 -r 24 gladiator-output.mp4
keyint=120 i.e; 24 fps * 5 seconds = 120
And now we create the segment files using Mp4Box
MP4Box -dash 5000 -frag 5000 -out gladiator.mpd -dash-profile on-demand -segment-name mv_ gladiator-output.mp4
So it will create segments like mv_1,mv_2, .. and so on having keyframes at the start of each segment file.
Seekable Dash Streaming Example
来源:https://stackoverflow.com/questions/36112002/calculate-m4s-segment-file-suffix-in-html5-video-streaming-when-user-seeks-to-a