Skip to content

Video editing with Python

merge

osaajani0bb9a77398da
diff --cc moviepy/video/io/ffmpeg_reader.py
index f5c60151,55eb1762..f96f584d
--- a/moviepy/video/io/ffmpeg_reader.py
+++ b/moviepy/video/io/ffmpeg_reader.py
@@@ -96,22 -100,8 +100,22 @@@ class FFMPEG_VideoReader
"%.06f" % offset,
]
else:
- i_arg = ["-i", self.filename]
+ i_arg = ["-i", ffmpeg_escape_filename(self.filename)]
+ # For webm video (vp8 and vp9) with transparent layer, force libvpx/libvpx-vp9
+ # as ffmpeg native webm decoder dont decode alpha layer
+ # (see
+ # https://www.reddit.com/r/ffmpeg/comments/fgpyfb/help_with_webm_with_alpha_channel/
+ # )
+ if self.depth == 4:
+ codec_name = self.infos.get("video_codec_name")
+ if codec_name == "vp9":
+ i_arg = ["-c:v", "libvpx-vp9"] + i_arg
+ elif codec_name == "vp8":
+ i_arg = ["-c:v", "libvpx"] + i_arg
+
+ print(self.infos)
+
cmd = (
[FFMPEG_BINARY]
+ i_arg
diff --cc moviepy/video/io/ffmpeg_writer.py
index 8d8f22f4,e66cb488..1fe28401
--- a/moviepy/video/io/ffmpeg_writer.py
+++ b/moviepy/video/io/ffmpeg_writer.py
@@@ -132,14 -129,9 +132,14 @@@ class FFMPEG_VideoWriter
if threads is not None:
cmd.extend(["-threads", str(threads)])
- if (codec == "libx264") and (size[0] % 2 == 0) and (size[1] % 2 == 0):
- cmd.extend(["-pix_fmt", "yuv420p"])
+ # Disable auto alt ref for transparent webm and set pix format yo yuva420p
+ if codec == "libvpx" and with_mask:
+ cmd.extend(["-pix_fmt", "yuva420p"])
+ cmd.extend(["-auto-alt-ref", "0"])
+ elif (codec == "libx264") and (size[0] % 2 == 0) and (size[1] % 2 == 0):
+ cmd.extend(["-pix_fmt", "yuva420p"])
+
- cmd.extend([filename])
+ cmd.extend([ffmpeg_escape_filename(filename)])
popen_params = cross_platform_popen_params(
{"stdout": sp.DEVNULL, "stderr": logfile, "stdin": sp.PIPE}
diff --cc tests/test_ffmpeg_writer.py
index db015f67,418d3718..a56d5096
--- a/tests/test_ffmpeg_writer.py
+++ b/tests/test_ffmpeg_writer.py
@@@ -233,23 -233,7 +233,29 @@@ def test_write_gif(util, clip_class, lo
assert b == 255
+def test_transparent_video(util):
+ # Has one R 30%
+ clip = ColorClip((100, 100), (255, 0, 0, 76.5)).with_duration(2)
+ filename = os.path.join(util.TMP_DIR, "opacity.webm")
+
+ ffmpeg_write_video(clip, filename, codec="libvpx", fps=5)
+
+ # Load output file and check transparency
+ result = VideoFileClip(filename, has_mask=True)
+
+ # Check for mask
+ assert result.mask is not None
+
+ # Check correct opacity, allow for some tolerance (about 1%)
+ # to consider rounding and compressing error
+ frame = result.mask.get_frame(1)
+ opacity = frame[50, 50]
+ assert abs(opacity - 0.3) < 0.01
+
+ result.close()
++
++
+ def test_write_file_with_spaces(util):
+ filename = os.path.join(util.TMP_DIR, "name with spaces.mp4")
+ clip = ColorClip((1, 1), color=1, is_mask=True).with_fps(1).with_duration(0.3)
+ ffmpeg_write_video(clip, filename, fps=1)
diff --cc tests/test_issues.py
index 8983ccfc,4bf46bd5..ed395707
--- a/tests/test_issues.py
+++ b/tests/test_issues.py
@@@ -360,81 -360,14 +360,90 @@@ def test_issue_1682_2(util)
clip.write_audiofile(output_audio_filepath)
+def test_issue_2269(util):
+ filename = "media/big_buck_bunny_0_30.webm"
+ clip = VideoFileClip(filename).subclipped(0, 3)
+ color_clip = ColorClip((500, 200), (255, 0, 0, 255)).with_duration(3)
+ txt_clip_with_margin = TextClip(
+ text="Hello",
+ font=util.FONT,
+ font_size=72,
+ stroke_color="white",
+ stroke_width=10,
+ margin=(10, 5, 0, 0),
+ text_align="center",
+ ).with_duration(3)
+
+ comp1 = CompositeVideoClip(
+ [color_clip, txt_clip_with_margin.with_position(("center", "center"))]
+ )
+ comp2 = CompositeVideoClip([clip, comp1.with_position(("center", "center"))])
+
+ # If transparency work as expected, this pixel should be pure red at 2 seconds
+ frame = comp2.get_frame(2)
+ pixel = frame[334, 625]
+
+ # We add a bit of tolerance (about 1%) to account
+ # For possible rounding errors
+ assert np.allclose(pixel, [255, 0, 0], rtol=0.01)
+
+
+def test_issue_2269_2(util):
+ clip1 = ColorClip((200, 200), (255, 0, 0)).with_duration(3)
+ clip2 = ColorClip((100, 100), (0, 255, 0, 76.5)).with_duration(3)
+ clip3 = ColorClip((50, 50), (0, 0, 255, 76.5)).with_duration(3)
+
+ compostite_clip1 = CompositeVideoClip(
+ [clip1, clip2.with_position(("center", "center"))]
+ )
+ compostite_clip2 = CompositeVideoClip(
+ [compostite_clip1, clip3.with_position(("center", "center"))]
+ )
+
+ # If transparency work as expected the clip should match thoses colors
+ frame = compostite_clip2.get_frame(2)
+ pixel1 = frame[100, 10]
+ pixel2 = frame[100, 60]
+ pixel3 = frame[100, 100]
+
+ # We add a bit of tolerance (about 1%) to account
+ # For possible rounding errors
+ assert np.allclose(pixel1, [255, 0, 0], rtol=0.01)
+ assert np.allclose(pixel2, [179, 76, 0], rtol=0.01)
+ assert np.allclose(pixel3, [126, 53, 76], rtol=0.01)
+
+
+def test_issue_2269_3(util):
+ # This time all clips have transparency
+ clip1 = ColorClip((200, 200), (255, 0, 0, 76.5)).with_duration(3)
+ clip2 = ColorClip((100, 100), (0, 255, 0, 76.5)).with_duration(3)
+ clip3 = ColorClip((50, 50), (0, 0, 255, 76.5)).with_duration(3)
+
+ compostite_clip1 = CompositeVideoClip(
+ [clip1, clip2.with_position(("center", "center"))]
+ )
+ compostite_clip2 = CompositeVideoClip(
+ [compostite_clip1, clip3.with_position(("center", "center"))]
+ )
+
+ # If transparency work as expected the clip transparency should be between 0.3 and 0.657
+ frame = compostite_clip2.mask.get_frame(2)
+ pixel1 = frame[100, 10]
+ pixel2 = frame[100, 60]
+ pixel3 = frame[100, 100]
+ assert pixel1 == 0.3
+ assert pixel2 == 0.51
+ assert pixel3 == 0.657
+
++
+ def test_issue_2160(util):
+ filename = "media/-video-with-dash-.mp4"
+ clip = VideoFileClip(filename)
+ output_video_filepath = os.path.join(
+ util.TMP_DIR, "big_buck_bunny_0_30_cutout.webm"
+ )
+ clip.write_videofile(output_video_filepath)
+
if __name__ == "__main__":
pytest.main()