ffmpeg open webcam using YUYV but i want MJPEG

14

7

I need ffmpeg to open webcam (logitech c910) in MJPEG mode, because the webcam can give ~24 using MJPEG "protocol" and only ~10 fps using the YUYV. Can i choose between them using ffmpeg command line?

xx@(none) ~ $ v4l2-ctl --list-formats
ioctl: VIDIOC_ENUM_FMT
    Index       : 0
    Type        : Video Capture
    Pixel Format: 'YUYV'
    Name        : YUV 4:2:2 (YUYV)

    Index       : 1
    Type        : Video Capture
    Pixel Format: 'MJPG' (compressed)
    Name        : MJPEG

My current command line:

ffmpeg -y -f alsa -i hw:3,0 -f video4linux2 -r 20 -s 1280x720 -i /dev/video0 -acodec libfaac -ab 128k -vcodec libx264 /tmp/web.avi

ffmpeg produces corrupted h264 stream when i record from webcam, but normal h264 strem when i record from x11grab. Another codecs (mjpeg, mpeg4) works well with webcam... But this is another story.

update Full ffmpeg's console output: http://pastebin.com/Hzem6CKF (you can see it opens video device in YUV mode, but the device can provide MJPEG outpud also).

pavelkolodin

Posted 2012-10-28T10:19:53.253

Reputation: 326

Answers

13

You can list additional information about what your webcam can output with v4l2-ctl --list-formats-ext. You can also show webcam information with ffmpeg using the -list_formats input option:

$ ffmpeg -f video4linux2 -list_formats all -i /dev/video0
[...]
[video4linux2,v4l2 @ 0x1fb7660] Raw       :   yuyv422 :     YUV 4:2:2 (YUYV) : 640x480 160x120 176x144 320x176 320x240 352x288 432x240 544x288 640x360
[video4linux2,v4l2 @ 0x1fb7660] Compressed:     mjpeg :                MJPEG : 640x480 160x120 176x144 320x176 320x240 352x288 432x240 544x288 640x360

This webcam from my example can support both raw (yuyv422) and compressed (mjpeg) formats, and you can tell ffmpeg which one you want with the -input_format input option.

Examples

Stream copy the MJPEG video stream (no re-encoding):

ffmpeg -f v4l2 -input_format mjpeg -i /dev/video0 -c:v copy output.mkv

Re-encode the raw webcam video to H.264:

ffmpeg -f v4l2 -input_format yuyv422 -i /dev/video0 -c:v libx264 -vf format=yuv420p output.mp4

Same as above but manually choose frame rate and video size (v4l2-ctl --list-formats-ext for available frame rate and video sizes):

ffmpeg -f v4l2 -input_format yuyv422 -framerate 30 -video_size 640x480 -i /dev/video0 -c:v libx264 -vf format=yuv420p output.mp4
  • See the video4linux2 input device documentation for more options.

  • If the frame rate being output is lower than expected then add more light: the webcam may be lowering the frame rate to get longer exposures in a dim environment.

llogan

Posted 2012-10-28T10:19:53.253

Reputation: 31 929

0

ffmpeg -t 60 -f video4linux2 -input_format mjpeg -i /dev/video0 -c:v libx264 -strict -2 /DataVolume/share/Public/v/outputs.mp4

somewood

Posted 2012-10-28T10:19:53.253

Reputation: 1

4Welcome to Super User! Can you expand on this? In particular, what information does it have that LordNekbeard's doesn't? :) – bertieb – 2018-04-16T12:00:48.520

No need for -strict -2 unless: 1) your ffmpeg is horribly outdated, and 2) you are encoding AAC audio. – llogan – 2018-04-16T18:06:48.133