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

9 Responses to “v4l2grab – Grabbing JPEGs from V4L2 devices”
[...] few weeks ago, I published my v4l2grab program to grab JPEGs from V4l2 [...]
I’m trying to use a webcam in V4L2_PIX_FMT_MJPEG format (not V4L2_PIX_FMT_YUYV) and grab image into jpeg files, some ideas on how do it?
Hoi,
Great work! It would be a good idea to add the compile statement to the readme file on github.
Works like a charm!
Groeten,
Frank.
I just built this for the DM3730 Logic SOM running Linux built from the 2.6.32 OMAP PSP_03.00.01.06 kernel from TI. I used libjpeg62-dev. Worked right out of the box. Great work.
Thanks,
Jemiah
.
I was running v4l2grab on ARM with logitech C200.
Even set the format by using VIDIOC_S_FMT, I still get 176×144 size of image.
I saw the notes in source as follow:
‘Note VIDIOC_S_FMT may change width and height.’
Could you please share the more detail about this?
Hi,
I’m trying to compile v4l2grab for an ARM processor and can see that some of you have already succeded in that. Could you please give me a step by step guide on how to accomplish this task? Thanks in advanced
/Anders
@Anders
On v5l ARM machine I have no compilation issue:
gcc v4l2grab.c -o v4l2grab -Wall -ljpeg -DIO_READ -DIO_MMAP -DIO_USERPTR
This is a very useful little utility, nice code, but has a stutter.
I’ve added some debugging text
static void imageProcess(const void* p) { unsigned char* src = (unsigned char*)p; unsigned char* dst = malloc(width*height*3*sizeof(char)); // convert from YUV422 to RGB888 printf("Munge\n"); fflush(stdout); YUV422toRGB888(width,height,src,dst); // write jpeg printf("Write\n"); fflush(stdout); jpegWrite(dst); // free temporary image free(dst); }Should it not read a frame once, process it once, and write t once ?
Thanks,
Jon
The program is reading the frame again, when the read returned an EAGAIN error.