Skip to content

Commit

Permalink
Only VideoStream now has avg_rate, base, guessed
Browse files Browse the repository at this point in the history
  • Loading branch information
WyattBlue committed Mar 9, 2024
1 parent 57f1b92 commit 81e27ad
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 57 deletions.
3 changes: 2 additions & 1 deletion av/audio/stream.pxd
Original file line number Diff line number Diff line change
@@ -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=?)
42 changes: 0 additions & 42 deletions av/stream.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
3 changes: 2 additions & 1 deletion av/video/stream.pxd
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
43 changes: 43 additions & 0 deletions av/video/stream.pyx
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -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)
14 changes: 1 addition & 13 deletions tests/test_file_probing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 81e27ad

Please sign in to comment.