Skip to content

Commit 9f9db90

Browse files
committed
StreamMetadata.sample_aspect_ratio: use Fraction instead of tuple[int, int]
1 parent 6c85f63 commit 9f9db90

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

src/torchcodec/_core/_metadata.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import json
99
import pathlib
1010
from dataclasses import dataclass
11+
from fractions import Fraction
1112
from typing import List, Optional, Union
1213

1314
import torch
@@ -80,12 +81,10 @@ class VideoStreamMetadata(StreamMetadata):
8081
average_fps_from_header: Optional[float]
8182
"""Averate fps of the stream, obtained from the header (float or None).
8283
We recommend using the ``average_fps`` attribute instead."""
83-
sample_aspect_ratio: Optional[tuple[int, int]]
84+
sample_aspect_ratio: Optional[Fraction]
8485
"""Sample Aspect Ratio (SAR), also known as Pixel Aspect Ratio
85-
(PAR), is the ratio between the width of a pixel and the height of
86-
each pixel. This is a tuple of two ints: the first element is the
87-
numerator, and the second element is the denominator. Not to be
88-
confused with Storage Aspect Ratio (also SAR)."""
86+
(PAR), is the ratio between the width and height of each pixel
87+
(``fractions.Fraction`` or None)."""
8988

9089
@property
9190
def duration_seconds(self) -> Optional[float]:
@@ -217,9 +216,9 @@ def best_audio_stream(self) -> AudioStreamMetadata:
217216
return metadata
218217

219218

220-
def _get_optional_sar_tuple(stream_dict):
219+
def _get_optional_par_fraction(stream_dict):
221220
try:
222-
return (
221+
return Fraction(
223222
stream_dict["sampleAspectRatioNum"],
224223
stream_dict["sampleAspectRatioDen"],
225224
)
@@ -263,9 +262,7 @@ def get_container_metadata(decoder: torch.Tensor) -> ContainerMetadata:
263262
num_frames_from_header=stream_dict.get("numFramesFromHeader"),
264263
num_frames_from_content=stream_dict.get("numFramesFromContent"),
265264
average_fps_from_header=stream_dict.get("averageFpsFromHeader"),
266-
# sample_aspect_ratio is a tuple. Return None,
267-
# and not (None, None), if missing.
268-
sample_aspect_ratio=_get_optional_sar_tuple(stream_dict),
265+
sample_aspect_ratio=_get_optional_sar_fraction(stream_dict),
269266
**common_meta,
270267
)
271268
)

test/test_metadata.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# LICENSE file in the root directory of this source tree.
66

77
import functools
8+
from fractions import Fraction
89

910
import pytest
1011

@@ -138,7 +139,7 @@ def test_num_frames_fallback(
138139
width=123,
139140
height=321,
140141
average_fps_from_header=30,
141-
sample_aspect_ratio=(1, 1),
142+
sample_aspect_ratio=Fraction(1, 1),
142143
stream_index=0,
143144
)
144145

@@ -163,7 +164,7 @@ def test_repr():
163164
num_frames_from_header: 390
164165
num_frames_from_content: 390
165166
average_fps_from_header: 29.97003
166-
sample_aspect_ratio: (1, 1)
167+
sample_aspect_ratio: 1
167168
duration_seconds: 13.013
168169
begin_stream_seconds: 0.0
169170
end_stream_seconds: 13.013

0 commit comments

Comments
 (0)