From 81e27ad28f2cc308d778eac50c7c840a00bbc9aa Mon Sep 17 00:00:00 2001 From: WyattBlue Date: Sat, 9 Mar 2024 07:59:46 -0500 Subject: [PATCH] Only VideoStream now has avg_rate, base, guessed --- av/audio/stream.pxd | 3 ++- av/stream.pyx | 42 ------------------------------------- av/video/stream.pxd | 3 ++- av/video/stream.pyx | 43 ++++++++++++++++++++++++++++++++++++++ tests/test_file_probing.py | 14 +------------ 5 files changed, 48 insertions(+), 57 deletions(-) diff --git a/av/audio/stream.pxd b/av/audio/stream.pxd index a366ad556..8462061f8 100644 --- a/av/audio/stream.pxd +++ b/av/audio/stream.pxd @@ -1,8 +1,9 @@ -from av.stream cimport Stream from av.packet cimport Packet +from av.stream cimport Stream from .frame cimport AudioFrame + cdef class AudioStream(Stream): cpdef encode(self, AudioFrame frame=?) cpdef decode(self, Packet packet=?) diff --git a/av/stream.pyx b/av/stream.pyx index 08744771e..1b5e967dd 100644 --- a/av/stream.pyx +++ b/av/stream.pyx @@ -217,48 +217,6 @@ cdef class Stream: """ to_avrational(value, &self.ptr.time_base) - @property - def average_rate(self): - """ - The average frame rate of this video stream. - - This is calculated when the file is opened by looking at the first - few frames and averaging their rate. - - :type: :class:`~fractions.Fraction` or ``None`` - - - """ - return avrational_to_fraction(&self.ptr.avg_frame_rate) - - @property - def base_rate(self): - """ - The base frame rate of this stream. - - This is calculated as the lowest framerate at which the timestamps of - frames can be represented accurately. See :ffmpeg:`AVStream.r_frame_rate` - for more. - - :type: :class:`~fractions.Fraction` or ``None`` - - """ - return avrational_to_fraction(&self.ptr.r_frame_rate) - - @property - def guessed_rate(self): - """The guessed frame rate of this stream. - - This is a wrapper around :ffmpeg:`av_guess_frame_rate`, and uses multiple - heuristics to decide what is "the" frame rate. - - :type: :class:`~fractions.Fraction` or ``None`` - - """ - # The two NULL arguments aren't used in FFmpeg >= 4.0 - cdef lib.AVRational val = lib.av_guess_frame_rate(NULL, self.ptr, NULL) - return avrational_to_fraction(&val) - @property def start_time(self): """ diff --git a/av/video/stream.pxd b/av/video/stream.pxd index 252243dd5..f0dcfb9b2 100644 --- a/av/video/stream.pxd +++ b/av/video/stream.pxd @@ -1,6 +1,7 @@ +from av.packet cimport Packet from av.stream cimport Stream + from .frame cimport VideoFrame -from av.packet cimport Packet cdef class VideoStream(Stream): diff --git a/av/video/stream.pyx b/av/video/stream.pyx index bff1f77f4..abd4eac52 100644 --- a/av/video/stream.pyx +++ b/av/video/stream.pyx @@ -1,6 +1,11 @@ +cimport libav as lib + from av.packet cimport Packet +from av.utils cimport avrational_to_fraction, to_avrational + from .frame cimport VideoFrame + cdef class VideoStream(Stream): def __repr__(self): return ( @@ -35,3 +40,41 @@ cdef class VideoStream(Stream): """ return self.codec_context.decode(packet) + + @property + def average_rate(self): + """ + The average frame rate of this video stream. + + This is calculated when the file is opened by looking at the first + few frames and averaging their rate. + + :type: :class:`~fractions.Fraction` or ``None`` + """ + return avrational_to_fraction(&self.ptr.avg_frame_rate) + + @property + def base_rate(self): + """ + The base frame rate of this stream. + + This is calculated as the lowest framerate at which the timestamps of + frames can be represented accurately. See :ffmpeg:`AVStream.r_frame_rate` + for more. + + :type: :class:`~fractions.Fraction` or ``None`` + """ + return avrational_to_fraction(&self.ptr.r_frame_rate) + + @property + def guessed_rate(self): + """The guessed frame rate of this stream. + + This is a wrapper around :ffmpeg:`av_guess_frame_rate`, and uses multiple + heuristics to decide what is "the" frame rate. + + :type: :class:`~fractions.Fraction` or ``None`` + """ + # The two NULL arguments aren't used in FFmpeg >= 4.0 + cdef lib.AVRational val = lib.av_guess_frame_rate(NULL, self.ptr, NULL) + return avrational_to_fraction(&val) diff --git a/tests/test_file_probing.py b/tests/test_file_probing.py index c3d239c50..928a5bd1d 100644 --- a/tests/test_file_probing.py +++ b/tests/test_file_probing.py @@ -34,20 +34,12 @@ def test_stream_probing(self): ) # actual stream properties - self.assertEqual(stream.average_rate, None) - self.assertEqual(stream.base_rate, None) - self.assertEqual(stream.guessed_rate, None) self.assertEqual(stream.duration, 554880) self.assertEqual(stream.frames, 0) self.assertEqual(stream.id, 256) self.assertEqual(stream.index, 0) self.assertEqual(stream.language, "eng") - self.assertEqual( - stream.metadata, - { - "language": "eng", - }, - ) + self.assertEqual(stream.metadata, {"language": "eng"}) self.assertEqual(stream.profile, "LC") self.assertEqual(stream.start_time, 126000) self.assertEqual(stream.time_base, Fraction(1, 90000)) @@ -98,9 +90,6 @@ def test_stream_probing(self): ) # actual stream properties - self.assertEqual(stream.average_rate, None) - self.assertEqual(stream.base_rate, None) - self.assertEqual(stream.guessed_rate, None) self.assertEqual(stream.duration, None) self.assertEqual(stream.frames, 0) self.assertEqual(stream.id, 0) @@ -239,7 +228,6 @@ def test_stream_probing(self): ) # actual stream properties - self.assertEqual(stream.average_rate, None) self.assertEqual(stream.duration, 8140) self.assertEqual(stream.frames, 6) self.assertEqual(stream.id, 1)