Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

video - Change ffmpeg input on the fly

I want to show videos non-stop without knowing beforehand which videofile would go next on a Linux host. This should work locally, without any networking. I'm going to playback videos with ffplay. When one video is finished, the next one should be played seamlessly, without any delay. I tried to append to a file and just play it with ffplay like this:

ffmpeg -re -i source.mp4 -f mpegts - >> video.ts
# In another console
ffplay video.ts

But that didn't work - once ffmpeg is done with the file, ffplay stops playing. If I run the same ffmpeg command again, ffplay plays the video from the start with ugly artifacts.

What I want to achieve is:

  1. Run ffplay and forget about it
  2. Run ffmpeg on a video file. ffplay should automatically start playing it
  3. Run ffmpeg on a video file. ffplay should automatically and seamlessly start playing it, but only after it finished with the first file
  4. Run ffmpeg on another file... I hope you get the idea

I'm really new to ffmpeg, so I'm sorry if I'm asking for something obvious or impossible. I hope you'll be able to get me into the right direction.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...