Search code examples
videoffmpegvideo-encodinglibavcodeclibav

FFMPEG - Can't create a video from series of images with RGB24 pixel format


Goal


I'm writing a small proof-of-concept application to take some raw image data I acquire from a digital camera (a series of RGB24 images), and combine them together into a simple, no-audio, video file.


Work So Far

The initialization code is as follows:


AVCodec* pCodec = NULL;
AVCodecContext* pCodecContext = NULL;
AVFrame* pFrame = NULL;

/* Register all available codecs. */
avcodec_register_all();

/* Determine if desired video encoder is installed. */
pCodec = avcodec_find_encoder(CODEC_ID_MJPEG);
if (!pCodec) {
    printf("Codec not installed!\n");
}

pCodecContext = avcodec_alloc_context3(pCodec);
pFrame = avcodec_alloc_frame();

Very cut-and-dry. However, when I try to register the codec, it fails with my custom error message, and one dropped in a nice color-coded format directly from the FFMPEG libraries:


[mjpeg @ 0x650d00] Specified pixel format rgb24 is invalid or not supported
Codec not available.

I can confirm that that pixel format is valid easily enough:


Pixel formats:
I.... = Supported Input  format for conversion
.O... = Supported Output format for conversion
..H.. = Hardware accelerated format
...P. = Paletted format
....B = Bitstream format

2>&1 ffmpeg -pix_fmts | grep -i -e rgb24 -e flags
FLAGS NAME            NB_COMPONENTS BITS_PER_PIXEL
IO... rgb24                  3            24

I can also confirm that the video codec I'm trying to use is valid for encoding.


Codecs:
 D..... = Decoding supported
 .E.... = Encoding supported
 ..V... = Video codec
 ..A... = Audio codec
 ..S... = Subtitle codec
 ...I.. = Intra frame-only codec
 ....L. = Lossy compression
 .....S = Lossless compression

2>&1 ffmpeg -codecs | grep -i mjpeg              
 DEVIL. mjpeg                Motion JPEG

Question


Why is this pixel format not supported? It seems like such a common one when working with other utilities like MATLAB, OpenCV, FreeImage, etc. Is there any set of options or functions in FFMPEG/AVcodec that can resolve this issue? I'd like to avoid having to to manually convert my image to a different color-space if possible, so I'm not burning up CPU cycles by first converting the RGB24 image to a new format, THEN encoding a video frame with it.

Thank you.


Solution

  • If you run ffmpeg -h encoder=mjpeg, you will see

    ...
    Encoder mjpeg [MJPEG (Motion JPEG)]:
        General capabilities: threads
        Threading capabilities: frame and slice
        Supported pixel formats: yuvj420p yuvj422p yuvj444p
    

    JPEGs work with pixels encoded as YUV. qtrle, png or libx264rgb, among others, will accept RGB, but should be saved as .mov - Quicktime.