How can I make a transparent video with Moviepy?

Viewed 148

I'm trying to create a transparent video with alpha.

This is my code:

import moviepy.editor as mpe
clip = mpe.VideoFileClip("input3.mov")
mask = mpe.ImageClip("01.png", ismask=True, fromalpha=True).to_mask()
clip = clip.set_mask(mask)
final_clip = mpe.CompositeVideoClip([clip])
final_clip.write_videofile("output1.avi", codec="rawvideo")

files used:

01.png

input3.mov

The output is not transparent (tested with Adobe After Effects).

After reading https://github.com/Zulko/moviepy/issues/1502 I found a way to do it - by rendering the clip as a sequence of images, I end up with a sequence of images with alpha. Although it is not a transparent video, but a transparent picture sequence, I can also accept it, but it needs to be reprocessed.

import moviepy.editor as mpe
clip = mpe.VideoFileClip("input3.mov")
mask = mpe.ImageClip("01.png", ismask=True, fromalpha=True).to_mask()
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')

But there is a little problem, there is a black border around the edge of the picture. ↓↓↓

black border.jpg

I guess it may be the effect of the default black background. After I changed the background color, I found that the exported image sequence lost alpha, which is not the result I wanted.

import moviepy.editor as mpe
clip = mpe.VideoFileClip("input3.mov")
mask = mpe.ImageClip("01.png", ismask=True, fromalpha=True).to_mask()
clip = clip.set_mask(mask)
final_clip = mpe.CompositeVideoClip([clip], bg_color=(255, 255, 255))
final_clip.write_images_sequence('00/frame%05d.png', fps=25, withmask=True, logger='bar')

White background image with missing alpha. ↓↓↓

missing alpha.png

How else can get the transparent video directly?

1 Answers

I modified the footage and code to make the video and I ended up with a transparent video.

This method has limitations and is not suitable for all situations.

This is the transparent image material used to make the video. After exporting the transparent sequence image, there will be a black border, so this method is not used.

enter image description here

change ideas:

Make each transparent image a white background image and a grayscale information image converted from alpha.

enter image description here enter image description here

Make a video twice with different image assets, then use Adobe Premiere's keying feature to make a transparent video.

The final effect is as follows:

[video.mp4]

Related