Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make FFMPEG_VideoReader more robust to missing information. #2253

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading