v4l2grab – Grabbing JPEGs from V4L2 devices

A friend of mine, wanted to use a standard USB WebCam as an IPCam. He used an old Media-Receiver Box (T-Online S100) and installed Gentoo Linux on an CF Card.

Since Kernel 2.6.27 most of the USB webcams are supported through the Linux UVC drivers, which provides an Video4Linux2 Interface to those cams. We found some programs who claimed being able to grab JPEG images from V4L2 Cams, but most of them either didn’t compile, supported only V4L Version 1 or just brought up some other errors.

So I checked up the Video for Linux Two API Specification and found there in Appendix B a small sample of howto communicate with the Cam. I adapted it to our needs and included an converter from YUV colorspace to RGB. Finally I added some code to support the export of JPEG images.

You can download it here: v4l2grab Version 0.1

The whole program is written in C and only needs libjpeg to compile. There are different ways to communicate with the camera, if you want to support all, you should go best with:

gcc v4l2grab.c -o v4l2grab -Wall -ljpeg -DIO_READ -DIO_MMAP -DIO_USERPTR

If you compiled the program, you can get some images with:

./v4l2grab -o image.jpg

With -W width -H height its possible to adjust the size of the image. If the camera doesn’t support the resolution the programs adjusts it automatically and prints an information on stderr.

The current version of V4L2grab can be found on github.

If you like this article, feel free to flattr it:

Apr23