Search code examples
c++videoffmpegalphawebm

How to read alpha channel from .webm video using ffmpeg in c++


Background

I have a .webm file (pix_fmt: yuva420p) converted from .mov video file in order to reduce file size and I would like to read the video data using c++, so I followed using this repo as a reference.

This works perfectly on .mov video.

Problem

By using same repo, however, there is no alpha channel data (pure zeros on that channel) for .webm video but I can get the alpha data from .mov video.

Apparently many people already noticed that after the video conversion, ffmpeg somehow detect video as yuv420p + alpha_mode : 1 and thus alpha channel is not used but there is no one discuss about workaround of this.

I tried forcing pixel format during this part to use yuva420p but that just broke the whole program.

  // Set up sws scaler
    if (!sws_scaler_ctx) {
        auto source_pix_fmt = correct_for_deprecated_pixel_format(av_codec_ctx->pix_fmt);
        sws_scaler_ctx = sws_getContext(width, height, source_pix_fmt,
                                        width, height, AV_PIX_FMT_RGB0,
                                        SWS_BILINEAR, NULL, NULL, NULL);
    }

I also verified my video that it contains alpha channel using other source so I am sure there is alpha channel in my webm video but I cannot fetch the data using ffmpeg.

Is there a way to fetch the alpha data? Other video format or using other libraries work as well as long as it does have some file compression but I need to access the data in c++.

Note: This is the code I used for converting video to webm

ffmpeg -i input.mov -c:v libvpx-vp9 -pix_fmt yuva420p output.webm

Solution

  • You have to force the decoder.

    Set the following before avformat_open_input()

    AVCodec *vcodec;
    vcodec = avcodec_find_decoder_by_name("libvpx-vp9");
    av_fmt_ctx->video_codec = vcodec;
    av_fmt_ctx->video_codec_id = vcodec->id;
    

    You don't need to set pixel format or any scaler args.

    This assumes that your libavcodec is linked with libvpx.