Reputation: 67
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:
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. ↓↓↓
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. ↓↓↓
How else can get the transparent video directly?
Upvotes: 3
Views: 5528
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:
if self.mask is not None:
withmask=True
else:
withmask=False
At the same spot in VideoClip.py, add the following to the ffmpeg_write_video() function call withmask=withmask,
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
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
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.
change ideas:
Make each transparent image a white background image and a grayscale information image converted from alpha.
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:
Upvotes: 1