Skip to content

Commit

Permalink
Make FFMPEG_VideoReader more robust to missing information.
Browse files Browse the repository at this point in the history
Allow `FFMPEG_VideoReader` to function more robustly in the presence of missing information.

Fixes #2256
  • Loading branch information
Breakthrough committed Nov 23, 2024
1 parent 9d784e1 commit e85d7ef
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions moviepy/video/io/ffmpeg_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ def __init__(
decode_file=decode_file,
print_infos=print_infos,
)
self.fps = infos["video_fps"]
self.size = infos["video_size"]
# If framerate is unavailable, assume 1.0 FPS to avoid divide-by-zero errors.
self.fps = infos.get("video_fps", 1.0)
# If frame size is unavailable, set 1x1 divide-by-zero errors.
self.size = infos.get("video_size", (1, 1))

# ffmpeg automatically rotates videos if rotation information is
# available, so exchange width and height
Expand All @@ -55,10 +57,10 @@ def __init__(
self.size = target_resolution
self.resize_algo = resize_algo

self.duration = infos["video_duration"]
self.ffmpeg_duration = infos["duration"]
self.n_frames = infos["video_n_frames"]
self.bitrate = infos["video_bitrate"]
self.duration = infos.get("video_duration", 0.0)
self.ffmpeg_duration = infos.get("duration", 0.0)
self.n_frames = infos.get("video_n_frames", 0)
self.bitrate = infos.get("video_bitrate", 0)

self.infos = infos

Expand Down Expand Up @@ -556,22 +558,25 @@ def parse(self):
# last input file, must be included in self.result
if self._current_input_file:
self._current_input_file["streams"].append(self._current_stream)
# include their chapters, if there are
if len(input_chapters) == self._current_input_file["input_number"] + 1:
# 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)

# 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["video_fps"]
self.result["duration"] * self.result.get("video_fps", 0)
)
self.result["video_duration"] = self.result["duration"]
else:
self.result["video_n_frames"] = 1
self.result["video_duration"] = None
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']

Expand Down

0 comments on commit e85d7ef

Please sign in to comment.