Linux – Take webcam picture from shell with minimal delay

ffmpeglinuxwebcam

I am looking for a command-line tool that can take a picture from a webcam as fast as possible and write it to stdout. In addition, I would like to be able to specify settings like input format, resolution and output format.

My first try was ffmpeg:

ffmpeg -f video4linux2 -video_size 1920x1080 -input_format yuyv422 -i /dev/video0 -f image2 -frames:v 1 -qscale:v 2 pipe:1

However, this has two drawbacks:

  1. It takes about 3 seconds until the image is written to stdout, which seems to be due to the time the webcam needs to initialize.

  2. The picture taken this way is rather dark, probably the webcam needs to capture a view video frames to adjust brightness etc..

My next idea is to continuously capture video frames from the webcam (i.e. "keep the webcam active"), stream these video frames somewhere, and then grab single video frames and convert them to an image on demand. However, I do not know how to do this (and if there is a better way).

EDIT: I need a one-off command that writes the image to stdout, so I can use it in a http server to serve a http GET request. It needs to be quick, because taking the picture is blocking a mechanical process in a machine.

Best Answer

For my webcam, fswebcam takes a picture in less than half a second:

$ time fswebcam test1.jpg
...
real    0m0.491s
user    0m0.024s
sys     0m0.000s

It takes a bit longer to write to stdout and then save it:

$ time fswebcam - > test2.jpg
...
real    0m0.538s
user    0m0.029s
sys     0m0.000s

You can also take images every n seconds (--loop n), if this is what you need (you didn't explain why you want it as fast as possible, and if or how a loop can help).

You can brighten the image, or adjust contrast etc. with the controls shown with fswebcam --list-controls.

Related Question