Skip to content

Commit 25c7b44

Browse files
committed
GH-59: Fix the last frame generation issue
1 parent f3fd16c commit 25c7b44

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/thumbnails/video.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,20 @@ def _extract_frame(self, start_time):
8888
output = os.path.join(self.tempdir.name, filename)
8989
os.close(os.open(output, os.O_CREAT, mode=0o664))
9090

91-
cmd = (
92-
ffmpeg_bin,
93-
"-ss", offset,
91+
ffmpeg_options = (
9492
"-i", self.filepath,
9593
"-loglevel", "error",
9694
"-vframes", "1",
9795
output,
9896
"-y",
9997
)
10098

101-
subprocess.Popen(cmd).wait()
99+
subprocess.Popen((ffmpeg_bin, "-ss", offset, *ffmpeg_options)).wait()
100+
101+
if os.stat(output).st_size == 0:
102+
# Check if the frame is empty. If so, try to extract it again with a smaller offset.
103+
# This handles the case when ffmpeg cannot extract the last frame of the video.
104+
subprocess.Popen((ffmpeg_bin, "-sseof", "-0.1", *ffmpeg_options)).wait()
102105

103106
def extract_frames(self):
104107
"""Extracts the frames from the video by given intervals."""

0 commit comments

Comments
 (0)