I've managed to get what I want with the following command (by following Ryan Williams's tip to use HLS):
ffmpeg -i source.mp4 -s 640x360 -hls_list_size 30 -hls_flags delete_segments+append_list+omit_endlist -f hls out.m3u8
Here's why it works:
HTTP Live Streaming is a protocol implemented by Apple. In a nutshell, it's pretty simple - it just lists chunks of video and their duration in a text file, so that player knew which chunk to play next. The player does not need to know beforehand how many chunks there are and which will be next - these were exactly my requirements.
To make this work, I added -f hls
flag to the ffmpeg command. It makes ffmpeg take the input video, split it in chunks, save them and generate a playlist for HLS consumer (ffplay in my case).
But I want to make sure that the playlist is infinite. By default, ffmpeg adds a terminating command to the m3u8 file. To prevent that, and also to make sure that ffmpeg cleanups and does not override the existing playlist, I added these flags: -hls_flags delete_segments+append_list+omit_endlist
.
I also changed the default number of chunks in the playlist to 30 with -hls_list_size 30
flag.
The result is the infinite video that I can play with ffplay out.m3u8
. When ffmpeg
command finishes, I just have to re-run it with the video I want to go next. The player picks everything up automatically.
Of course, I still have to make sure that I have the next video ready when the current video playback comes to an end. Also, I suspect that if I ran ffmpeg
commands in quick succession it could lead to stuttering the video being played - because ffmpeg could clean up chunks that were not played back yet, but ffmpeg considered them old.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…