Skip to content

Video editing with Python

Make ffmpeg reader extract codec if possible, and use it to force codec on video reading with alpha channel for vp8 and vp9

osaajani036231e0d2c0
diff --git a/media/transparent.webm b/media/transparent.webm
new file mode 100644
index 00000000..9d900a0c
Binary files /dev/null and b/media/transparent.webm differ
diff --git a/moviepy/video/io/ffmpeg_reader.py b/moviepy/video/io/ffmpeg_reader.py
index 68ff641c..31150570 100644
--- a/moviepy/video/io/ffmpeg_reader.py
+++ b/moviepy/video/io/ffmpeg_reader.py
@@ -98,11 +98,15 @@ class FFMPEG_VideoReader:
else:
i_arg = ["-i", self.filename]
- # For webm video with transparent layer, force libvpx-vp9 as ffmpeg native webm
- # decoder dont decode alpha layer
+ # 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 and self.filename[-5:] == '.webm' :
- i_arg = ["-c:v", "libvpx"] + i_arg
+ 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
cmd = (
[FFMPEG_BINARY]
@@ -124,8 +128,6 @@ class FFMPEG_VideoReader:
]
)
- print(' '.join(cmd))
-
popen_params = cross_platform_popen_params(
{
"bufsize": self.bufsize,
@@ -699,6 +701,25 @@ class FFmpegInfosParser:
fps = x * coef
stream_data["fps"] = fps
+ # Try to extract video codec and profile
+ main_info_match = re.search(
+ r"Video:\s(\w+)?\s(\([^)]+\))?",
+ line.lstrip(),
+ )
+ if main_info_match is not None :
+ (
+ codec_name,
+ profile
+ ) = main_info_match.groups()
+ stream_data['codec_name'] = codec_name
+ stream_data['profile'] = profile
+
+ if self._current_stream["default"] or "video_codec_name" not in self.result:
+ global_data["video_codec_name"] = stream_data.get("codec_name", None)
+
+ if self._current_stream["default"] or "video_profile" not in self.result:
+ global_data["video_profile"] = stream_data.get("profile", None)
+
if self._current_stream["default"] or "video_size" not in self.result:
global_data["video_size"] = stream_data.get("size", None)
if self._current_stream["default"] or "video_bitrate" not in self.result:
@@ -784,6 +805,8 @@ def ffmpeg_parse_infos(
- ``"audio_fps"``
- ``"audio_bitrate"``
- ``"audio_metadata"``
+ - ``"video_codec_name"``
+ - ``"video_profile"``
Note that "video_duration" is slightly smaller than "duration" to avoid
fetching the incomplete frames at the end, which raises an error.