即时更改ffmpeg输入 [英] Change ffmpeg input on the fly

查看:97
本文介绍了即时更改ffmpeg输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想不间断地显示视频,而无需事先知道在Linux主机上接下来要播放哪个视频文件.这应该在本地工作,无需任何网络.我要用ffplay播放视频.播放完一部视频后,应立即播放下一部视频,不要有任何延迟.我试图附加到文件中,然后像这样用ffplay播放它:

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

但是那没用-ffmpeg文件完成后,ffplay停止播放.如果我再次运行相同的ffmpeg命令,则ffplay会从头开始播放带有丑陋伪像的视频.

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.

我想要实现的是:

  1. 运行ffplay,然后忘记它
  2. 在视频文件上运行ffmpeg. ffplay应该会自动开始播放
  3. 在视频文件上运行ffmpeg. ffplay应该自动无缝地开始播放,但只有在第一个文件结束后才开始播放
  4. 在另一个文件上运行ffmpeg ...希望您能理解

我对ffmpeg真的很陌生,所以如果我要求显而易见的或不可能的事情,我感到抱歉.希望您能够引导我朝正确的方向前进.

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.

推荐答案

通过以下命令,我设法获得了想要的东西(通过遵循

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

这是它起作用的原因:

HTTP实时流是Apple实施的协议.简而言之,这非常简单-它仅在文本文件中列出视频块及其持续时间,以便播放器知道接下来要播放哪个块.玩家不需要事先知道有多少块,接下来是什么-这些正是我的要求.

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.

为使此工作有效,我在ffmpeg命令中添加了-f hls标志.它使ffmpeg接受输入的视频,将其分割成块,保存并为HLS使用者生成播放列表(在我的情况下为ffplay).

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).

但是我要确保播放列表是无限的.默认情况下,ffmpeg将终止命令添加到m3u8文件.为了防止这种情况,并确保ffmpeg清理并且不覆盖现有的播放列表,我添加了以下标志:-hls_flags delete_segments+append_list+omit_endlist.

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.

我还使用-hls_list_size 30标志将播放列表中的默认块数更改为30.

I also changed the default number of chunks in the playlist to 30 with -hls_list_size 30 flag.

结果是我可以使用ffplay out.m3u8播放的无限视频.当ffmpeg命令完成时,我只需要使用我接下来要播放的视频重新运行它即可.播放器会自动拾取所有内容.

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.

当然,当当前视频播放结束时,我仍然必须确保准备好下一个视频.另外,我怀疑如果我快速连续运行ffmpeg命令,可能会导致播放的视频停顿-因为ffmpeg可以清理尚未播放的块,但ffmpeg认为它们很旧.

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.

这篇关于即时更改ffmpeg输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆