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

[mob][photos] Consider sample aspect ratio or pixel aspect ratio when parsing width and height of video used for calculating aspect ratio #3126

Merged
merged 1 commit into from
Sep 4, 2024
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
1 change: 1 addition & 0 deletions mobile/lib/models/ffmpeg/ffprobe_keys.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class FFProbeKeys {
static const sideDataList = 'side_data_list';
static const rotation = 'rotation';
static const sideDataType = 'side_data_type';
static const sampleAspectRatio = 'sample_aspect_ratio';
}

class MediaStreamTypes {
Expand Down
33 changes: 25 additions & 8 deletions mobile/lib/models/ffmpeg/ffprobe_props.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,31 @@ class FFProbeProps {

int? get width {
if (_width == null || _height == null) return null;
final intWidth = int.tryParse(_width!);
if (_rotation == null) {
return intWidth;
} else {
if ((_rotation! ~/ 90).isEven) {
return intWidth;
} else {
return int.tryParse(_height!);
int? finalWidth = int.tryParse(_width!);
if (propData?[FFProbeKeys.sampleAspectRatio] != null &&
finalWidth != null) {
finalWidth = _calculateWidthConsideringSAR(finalWidth);
}
if (_rotation != null) {
if ((_rotation! ~/ 90).isOdd) {
finalWidth = int.tryParse(_height!);
}
}
return finalWidth;
}

/// To know more, read about Sample Aspect Ratio (SAR), Display Aspect Ratio (DAR)
/// and Pixel Aspect Ratio (PAR)
int _calculateWidthConsideringSAR(int width) {
final List<String> sar =
propData![FFProbeKeys.sampleAspectRatio].toString().split(":");
if (sar.length == 2) {
final int sarWidth = int.tryParse(sar[0]) ?? 1;
final int sarHeight = int.tryParse(sar[1]) ?? 1;
return (width * (sarWidth / sarHeight)).toInt();
} else {
return width;
}
}

int? get height {
Expand Down Expand Up @@ -202,6 +217,8 @@ class FFProbeProps {
parsedData[FFProbeKeys.rotation] = result._rotation;
}
}
} else if (key == FFProbeKeys.sampleAspectRatio) {
parsedData[key] = stream[key];
}
}
}
Expand Down