ffmpeg capture current frame and overwrite the ima

2020-06-03 02:18发布

问题:

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.

my below code works but it outputs to multiple image jpg files: 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?

回答1:

Following command line should work for you.

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


回答2:

To elaborate a bit on the already accepted answer from pragnesh,

FFmpeg

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

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

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

  • 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

The image2 format muxer expects a filename pattern, such as img%01d.jpg to produce a sequentially numbered series of files. If the update option is set to 1, the filename will be interpreted as just a filename, not a pattern, thereby overwriting the same file.

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

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

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.

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

AVconv

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 -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:

-an : Disables audio recording.

  • Found in Audio Options Section

-r < fps > : sets frame rate

  • Found in the Video Options Section
  • used as an output option is actually a a substitute for the fps filter

leading to an alternate version :

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

Hope it helps understand for possible further tweaking ;)



回答3:

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.

ffmpeg -i http://xx:yy@192.168.1.xx:yyy/snapshot.cgi /tmp/Capture2.jpg -y



回答4:

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

#!/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

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



标签: bash ffmpeg