Video editing with Python
Fix incohernet frame seek as describe in issue #2115 and pr #2117
osaajani5b69eb508e85
diff --git a/media/smpte-2997.mp4 b/media/smpte-2997.mp4new file mode 100644index 00000000..4f387ed1Binary files /dev/null and b/media/smpte-2997.mp4 differdiff --git a/moviepy/video/io/ffmpeg_reader.py b/moviepy/video/io/ffmpeg_reader.pyindex 8f6835aa..f270829c 100644--- a/moviepy/video/io/ffmpeg_reader.py+++ b/moviepy/video/io/ffmpeg_reader.py@@ -89,6 +89,27 @@ class FFMPEG_VideoReader:"""self.close(delete_lastread=False) # if any+ # self.pos represents the (0-indexed) index of the frame that is next in line+ # to be read by self.read_frame().+ # Eg when self.pos is 1, the 2nd frame will be read next.+ self.pos = self.get_frame_number(start_time)++ # Getting around a difference between ffmpeg and moviepy seeking:+ # "moviepy seek" means "get the frame displayed at time t"+ # Hence given a 29.97 FPS video, seeking to .01s means "get frame 0".+ # "ffmpeg seek" means "skip all frames until you reach time t".+ # This time, seeking to .01s means "get frame 1". Surprise!+ #+ # (In 30fps, timestamps like 2.0s, 3.5s will give the same frame output+ # under both rules, for the timestamp can be represented exactly in+ # decimal.)+ #+ # So we'll subtract an epsilon from the timestamp given to ffmpeg.+ if self.pos != 0:+ start_time = self.pos * (1 / self.fps) - 0.00001+ else:+ start_time = 0.0+if start_time != 0:offset = min(1, start_time)i_arg = [@@ -143,11 +164,6 @@ class FFMPEG_VideoReader:})self.proc = sp.Popen(cmd, **popen_params)-- # self.pos represents the (0-indexed) index of the frame that is next in line- # to be read by self.read_frame().- # Eg when self.pos is 1, the 2nd frame will be read next.- self.pos = self.get_frame_number(start_time)self.last_read = self.read_frame()def skip_frames(self, n=1):diff --git a/tests/test_ffmpeg_reader.py b/tests/test_ffmpeg_reader.pyindex 40a520c1..7b5896e9 100644--- a/tests/test_ffmpeg_reader.py+++ b/tests/test_ffmpeg_reader.py@@ -784,5 +784,10 @@ def test_read_transparent_video():assert mask[100, 100] == 255+def test_frame_seek():+ reader = FFMPEG_VideoReader("media/smpte-2997.mp4", pixel_format="rgba")+++if __name__ == "__main__":pytest.main()