11

I'm trying to convert a simple WEBM video with transparency to HEVC with alpha. I couldn't find any resources about how to convert without losing the alpha channel.

I don't know if it's possible with FFMPEG:

encoding requests cannot be made here as FFmpeg does not contain a native hevc encoder.

cf. https://trac.ffmpeg.org/ticket/7965

3 Answers 3

26

VideoToolbox supports alpha / transparency

FFmpeg does not have its own HEVC/H.265 encoder. It supports 7 HEVC/H.265 external encoders (most are hardware based) including libx265 which does not currently support alpha.

As of now only the VideoToolbox HEVC Encoder (-c:v hevc_videotoolbox) supports alpha. This is a hardware accelerated encoder for Apple hardware. This has been updated after FFmpeg 4.3 was released, so you'll have to use a build from the current git branch, or wait for 4.4.

Example command adapted from the relevant commit:

ffmpeg -i input%03d.png -c:v hevc_videotoolbox -allow_sw 1 -alpha_quality 0.75 -vtag hvc1 output.mov

The "HEVC Video with Alpha" profile will only be used when the -alpha_quality value is not 0 (default is 0, range 0-1.). See ffmpeg -h encoder=hevc_videotoolbox for more info.

This answer may become outdated due to continuous developments, so make sure to research the current situation.

Checking if an encoder supports alpha / transparency

$ ffmpeg -h encoder=libx265
Supported pixel formats: yuv420p yuvj420p yuv422p yuvj422p yuv444p yuvj444p gbrp yuv420p10le yuv422p10le yuv444p10le gbrp10le yuv420p12le yuv422p12le yuv444p12le gbrp12le gray gray10le gray12le

If it supported alpha it would have pixel format with an a in the name, such as yuva420p or rgba. As you can see libx265 does not yet support alpha.

Related bug reports to watch

10
  • 1
    So it's impossible to do that, at least with FFMPEG? Commented May 7, 2020 at 16:59
  • would you know an alternative solution? Commented May 7, 2020 at 17:02
  • @BastienRobert Stick with VP9 (webm) for alpha, or investigate other HEVC encoders.
    – llogan
    Commented May 7, 2020 at 17:06
  • @BastienRobert Looks like Apple's Compressor can do it.
    – llogan
    Commented May 7, 2020 at 17:11
  • 1
    I tried this solution, copied exactly what the poster said, but the alpha channel was missing. I'm noticing that the "HEVC Video with Alpha" profile is also missing. Commented Sep 22, 2022 at 0:45
4

This is my example how to convert transparent VP9 webm video to mov. Instead of libvpx better to use a new libvpx-vp9 (the more advanced version of libvpx).

Step 1. Split webm video to frames

ffmpeg -vcodec libvpx-vp9 -i input.webm -pix_fmt rgba image_%04d.png

Step 2. Combine all frames to a new mov video.

ffmpeg -i image_%04d.png -c:v hevc_videotoolbox -allow_sw 1 -alpha_quality 0.75 -vtag hvc1 output.mov

Now you can use it on web

<video autoplay muted loop playsinline>
    <source src="output.mov" type="video/quicktime">
    <source src="input.webm" type="video/webm">
</video>
0
2

I also racked my brains over this problem but finally got it to work. Here are the steps I did (on a Mac):

  1. Convert the transparent webm to a set of transparent pngs (with 25fps) with this command:

    ffmpeg -vcodec libvpx -i transparent.webm -pix_fmt rgba -r 25 out/image_%d.png

  2. Create a transparent mov video from the pngs:

    cd out
    ffmpeg -i image_%d.png -c:v prores_ks -profile:v 5  -bits_per_mb 8000 -pix_fmt yuva444p10le output.mov
    

(taken from: how can I convert a stack of PNGs with transparency to a video playable with Keynote )

  1. This mov is transparent but has a huge filesize (10gb in my case). Open the video with quicktime, click File > Export as. Select the desired resolution and then select HEVC and keep “Preserve Transparency” checked.

Not the answer you're looking for? Browse other questions tagged or ask your own question.