video – Preserving alpha when re-encoding VP8/VP9 WebM in FFmpeg without specifying input codec

I’m trying to understand why re-encoding a VP8 or VP9 WebM with alpha channel produces black pixels instead of preserving transparency.

I simplified the problem to re-encoding VP8 with alpha to VP9 with alpha:

ffmpeg -i video-vp8-with-alpha.webm -c:v libvpx-vp9 video-vp8-with-alpha-bad.webm

The output loses the alpha channel. ffprobe shows:

Input #0, matroska,webm, from 'video-vp8-with-alpha-bad.webm':
  Metadata:
    ENCODER         : Lavf62.1.103
  Duration: 00:00:03.07, start: 0.017000, bitrate: 152 kb/s
  Stream #0:0: Video: vp9 (Profile 0), yuv420p(tv, bt470bg/bt709/bt709, progressive), 720x1560, SAR 1:1 DAR 6:13, 30 fps, 30 tbr, 1k tbn, start 0.067000
    Metadata:
      ENCODER         : Lavc62.8.101 libvpx-vp9
      ALPHA_MODE      : 1
      DURATION        : 00:00:03.066000000
  Stream #0:1: Audio: opus, 48000 Hz, stereo, fltp, start 0.017000
    Metadata:
      ENCODER         : Lavc62.8.101 libopus
      DURATION        : 00:00:03.049000000

However, if I explicitly force the input codec:

ffmpeg -c:v libvpx -i video-vp8-with-alpha.webm -c:v libvpx-vp9 video-vp8-with-alpha-good.webm

The alpha channel is preserved. ffprobe shows:

Input #0, matroska,webm, from 'video-vp8-with-alpha-good.webm':
  Metadata:
    ENCODER         : Lavf62.1.103
  Duration: 00:00:03.07, start: 0.017000, bitrate: 400 kb/s
  Stream #0:0: Video: vp9 (Profile 0), yuv420p(tv, unknown/bt709/bt709, progressive), 720x1560, SAR 1:1 DAR 6:13, 30 fps, 30 tbr, 1k tbn, start 0.067000
    Metadata:
      ENCODER         : Lavc62.8.101 libvpx-vp9
      ALPHA_MODE      : 1
      DURATION        : 00:00:03.066000000
  Stream #0:1: Audio: opus, 48000 Hz, stereo, fltp, start 0.017000
    Metadata:
      ENCODER         : Lavc62.8.101 libopus
      DURATION        : 00:00:03.049000000

I don’t know in advance whether the input is VP8 or VP9, and I don’t want to run ffprobe before ffmpeg.

How can I make ffmpeg automatically preserve the alpha channel from the input file without explicitly specifying the decoder?

The ffmpeg from commit 0828a3b636;

Build of ffmpeg download from BtbN/FFmpeg-Builds

Read more here: Source link