|
8 | 8 | import json |
9 | 9 | import pathlib |
10 | 10 | from dataclasses import dataclass |
| 11 | +from fractions import Fraction |
11 | 12 | from typing import List, Optional, Union |
12 | 13 |
|
13 | 14 | import torch |
@@ -80,12 +81,10 @@ class VideoStreamMetadata(StreamMetadata): |
80 | 81 | average_fps_from_header: Optional[float] |
81 | 82 | """Averate fps of the stream, obtained from the header (float or None). |
82 | 83 | We recommend using the ``average_fps`` attribute instead.""" |
83 | | - sample_aspect_ratio: Optional[tuple[int, int]] |
| 84 | + sample_aspect_ratio: Optional[Fraction] |
84 | 85 | """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).""" |
89 | 88 |
|
90 | 89 | @property |
91 | 90 | def duration_seconds(self) -> Optional[float]: |
@@ -217,9 +216,9 @@ def best_audio_stream(self) -> AudioStreamMetadata: |
217 | 216 | return metadata |
218 | 217 |
|
219 | 218 |
|
220 | | -def _get_optional_sar_tuple(stream_dict): |
| 219 | +def _get_optional_par_fraction(stream_dict): |
221 | 220 | try: |
222 | | - return ( |
| 221 | + return Fraction( |
223 | 222 | stream_dict["sampleAspectRatioNum"], |
224 | 223 | stream_dict["sampleAspectRatioDen"], |
225 | 224 | ) |
@@ -263,9 +262,7 @@ def get_container_metadata(decoder: torch.Tensor) -> ContainerMetadata: |
263 | 262 | num_frames_from_header=stream_dict.get("numFramesFromHeader"), |
264 | 263 | num_frames_from_content=stream_dict.get("numFramesFromContent"), |
265 | 264 | 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), |
269 | 266 | **common_meta, |
270 | 267 | ) |
271 | 268 | ) |
|
0 commit comments