Skip to content

Commit

Permalink
Use av_guess_sample_aspect_ratio when reporting stream SAR, DAR
Browse files Browse the repository at this point in the history
  • Loading branch information
moonsikpark committed Jun 17, 2024
1 parent 5ab63db commit 3646f8d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions av/video/stream.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class VideoStream(Stream):
bit_rate_tolerance: int
thread_count: int
thread_type: Any
sample_aspect_ratio: Fraction | None
display_aspect_ratio: Fraction | None
codec_context: VideoCodecContext
# from codec context
format: VideoFormat
Expand All @@ -24,8 +26,6 @@ class VideoStream(Stream):
framerate: Fraction
rate: Fraction
gop_size: int
sample_aspect_ratio: Fraction | None
display_aspect_ratio: Fraction | None
has_b_frames: bool
coded_width: int
coded_height: int
Expand Down
29 changes: 29 additions & 0 deletions av/video/stream.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,32 @@ cdef class VideoStream(Stream):
# 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 sample_aspect_ratio(self):
"""The guessed sample aspect ratio (SAR) of this stream.
This is a wrapper around :ffmpeg:`av_guess_sample_aspect_ratio`, and uses multiple
heuristics to decide what is "the" sample aspect ratio.
:type: :class:`~fractions.Fraction` or ``None``
"""
cdef lib.AVRational sar = lib.av_guess_sample_aspect_ratio(self.container.ptr, self.ptr, NULL)
return avrational_to_fraction(&sar)

@property
def display_aspect_ratio(self):
"""The guessed display aspect ratio (DAR) of this stream.
This is calculated from :meth:`.VideoStream.guessed_sample_aspect_ratio`.
:type: :class:`~fractions.Fraction` or ``None``
"""
cdef lib.AVRational dar

lib.av_reduce(
&dar.num, &dar.den,
self.format.width * self.sample_aspect_ratio.num,
self.format.height * self.sample_aspect_ratio.den, 1024*1024)

return avrational_to_fraction(&dar)
6 changes: 6 additions & 0 deletions include/libavformat/avformat.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,12 @@ cdef extern from "libavformat/avformat.h" nogil:
AVFrame *frame
)

cdef AVRational av_guess_sample_aspect_ratio(
AVFormatContext *ctx,
AVStream *stream,
AVFrame *frame
)

cdef const AVInputFormat* av_demuxer_iterate(void **opaque)
cdef const AVOutputFormat* av_muxer_iterate(void **opaque)

Expand Down

0 comments on commit 3646f8d

Please sign in to comment.