Bo_O
Bo_O

Reputation: 67

How can I make a transparent video with Moviepy?

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?

Upvotes: 3

Views: 5528

Answers (3)

Jason Brickman
Jason Brickman

Reputation: 1

THERE IS A BUG IN THE MOVIEPY LIBRARY!! WHEN YOU USE .write_videofile(), IT DOES NOT USE MASKS PROPERLY!!

I have done some digging into this, and I have a solution here.

I looked into the decorators, and the @convert_masks_to_RGB only applies if you try to export a mask, not if you try to export a clip that has a mask, so that's not the problem.

The problem is in 2 files: moviepy/video/VideoClip.py and moviepy/video/io/ffmpeg_writer.py

to fix it, do as follows:

  1. Add this to VideoClip.py before ffmpeg_write_video() - line 300
if self.mask is not None:
    withmask=True
else:
    withmask=False
  1. At the same spot in VideoClip.py, add the following to the ffmpeg_write_video() function call withmask=withmask,

  2. in ffmpeg_writer.py, line 212, add the following to the list of passed arguments line in the line that says "with FFMPEG_VideoWriter(...) as writer": withmask=withmask,

I am also using ffmpeg commands to export to prores 4444 e.g. phrase.write_videofile("clip.mov"), codec = 'prores_ks', ffmpeg_params = ['-c:v', 'prores_ks', '-profile:v', '4', '-pix_fmt', 'yuva444p10le'])

This bug caused me LOTS of headaches, but once I did the above, video masking worked perfectly. Please let me know your results.

Upvotes: 0

Jason Brickman
Jason Brickman

Reputation: 1

I am working with moviepy and I used something like this in order to export a transparent video without having to use adobe premiere pro

composite.write_videofile("composition.avi", codec = 'hap_alpha', ffmpeg_params = ['-c:v', 'hap', '-format', 'hap_alpha', '-vf', 'chromakey=black:0.1:0.1'], audio = False)

First I specified a codec that accepts transparency. I'm using hap, which I had to install independently. You could also use like pro res or something. Then, I used the ffmpeg_params parameter to pass a list of parameters to ffmpeg as the video exports. It doesn't really make sense, but you need quotes around every piece and a comma between every piece. These are just the params you would give to a terminal in order to export a video using ffmpeg regularly. At first I had 2 sets of quotes around the chromakey, and that threw an error, so only use 1 set of quotes!

Upvotes: 0

Bo_O
Bo_O

Reputation: 67

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]

Upvotes: 1

Related Questions