Search code examples
videoffmpeg

How to make ffmpeg preserve transparency?


We know ffmpeg can help us with overlaying image on video. My target is to wrap a video with an image whose background is transparent.

ffmpeg -i device/nexus5_portrait.png -i device/input.mp4\
-filter_complex "overlay=(W-w)/2:(H-h)/2:enable='between(t,0,5)'"\
-b:v 16M -bufsize 16M device/output.mp4;

Output.mp4 has a black background. So is there any way to preserve the alpha channel?

Thanks in advance!


Solution

  • A new idea saves me.

    • Use ffmpeg caonver input.mp4 to input.gif
    • Use ImageMagick to overlay input.gif on nexus5_portrait.png.

      ffmpeg -i input.mp4 -b:v 16M input.gif
      magick image.png null: \( input.gif -coalesce \) -gravity Center -layers composite des.gif