Repository: just-work/fffw

Issue:
Trim sets incorrect duration
If Trim end is after EOF, stream output duration is greater than expected.

Discussion:

Patch:
diff --git a/fffw/encoding/filters.py b/fffw/encoding/filters.py
index 194424c..1f49262 100644
--- a/fffw/encoding/filters.py
+++ b/fffw/encoding/filters.py
@@ -261,6 +261,8 @@ class Trim(AutoFilter):
         meta = metadata[0]
         scenes = []
         streams: List[str] = []
+        start = self.start or TS(0)
+        end = min(meta.duration, self.end or TS(0))
         for scene in meta.scenes:
             if scene.stream and (not streams or streams[0] != scene.stream):
                 # Adding an input stream without contiguous duplicates.
@@ -279,12 +281,12 @@ class Trim(AutoFilter):
                                     duration=end - start))
 
         kwargs = {
-            'start': self.start,
-            'duration': self.end,
+            'start': start,
+            'duration': end,
             'scenes': scenes,
             'streams': streams
         }
-        interval = cast(TS, self.end) - cast(TS, self.start)
+        interval = cast(TS, end) - cast(TS, start)
         if isinstance(meta, AudioMeta):
             kwargs['samples'] = round(meta.sampling_rate * interval)
         if isinstance(meta, VideoMeta):


Test patch:
diff --git a/tests/test_graph.py b/tests/test_graph.py
index ecf7948..c7db0fd 100644
--- a/tests/test_graph.py
+++ b/tests/test_graph.py
@@ -316,6 +316,15 @@ class FilterGraphTestCase(TestCase):
         self.assertEqual(vm.duration, TS(4.0))
         self.assertEqual(vm.frames, 1.0 * vm.frame_rate)
 
+    def test_video_trim_end_of_stream(self):
+        """
+        If Trim ends after stream end, duration is set to min value.
+        """
+        f = self.source | Trim(VIDEO, start=5.0, end=400.0) | SetPTS(VIDEO)
+        f > self.output
+        vm = cast(VideoMeta, self.output.codecs[0].get_meta_data())
+        self.assertEqual(vm.duration, TS(295.0))
+
     def test_audio_trim_metadata(self):
         """
         Trim filter sets start and changes stream duration.