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):

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.

Trim sets incorrect duration
If Trim end is after EOF, stream output duration is greater than expected.
tests/test_graph.py::FilterGraphTestCase::test_video_trim_end_of_stream
tests/test_graph.py::FilterGraphTestCase::test_any_hardware_filter tests/test_graph.py::FilterGraphTestCase::test_audio_trim_metadata tests/test_graph.py::FilterGraphTestCase::test_codec_metadata_transform tests/test_graph.py::FilterGraphTestCase::test_concat_audio_metadata tests/test_graph.py::FilterGraphTestCase::test_concat_video_metadata tests/test_graph.py::FilterGraphTestCase::test_disabled_filters tests/test_graph.py::FilterGraphTestCase::test_ensure_audio tests/test_graph.py::FilterGraphTestCase::test_ensure_video tests/test_graph.py::FilterGraphTestCase::test_filter_graph tests/test_graph.py::FilterGraphTestCase::test_filter_validates_hardware_device tests/test_graph.py::FilterGraphTestCase::test_filter_validates_stream_kind tests/test_graph.py::FilterGraphTestCase::test_overlay_metadata tests/test_graph.py::FilterGraphTestCase::test_scale_changes_metadata tests/test_graph.py::FilterGraphTestCase::test_setpts_metadata tests/test_graph.py::FilterGraphTestCase::test_skip_not_connected_sources tests/test_graph.py::FilterGraphTestCase::test_video_trim_metadata