ffmpeg - Continuously stream webcam to single .jpg file (overwrite)

Germanunkol picture Germanunkol · Jul 13, 2013 · Viewed 16.4k times · Source

I have installed ffmpeg and mjpeg-streamer. The latter reads a .jpg file from /tmp/stream and outputs it via http onto a website, so I can stream whatever is in that folder through a web browser.

I wrote a bash script that continuously captures a frame from the webcam and puts it in /tmp/stream:

while true
do
    ffmpeg -f video4linux2 -i /dev/v4l/by-id/usb-Microsoft_Microsoft_LifeCam_VX-5000-video-index0 -vframes 1 /tmp/stream/pic.jpg
done

This works great, but is very slow (~1 fps). In the hopes of speeding it up, I want to use a single ffmpeg command which continuously updates the .jpg at, let's say 10 fps. What I tried was the following:

ffmpeg -f video4linux2 -r 10 -i /dev/v4l/by-id/usb-Microsoft_Microsoft_LifeCam_VX-5000-video-index0 /tmp/stream/pic.jpg

However this - understandably - results in the error message:

[image2 @ 0x1f6c0c0] Could not get frame filename number 2 from pattern '/tmp/stream/pic.jpg'
av_interleaved_write_frame(): Input/output error

...because the output pattern is bad for a continuous stream of images.

Is it possible to stream to just one jpg with ffmpeg?

Thanks...

Answer

llogan picture llogan · Jul 14, 2013

You can use the -update option:

ffmpeg -y -f v4l2 -i /dev/video0 -update 1 -r 1 output.jpg

From the image2 file muxer documentation:

-update number

If number is nonzero, the filename will always be interpreted as just a
filename, not a pattern, and this file will be continuously overwritten
with new images.