Reputation: 633
My task is to generate (by piping, so that a file can be played at the same time with generation) an mp4 file which is a part of a larger file, with the result looking like a static file link, being seekable before it fully loads (i.e. supporting range headers).
Here is how I do it now:
ffmpeg -ss $1 -i teststream_hls1_replay.mp4 -t $2 -timecode $3 \
-codec copy -movflags frag_keyframe+faststart -f mp4 pipe:1
Result is OK (video starts from the right point) except the player does not see the total duration of a file so a controlbar looks weird, and seeking isn't possible properly, just because the controlbar jumps all the time.
How do I indicate to ffmpeg that it has to set moov atom to contain right duration?
Basically the question boils down to: how do I force set some arbitrary duration of file in a moov atom, when I am generating a fragmented mp4? ffmpeg will not get know how long will it be, so explainably it can't do it itself, but I know... is there a command line parameters to specify a 'forced duration'?
Upvotes: 1
Views: 1023
Reputation: 1507
Even if it's possible to set moov atom to contain the right duration, the player still won't be able to seek properly. Due to the nature of piping, the player needs to process the video sequentially. How many seconds the video can be sought forward or backward depends on how much the video data the player is caching.
For example, in mpv you can set forward cache and backward cache to 4 MiB and 2 MiB, respectively:
ffmpeg [..] | mpv - --cache=yes --demuxer-max-bytes=4MiB --demuxer-max-back-bytes=2MiB
If the video's bitrate is 100 KiB/s, you can't seek forward and backward more than 40s and 20s, respectively, from the current timestamp.
For some players it might be more desirable to set the duration to 0 by setting -movflags empty_moov
. In your case:
ffmpeg -ss $1 -i /root/nextpro-livestream/replay/teststream_hls1_replay.mp4 -t $2 -timecode $3 -codec copy -movflags frag_keyframe+empty_moov -f mp4 pipe:1
That way, the player's control bar won't jump all the time, so users will be able to seek more properly. But still, the seek amount is limited by the player's cache.
If you really want the users to be able to seek to any timestamp, you need to change the protocol from pipe
to file
or http
or other protocols supported by ffmpeg. You might not need to generate a fragmented mp4 video (-movflags frag_keyframe
), but you might still need to set -movflags +faststart
.
Upvotes: 1