bash ffmpeg 捕获当前帧并覆盖图像输出文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25360470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:38:15  来源:igfitidea点击:

ffmpeg capture current frame and overwrite the image output file

bashffmpeg

提问by Xianlin

I am trying to extract the image file from a RTSP stream url every second (could be every 1 min also) and overwrite this image file.

我正在尝试每秒从 RTSP 流 url 中提取图像文件(也可能是每 1 分钟一次)并覆盖此图像文件。

my below code works but it outputs to multiple image jpg files: img1.jpg, img2.jpg, img3.jpg...

我下面的代码有效,但它输出到多个图像 jpg 文件: img1.jpg, img2.jpg, img3.jpg...

ffmpeg -i rtsp://IP_ADDRESS/live.sdp -f image2 -r 1 img%01d.jpg

How to use ffmpeg or perhaps bash scripts in Linux to overwrite the same image file while continuously extract the image at a NOT high frequecy, say 1 min or 10 sec?

如何在 Linux 中使用 ffmpeg 或 bash 脚本覆盖相同的图像文件,同时以不高的频率连续提取图像,比如 1 分钟或 10 秒?

回答by pragnesh

Following command line should work for you.

以下命令行应该适合您。

ffmpeg -i rtsp://IP_ADDRESS/live.sdp -f image2 -updatefirst 1 img.jpg

回答by Pau Coma Ramirez

To elaborate a bit on the already accepted answerfrom pragnesh,

要在已经详细一点接受的答案pragnesh

FFmpeg

FFmpeg

As stated in the ffmpeg documentation: ffmpeg command line options are specified as

ffmpeg 文档中所述:ffmpeg 命令行选项指定为

ffmpeg [global_options] {[input_options] -i input_file} ... {[output_options] output_file} ...

ffmpeg [global_options] {[input_options] -i input_file} ... {[output_options] output_file} ...

So

所以

ffmpeg -i rtsp://<rtsp_source_addr> -f image2 -update 1 img.jpg

ffmpeg -i rtsp://<rtsp_source_addr> -f image2 -update 1 img.jpg

Uses output option-f image2 , force output format to image2 format, as part of the muxer stage.

使用输出选项-f image2 ,强制输出格式为 image2 格式,作为复用器阶段的一部分。

  • Note that in ffmpeg, if the output file name specifies an image format the image2 muxer will be used by default, so the command could be shortened to:

    ffmpeg -i rtsp://<rtsp_source_addr> -update 1 img.jpg

  • 请注意,在 ffmpeg 中,如果输出文件名指定了图像格式,则默认情况下将使用 image2 muxer,因此该命令可以缩短为:

    ffmpeg -i rtsp://<rtsp_source_addr> -update 1 img.jpg

The image2 format muxerexpects a filename pattern, such as img%01d.jpgto produce a sequentially numbered series of files. If the updateoptionis set to 1, the filename will be interpreted as just a filename, not a pattern, thereby overwriting the same file.

图像2格式复用器期望的文件名模式,例如img%01d.jpg,以产生按顺序编号的一系列文件。如果更新选项设置为 1,文件名将被解释为只是一个文件名,而不是一个模式,从而覆盖同一个文件。

Using the -r, set frame rate, video optionworks, but generated me a whole lot of dropping frame messages which was bugging me.

使用-r,设置帧速率,视频选项有效,但给我带来了很多丢帧消息,这让我很烦恼。

Thanks to another answer on the same topic, I found the fps Video Filterto do a better job.

感谢同一主题的另一个答案,我发现fps 视频过滤器做得更好。

So my version of the working command is

所以我的工作命令版本是

ffmpeg -i rtsp://<rtsp_source_addr> -vf fps=fps=1/20 -update 1 img.jpg

For some reason still unkown to me the minimum framerate I can achieve from my feed is 1/20 or 0.05.

出于某种原因,我仍然不知道我可以从我的提要中获得的最低帧率是 1/20 或 0.05。

There also exists the video filter thumbnail, which selects an image from a series of frames but this is more processing intensive and therefore I would not recommend it.

还有视频过滤器缩略图,它从一系列帧中选择一个图像,但这需要更多的处理,因此我不推荐它。

Most of this and more I found on the FFMpeg Online Documentation

我在FFMpeg 在线文档中找到的大部分内容和更多内容

AVconv

AV转换

For those of you who use avconv it is very similar. They are after all forks of what was once a common library. The AVconv image2 documentation is found here.

对于那些使用 avconv 的人来说,它非常相似。毕竟,它们是曾经是公共图书馆的分支。该AVconv图像2文档可以在这里找到

avconv -i rtsp://<rtsp_source_addr> -vf fps=fps=1/20 -update 1 img.jpg

avconv -i rtsp://<rtsp_source_addr> -vf fps=fps=1/20 -update 1 img.jpg

As Xianlin pointed out there may be a couple other interesting options to use:

正如 Xianlin 指出的那样,可能还有其他一些有趣的选项可供使用:

-an: Disables audio recording.

-an:禁用录音。

-r< fps >: sets frame rate

-r < fps >: 设置帧率

leading to an alternate version :

导致替代版本:

avconv -i rtsp://<rtsp_source_addr> -r 1/20 -an -update 1 img.jpg

avconv -i rtsp://<rtsp_source_addr> -r 1/20 -an -update 1 img.jpg

Hope it helps understand for possible further tweaking ;)

希望它有助于理解可能的进一步调整;)

回答by bleekerk

I couldn't get the option -update working to overwrite the .jpg. Doing some experiments resulted in a working solution (at least for me) with the option -y at the end (upper-case is not working). I also needed http:// instead of rstp:// for this camera.

我无法使用选项 -update 来覆盖 .jpg。做一些实验得到了一个可行的解决方案(至少对我来说),最后带有 -y 选项(大写不起作用)。对于这台相机,我还需要 http:// 而不是 rstp://。

ffmpeg -i http://xx:[email protected]:yyy/snapshot.cgi /tmp/Capture2.jpg -y

ffmpeg -i http://xx:[email protected]:yyy/snapshot.cgi /tmp/Capture2.jpg -y

回答by Kokkie

Grab a snapshot from an RTSP video stream every 10 seconds.

每 10 秒从 RTSP 视频流中获取快照。

#!/bin/bash
#fetch-snapshots.sh
url='rtsp://IP_ADDRESS/live.sdp'
avconv -i $url -r 0.1 -vsync 1 -qscale 1 -f image2 images%09d.jpg

-r rate set frame rate to 0.1 frames a second (this equals to 1 frame every 10 seconds). Thanks to westonruter, see https://gist.github.com/westonruter/4508842

-r rate 将帧速率设置为每秒 0.1 帧(这等于每 10 秒 1 帧)。感谢 westonruter,参见https://gist.github.com/westonruter/4508842

Furthermore have a look at FFMPEG: Extracting 20 images from a video of variable length

此外看看FFMPEG:从可变长度的视频中提取20张图像