Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use av_guess_sample_aspect_ratio when reporting stream SAR #1425

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading