FFmpeg change output to specific pixel format?

R.martinez picture R.martinez · Dec 16, 2018 · Viewed 17.5k times · Source

I am working on an openCV project, I have a stable running variant that is taking input from an HDMI capture card and using FFmpeg to output to a v4L2 loopback device (/dev/video0) my openCV project takes its input from /dev/video0.

The issue comes when I try to use an rtsp feed, the following command works to send the feed to my loopback device:

ffmpeg -rtsp_transport tcp -i rtsp://@192.168.1.27:552//stream1 -acodec rawvideo -vcodec rawvideo -f v4l2 /dev/video0

And I am able to view that feed with VLC (on /dev/video0) no problem, however when I feed it to my openCV app, I get the following error:

VIDEOIO ERROR: V4L2: Pixel format of incoming image is unsupported by OpenCV

When I run v4l2-ctl -d /dev/video0 --all on both working and non working variants this is what I get:

Working output

Format Video Output:
Width/Height      : 1920/1080
Pixel Format      : 'UYVY'
Field             : None
Bytes per Line    : 3840
Size Image        : 4147200
Colorspace        : sRGB
Transfer Function : Default
YCbCr Encoding    : Default
Quantization      : Default
Flags             : 

Nonfunctional output

Format Video Output:
Width/Height      : 1280/720
Pixel Format      : 'YU12'
Field             : None
Bytes per Line    : 1280
Size Image        : 1382400
Colorspace        : sRGB
Transfer Function : Default
YCbCr Encoding    : Default
Quantization      : Default
Flags             : 

So I am concluding that the pixel format 'YU12' is not compatible with openCV while format 'UYVY' is. If it's possible, how do I set the output of FFmpeg to be in pixel format UYVY when the input is YU12?

Answer

user9258013 picture user9258013 · Jan 15, 2019

Use the format filter or the -pix_fmt option. For example we'll use yuv420p pixel format!

# Using the format filter (yuv420p)
ffmpeg -i in_file -filter:v "format=yuv420p" out_file

# Using the 'pix_fmt' option
ffmpeg -i in_file -pix_fmt yuv420p out_file

[Bonus] there are plenty of pixel formats available to get a list of them run ffmpeg -pix_fmts.