14

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?

Xianlin
  • 1,139
  • 5
  • 20
  • 34
  • there is an "updatefirst" option for image2, IIRC, just remember that if you're on windows, it will be overwriting files as you try to read them, which may cause a conflict/collision – rogerdpack Aug 18 '14 at 20:53

5 Answers5

20

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.

-r < fps > : sets frame rate

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

Community
  • 1
  • 1
Pau Coma Ramirez
  • 4,261
  • 1
  • 20
  • 19
  • to use avconv instead of ffmpeg, `avconv -i rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov -r 1/20 -an -y -update 1 test.jpg` – Xianlin Jun 26 '15 at 15:36
  • According to the **-r** [Video Options docs](https://libav.org/avconv.html#Video-Options), **-r** option actually causes the fps filter to be inserted. so **-vf fps=fps=1/20** should also do the same. But it is shorter with -r :) Also I don't think you need the **-y** *( Overwrite output files without asking)* option as the **-update 1** already knows it should overwrite. **-an** addition is an interesting one, which may reduce process guessing. Thanks for the comment I'll update the answer with avconv option. – Pau Coma Ramirez Jul 03 '15 at 09:57
16

Following command line should work for you.

ffmpeg -i rtsp://IP_ADDRESS/live.sdp -f image2 -updatefirst 1 img.jpg
pragnesh
  • 1,240
  • 7
  • 18
2

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

Andronicus
  • 25,419
  • 17
  • 47
  • 88
bleekerk
  • 21
  • 1
0

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

Community
  • 1
  • 1
Kokkie
  • 546
  • 6
  • 15
  • 1
    so I will have 6 images within 60 seconds, how about I only have ONLY one image overwritten 6 times within 60 seconds? That's what I need. – Xianlin Aug 19 '14 at 09:12
  • Have you tried the option ‘-y (global)’ Overwrite output files without asking. – Kokkie Aug 19 '14 at 10:42
0

ffmpeg -i rtsp://root:password@192.168.1.1/mpeg4 -ss 00:00:01 -f image2 -vframes 1 thumb.jpg

  • replace with your rtsp protocol url
  • make sure 00:00:01
  • if you put other numbers, the image will be crashed