ffmpeg open webcam using YUYV but i want MJPEG

Solution 1:

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.