You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Moviepy制作带Alpha通道的透明视频?现有方案遇问题

Solution for Generating Transparent Videos with MoviePy

Key Issues in Your Current Approach

  • The rawvideo codec in AVI doesn't support alpha channels—AVI is not suitable for transparent video output.
  • Setting bg_color when exporting image sequences overrides alpha channel preservation, which causes the loss of transparency.

1. Export as MOV with PNG Codec

The MOV container supports alpha channels when using the PNG codec, which reliably preserves transparency.

import moviepy.editor as mpe

# Load source video and mask
clip = mpe.VideoFileClip("input3.mov")
mask = mpe.ImageClip("01.png", ismask=True, fromalpha=True).to_mask()
# Ensure mask matches video dimensions
mask = mask.resize(clip.size)
clip = clip.set_mask(mask)

# Export with PNG codec (supports alpha)
final_clip = mpe.CompositeVideoClip([clip])
final_clip.write_videofile(
    "output_transparent.mov",
    codec="png",
    fps=25,
    logger="bar"
)

2. Export as WebM with VP9 Codec

WebM is a modern, web-friendly format that supports alpha channels via the VP9 codec.

import moviepy.editor as mpe

clip = mpe.VideoFileClip("input3.mov")
mask = mpe.ImageClip("01.png", ismask=True, fromalpha=True).to_mask()
mask = mask.resize(clip.size)
clip = clip.set_mask(mask)

final_clip = mpe.CompositeVideoClip([clip])
final_clip.write_videofile(
    "output_transparent.webm",
    codec="libvpx-vp9",
    ffmpeg_params=["-pix_fmt", "yuva420p"],
    fps=25,
    logger="bar"
)

3. Fixing Image Sequence Alpha (Optional)

If you still need image sequences without black edges, avoid setting bg_color. The black edges are likely from mask edge artifacts—pre-process your PNG mask to add soft anti-aliasing to smooth edges.

import moviepy.editor as mpe

clip = mpe.VideoFileClip("input3.mov")
mask = mpe.ImageClip("01.png", ismask=True, fromalpha=True).to_mask()
mask = mask.resize(clip.size)
clip = clip.set_mask(mask)

final_clip = mpe.CompositeVideoClip([clip])
final_clip.write_images_sequence(
    '00/frame%05d.png',
    fps=25,
    withmask=True,
    logger='bar'
)

Important Notes

  • Always ensure your mask has the exact same dimensions as the source video (use resize to adjust if needed).
  • Adobe After Effects will correctly recognize transparency in MOV/PNG or WebM/VP9 files.
  • Avoid using AVI for transparent video workflows; opt for MOV, WebM, or ProRes (for professional production) instead.

内容的提问来源于stack exchange,提问作者Bo_O

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 15:45:57