e42.uk Circle Device

 

Quick Reference

ffmpeg v4l2 playback and record

ffmpeg v4l2 playback and record

You want to record some video from your webcam and see the video at the same time on your X desktop system? You also want to use MJPEG for some crazy reason. Well, here we go...

ffmpeg -f v4l2 \
   -input_format mjpeg -video_size 1280x720 -framerate 25 \
   -i /dev/video0 \
   -c:v copy output.mkv \
   -c:v rawvideo -pix_fmt yuyv422 \
   -f xv -

Simple! Well, let's see what is going on here:

Input from the v4l2 driver configured with a resolution of 1280x720 and a framerate of 25 fps using device /dev/video0 it is important that the -i switch and argument come after the configuration arguments! Then we write that to the output.mkv file using the stream copy codec (we do not want to re-encode this data but rather take it raw from the webcam over the USB bus). Then we encode the mjpeg stream to raw video (rawvideo is the codec and it might seem more accurate to say we are DECODING the video now. We are doing this so the input to the next output module (the xv module) is compatible, the desired format is provided in pix_fmt, in this case yuyv422, others include: yuv420p, uyvy422, yuyv422.

Encoding Sound with your Video

You should be able to do this with FFmpeg by adding the following to your command line (at the start)

-f alsa -i default

That does not work properly for me though :-(. As a result I have to record audio using arecord and then merge them later.

To record audio with arecord and then merge the video is something of a pain in the bottom but with a little patience it can be done with some reasonable results.

arecord -f cd > output.wav
ffmpeg -f v4l2 \
   -input_format mjpeg -video_size 1280x720 -framerate 30 \
   -i /dev/video0 \
   -c:v copy output.mkv \
   -c:v rawvideo -pix_fmt yuv420p -f xv -
ffmpeg -itsoffset -1.5 -i output.wav -i output.mkv \
   -c:a copy -c:v copy -map 0:0 -map 1:0 output_.mkv

The -itsoffset argument should come before the file you want to modify. In this example I want to get rid of 1.5 seconds worth of audio from the output.wav file. Lots of this stuff would be much easier in some kind of video editing software like PiTiVi but who wants easy?

Misc

Want to see some information about your device?

v4l2-ctl --device /dev/video0 --all

List supported pixel formats:

v4l2-ctl --list-formats

List resolutions (gives lots of detail!):

v4l2-ctl --list-formats-ext

References

Quick Links: Techie Stuff | General | Personal | Quick Reference