Skip to content

Video editing with Python

Working ffmpeg replacement parser

osaajanib4f28d5e86eb
diff --git a/moviepy/video/io/ffmpeg_reader.py b/moviepy/video/io/ffmpeg_reader.py
index 2cce72c4..68a16130 100644
--- a/moviepy/video/io/ffmpeg_reader.py
+++ b/moviepy/video/io/ffmpeg_reader.py
@@ -327,505 +327,6 @@ def ffmpeg_read_image(filename, with_mask=True, pixel_format=None):
return im
-class FFmpegInfosParser:
- """Finite state ffmpeg `-i` command option file information parser.
- Is designed to parse the output fast, in one loop. Iterates line by
- line of the `ffmpeg -i <filename> [-f null -]` command output changing
- the internal state of the parser.
-
- Parameters
- ----------
-
- filename
- Name of the file parsed, only used to raise accurate error messages.
-
- infos
- Information returned by FFmpeg.
-
- fps_source
- Indicates what source data will be preferably used to retrieve fps data.
-
- check_duration
- Enable or disable the parsing of the duration of the file. Useful to
- skip the duration check, for example, for images.
-
- decode_file
- Indicates if the whole file has been decoded. The duration parsing strategy
- will differ depending on this argument.
- """
-
- def __init__(
- self,
- infos,
- filename,
- fps_source="fps",
- check_duration=True,
- decode_file=False,
- ):
- self.infos = infos
- self.filename = filename
- self.check_duration = check_duration
- self.fps_source = fps_source
- self.duration_tag_separator = "time=" if decode_file else "Duration: "
-
- self._reset_state()
-
- def _reset_state(self):
- """Reinitializes the state of the parser. Used internally at
- initialization and at the end of the parsing process.
- """
- # could be 2 possible types of metadata:
- # - file_metadata: Metadata of the container. Here are the tags set
- # by the user using `-metadata` ffmpeg option
- # - stream_metadata: Metadata for each stream of the container.
- self._inside_file_metadata = False
-
- # this state is needed if `duration_tag_separator == "time="` because
- # execution of ffmpeg decoding the whole file using `-f null -` appends
- # to the output the blocks "Stream mapping:" and "Output:", which
- # should be ignored
- self._inside_output = False
-
- # map from stream type to default stream
- # if a default stream is not indicated, pick the first one available
- self._default_streams = {}
-
- # current input file, stream and chapter, which will be built at runtime
- self._current_input_file = {"streams": []}
- self._current_stream = None
- self._current_chapter = None
-
- # resulting data of the parsing process
- self.result = {
- "video_found": False,
- "audio_found": False,
- "metadata": {},
- "inputs": [],
- }
-
- # keep the value of latest metadata value parsed so we can build
- # at next lines a multiline metadata value
- self._last_metadata_field_added = None
-
- def parse(self):
- """Parses the information returned by FFmpeg in stderr executing their binary
- for a file with ``-i`` option and returns a dictionary with all data needed
- by MoviePy.
- """
- # chapters by input file
- input_chapters = []
-
- for line in self.infos.splitlines()[1:]:
- if (
- self.duration_tag_separator == "time="
- and self.check_duration
- and "time=" in line
- ):
- # parse duration using file decodification
- self.result["duration"] = self.parse_duration(line)
- elif self._inside_output or line[0] != " ":
- if self.duration_tag_separator == "time=" and not self._inside_output:
- self._inside_output = True
- # skip lines like "At least one output file must be specified"
- elif not self._inside_file_metadata and line.startswith(" Metadata:"):
- # enter " Metadata:" group
- self._inside_file_metadata = True
- elif line.startswith(" Duration:"):
- # exit " Metadata:" group
- self._inside_file_metadata = False
- if self.check_duration and self.duration_tag_separator == "Duration: ":
- self.result["duration"] = self.parse_duration(line)
-
- # parse global bitrate (in kb/s)
- bitrate_match = re.search(r"bitrate: (\d+) k(i?)b/s", line)
- self.result["bitrate"] = (
- int(bitrate_match.group(1)) if bitrate_match else None
- )
-
- # parse start time (in seconds)
- start_match = re.search(r"start: (\d+\.?\d+)", line)
- self.result["start"] = (
- float(start_match.group(1)) if start_match else None
- )
- elif self._inside_file_metadata:
- # file metadata line
- field, value = self.parse_metadata_field_value(line)
-
- # multiline metadata value parsing
- if field == "":
- field = self._last_metadata_field_added
- value = self.result["metadata"][field] + "\n" + value
- else:
- self._last_metadata_field_added = field
- self.result["metadata"][field] = value
- elif line.lstrip().startswith("Stream "):
- # exit stream " Metadata:"
- if self._current_stream:
- self._current_input_file["streams"].append(self._current_stream)
-
- # get input number, stream number, language and type
- main_info_match = re.search(
- r"^Stream\s#(\d+):(\d+)(?:\[\w+\])?\(?(\w+)?\)?:\s(\w+):",
- line.lstrip(),
- )
- (
- input_number,
- stream_number,
- language,
- stream_type,
- ) = main_info_match.groups()
- input_number = int(input_number)
- stream_number = int(stream_number)
- stream_type_lower = stream_type.lower()
-
- if language == "und":
- language = None
-
- # start builiding the current stream
- self._current_stream = {
- "input_number": input_number,
- "stream_number": stream_number,
- "stream_type": stream_type_lower,
- "language": language,
- "default": (stream_type_lower not in self._default_streams)
- and line.endswith("(default)"),
- }
-
- # for default streams, set their numbers globally, so it's
- # easy to get without iterating all
- if self._current_stream["default"]:
- self._default_streams[stream_type_lower] = self._current_stream
-
- # exit chapter
- if self._current_chapter:
- input_chapters[input_number].append(self._current_chapter)
- self._current_chapter = None
-
- if "input_number" not in self._current_input_file:
- # first input file
- self._current_input_file["input_number"] = input_number
- elif self._current_input_file["input_number"] != input_number:
- # new input file
-
- # include their chapters if there are for this input file
- if len(input_chapters) >= input_number + 1:
- self._current_input_file["chapters"] = input_chapters[
- input_number
- ]
-
- # add new input file to result
- self.result["inputs"].append(self._current_input_file)
- self._current_input_file = {"input_number": input_number}
-
- # parse relevant data by stream type
- try:
- stream_data = self.parse_data_by_stream_type(stream_type, line)
- except NotImplementedError as exc:
- warnings.warn(
- f"{str(exc)}\nffmpeg output: \n\n{self.infos}", UserWarning
- )
- else:
- self._current_stream.update(stream_data)
- elif line.startswith(" Metadata:"):
- # enter group " Metadata:"
- continue
- elif self._current_stream:
- # stream metadata line
- if "metadata" not in self._current_stream:
- self._current_stream["metadata"] = {}
-
- field, value = self.parse_metadata_field_value(line)
-
- if self._current_stream["stream_type"] == "video":
- field, value = self.video_metadata_type_casting(field, value)
- # ffmpeg 7 now use displaymatrix instead of rotate
- if field == "rotate":
- self.result["video_rotation"] = value
- elif field == "displaymatrix":
- self.result["video_rotation"] = value
-
- # multiline metadata value parsing
- if field == "":
- field = self._last_metadata_field_added
- value = self._current_stream["metadata"][field] + "\n" + value
- else:
- self._last_metadata_field_added = field
- self._current_stream["metadata"][field] = value
- elif line.startswith(" Chapter"):
- # Chapter data line
- if self._current_chapter:
- # there is a previews chapter?
- if len(input_chapters) < self._current_chapter["input_number"] + 1:
- input_chapters.append([])
- # include in the chapters by input matrix
- input_chapters[self._current_chapter["input_number"]].append(
- self._current_chapter
- )
-
- # extract chapter data
- chapter_data_match = re.search(
- r"^ Chapter #(\d+):(\d+): start (\d+\.?\d+?), end (\d+\.?\d+?)",
- line,
- )
- input_number, chapter_number, start, end = chapter_data_match.groups()
-
- # start building the chapter
- self._current_chapter = {
- "input_number": int(input_number),
- "chapter_number": int(chapter_number),
- "start": float(start),
- "end": float(end),
- }
- elif self._current_chapter:
- # inside chapter metadata
- if "metadata" not in self._current_chapter:
- self._current_chapter["metadata"] = {}
- field, value = self.parse_metadata_field_value(line)
-
- # multiline metadata value parsing
- if field == "":
- field = self._last_metadata_field_added
- value = self._current_chapter["metadata"][field] + "\n" + value
- else:
- self._last_metadata_field_added = field
- self._current_chapter["metadata"][field] = value
-
- # last input file, must be included in the result
- if self._current_input_file:
- self._current_input_file["streams"].append(self._current_stream)
- # include their chapters, if there are any
- if (
- "input_number" in self._current_input_file
- and len(input_chapters) == self._current_input_file["input_number"] + 1
- ):
- self._current_input_file["chapters"] = input_chapters[
- self._current_input_file["input_number"]
- ]
- self.result["inputs"].append(self._current_input_file)
-
- # set any missing default automatically
- for stream in self._current_input_file["streams"]:
- if stream["stream_type"] not in self._default_streams:
- self._default_streams[stream["stream_type"]] = stream
- stream["default"] = True
-
- # set some global info based on the defaults
- for stream_type_lower, stream_data in self._default_streams.items():
- self.result[f"default_{stream_type_lower}_input_number"] = stream_data[
- "input_number"
- ]
- self.result[f"default_{stream_type_lower}_stream_number"] = stream_data[
- "stream_number"
- ]
-
- if stream_type_lower == "audio":
- self.result["audio_found"] = True
- self.result["audio_fps"] = stream_data["fps"]
- self.result["audio_bitrate"] = stream_data["bitrate"]
- elif stream_type_lower == "video":
- self.result["video_found"] = True
- self.result["video_size"] = stream_data.get("size", None)
- self.result["video_bitrate"] = stream_data.get("bitrate", None)
- self.result["video_fps"] = stream_data["fps"]
- self.result["video_codec_name"] = stream_data.get("codec_name", None)
- self.result["video_profile"] = stream_data.get("profile", None)
-
- # some video duration utilities
- if self.result["video_found"] and self.check_duration:
- self.result["video_duration"] = self.result["duration"]
- self.result["video_n_frames"] = int(
- self.result["duration"] * self.result.get("video_fps", 0)
- )
- else:
- self.result["video_n_frames"] = 0
- self.result["video_duration"] = 0.0
- # We could have also recomputed duration from the number of frames, as follows:
- # >>> result['video_duration'] = result['video_n_frames'] / result['video_fps']
-
- # not default audio found, assume first audio stream is the default
- if self.result["audio_found"] and not self.result.get("audio_bitrate"):
- self.result["audio_bitrate"] = None
- for streams_input in self.result["inputs"]:
- for stream in streams_input["streams"]:
- if stream["stream_type"] == "audio" and stream.get("bitrate"):
- self.result["audio_bitrate"] = stream["bitrate"]
- break
-
- if self.result["audio_bitrate"] is not None:
- break
-
- result = self.result
-
- # reset state of the parser
- self._reset_state()
-
- return result
-
- def parse_data_by_stream_type(self, stream_type, line):
- """Parses data from "Stream ... {stream_type}" line."""
- try:
- return {
- "Audio": self.parse_audio_stream_data,
- "Video": self.parse_video_stream_data,
- "Data": lambda _line: {},
- }[stream_type](line)
- except KeyError:
- raise NotImplementedError(
- f"{stream_type} stream parsing is not supported by moviepy and"
- " will be ignored"
- )
-
- def parse_audio_stream_data(self, line):
- """Parses data from "Stream ... Audio" line."""
- stream_data = {}
- try:
- stream_data["fps"] = int(re.search(r" (\d+) Hz", line).group(1))
- except (AttributeError, ValueError):
- # AttributeError: 'NoneType' object has no attribute 'group'
- # ValueError: invalid literal for int() with base 10: '<string>'
- stream_data["fps"] = "unknown"
- match_audio_bitrate = re.search(r"(\d+) k(i?)b/s", line)
- stream_data["bitrate"] = (
- int(match_audio_bitrate.group(1)) if match_audio_bitrate else None
- )
- return stream_data
-
- def parse_video_stream_data(self, line):
- """Parses data from "Stream ... Video" line."""
- stream_data = {}
-
- try:
- match_video_size = re.search(r" (\d+)x(\d+)[,\s]", line)
- if match_video_size:
- # size, of the form 460x320 (w x h)
- stream_data["size"] = [int(num) for num in match_video_size.groups()]
- except Exception:
- raise IOError(
- (
- "MoviePy error: failed to read video dimensions in"
- " file '%s'.\nHere are the file infos returned by"
- "ffmpeg:\n\n%s"
- )
- % (self.filename, self.infos)
- )
-
- match_bitrate = re.search(r"(\d+) k(i?)b/s", line)
- stream_data["bitrate"] = int(match_bitrate.group(1)) if match_bitrate else None
-
- # Get the frame rate. Sometimes it's 'tbr', sometimes 'fps', sometimes
- # tbc, and sometimes tbc/2...
- # Current policy: Trust fps first, then tbr unless fps_source is
- # specified as 'tbr' in which case try tbr then fps
-
- # If result is near from x*1000/1001 where x is 23,24,25,50,
- # replace by x*1000/1001 (very common case for the fps).
-
- if self.fps_source == "fps":
- try:
- fps = self.parse_fps(line)
- except (AttributeError, ValueError):
- fps = self.parse_tbr(line)
- elif self.fps_source == "tbr":
- try:
- fps = self.parse_tbr(line)
- except (AttributeError, ValueError):
- fps = self.parse_fps(line)
- else:
- raise ValueError(
- ("fps source '%s' not supported parsing the video '%s'")
- % (self.fps_source, self.filename)
- )
-
- # It is known that a fps of 24 is often written as 24000/1001
- # but then ffmpeg nicely rounds it to 23.98, which we hate.
- coef = 1000.0 / 1001.0
- for x in [23, 24, 25, 30, 50]:
- if (fps != x) and abs(fps - x * coef) < 0.01:
- 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
-
- return stream_data
-
- def parse_fps(self, line):
- """Parses number of FPS from a line of the ``ffmpeg -i`` command output."""
- return float(re.search(r" (\d+.?\d*) fps", line).group(1))
-
- def parse_tbr(self, line):
- """Parses number of TBS from a line of the ``ffmpeg -i`` command output."""
- s_tbr = re.search(r" (\d+.?\d*k?) tbr", line).group(1)
-
- # Sometimes comes as e.g. 12k. We need to replace that with 12000.
- if s_tbr[-1] == "k":
- tbr = float(s_tbr[:-1]) * 1000
- else:
- tbr = float(s_tbr)
- return tbr
-
- def parse_duration(self, line):
- """Parse the duration from the line that outputs the duration of
- the container.
- """
- try:
- time_raw_string = line.split(self.duration_tag_separator)[-1]
- match_duration = re.search(
- r"([0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9])",
- time_raw_string,
- )
- if match_duration is None:
- raise VideoCorruptedError(
- f"Could not parse duration from {time_raw_string!r}"
- )
- return convert_to_seconds(match_duration.group(1))
- except VideoCorruptedError:
- raise
- except Exception:
- raise IOError(
- (
- "MoviePy error: failed to read the duration of file '%s'.\n"
- "Here are the file infos returned by ffmpeg:\n\n%s"
- )
- % (self.filename, self.infos)
- )
-
- def parse_metadata_field_value(
- self,
- line,
- ):
- """Returns a tuple with a metadata field-value pair given a ffmpeg `-i`
- command output line.
- """
- info = line.split(":", 1)
- if len(info) == 2:
- raw_field, raw_value = info
- return (raw_field.strip(" "), raw_value.strip(" "))
- else:
- return ("", "")
-
- def video_metadata_type_casting(self, field, value):
- """Cast needed video metadata fields to other types than the default str."""
- if field == "rotate":
- return (field, float(value))
-
- elif field == "displaymatrix":
- match = re.search(r"[-+]?\d+(\.\d+)?", value)
- if match:
- # We must multiply by -1 because displaymatrix return info
- # about how to rotate to show video, not about video rotation
- return (field, float(match.group()) * -1)
-
- return (field, value)
-
-
class FFmpegBetterInfosParser:
"""Finite state ffmpeg `-i` command option file information parser.
Is designed to parse the output fast, in one loop. Iterates line by
@@ -903,6 +404,8 @@ class FFmpegBetterInfosParser:
"video_found": False,
"audio_found": False,
"metadata": {},
+ "blocks": None,
+ "inputs": {},
}
def _extract_block(self, index, start_indent, block: InfoBlock = None):
@@ -942,7 +445,7 @@ class FFmpegBetterInfosParser:
# New block
if line.lstrip().startswith(
- ("Metadata", "Stream", "Side data", "Chapter")
+ ("Metadata", "Stream", "Side data", "Chapter", "Chapters")
):
(child_block, index) = self._extract_block(
index, indent_level, self.InfoBlock(line.lstrip(), indent_level)
@@ -969,6 +472,8 @@ class FFmpegBetterInfosParser:
self._parse_stream(block)
elif line.startswith("Side data:"):
block.type = "side_data"
+ elif line.startswith("Chapters"):
+ block.type = "chapters"
elif line.startswith("Chapter"):
block.type = "chapter"
self._parse_chapter(block)
@@ -1144,16 +649,16 @@ class FFmpegBetterInfosParser:
tbr = float(s_tbr)
return tbr
- def _parse_chapter(self, block):
+ def _parse_chapter(self, block: InfoBlock):
# extract chapter data
chapter_data_match = re.search(
r"^Chapter #(\d+):(\d+): start (\d+\.?\d+?), end (\d+\.?\d+?)",
- block.head_line.lstrip(),
+ block.head_line.strip(),
)
input_number, chapter_number, start, end = chapter_data_match.groups()
# start building the chapter
- self.data = {
+ block.data = {
"input_number": int(input_number),
"chapter_number": int(chapter_number),
"start": float(start),
@@ -1211,6 +716,40 @@ class FFmpegBetterInfosParser:
self.result["video_n_frames"] = 0
self.result["video_duration"] = 0.0
+ self._populate_inputs(root=root)
+
+ def _populate_inputs(self, root: InfoBlock):
+ """Forge inputs for compatibility with old versions, not used anywhere though"""
+ for child in root.childs:
+ if child.type == "stream":
+ if "streams" not in self.result["inputs"]:
+ self.result["inputs"]["streams"] = []
+
+ stream = child.data
+
+ for stream_child in child.childs:
+ if stream_child.type == "metadata":
+ stream["metadata"] = stream_child.data
+ elif stream_child.type == "side_data":
+ stream["side_data"] = stream_child.data
+
+ self.result["inputs"]["streams"].append(stream)
+
+ elif child.type == "chapters":
+ for chapter in child.childs:
+ if "chapters" not in self.result["inputs"]:
+ self.result["inputs"]["chapters"] = []
+
+ chap = chapter.data
+
+ for chapter_child in chapter.childs:
+ if chapter_child.type == "metadata":
+ chap["metadata"] = chapter_child.data
+ elif chapter_child.type == "side_data":
+ chap["side_data"] = chapter_child.data
+
+ self.result["inputs"]["chapters"].append(chap)
+
def parse(self):
"""Parses the information returned by FFmpeg in stderr executing their binary
for a file with ``-i`` option and returns a dictionary with all data needed
@@ -1227,6 +766,7 @@ class FFmpegBetterInfosParser:
self._extract_block(first_input, 0, root_block)
self._parse_blocks(root_block)
self.blocks = root_block
+ self.result["blocks"] = root_block
return self.result
except self.ParseDimensionError:
raise IOError(
diff --git a/moviepy/video/io/ffmpeg_tools.py b/moviepy/video/io/ffmpeg_tools.py
index 3d27147d..a558b10a 100644
--- a/moviepy/video/io/ffmpeg_tools.py
+++ b/moviepy/video/io/ffmpeg_tools.py
@@ -242,6 +242,7 @@ def ffmpeg_copy(input_file: Union[str, Path], output_file: Union[str, Path]):
# Construct the ffmpeg command
command = [
FFMPEG_BINARY,
+ "-y",
"-i",
str(input_path),
"-c",