Skip to content

Commit 3b1ca86

Browse files
committed
fix for ffmpeg not correctly reading width and height ratios of a rotated video when the pixel aspect ratio says otherwise.
1 parent a1119dd commit 3b1ca86

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

src/PHPVideoToolkit/MediaParser.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -308,12 +308,31 @@ public function getFileVideoComponent($file_path, $read_from_cache=true)
308308
$data['language'] = $lang_matches[1];
309309
}
310310

311+
// get the ratios
312+
if(preg_match('/\[[P|S]AR\s+([0-9\:\.]+)\s+DAR\s+([0-9\:\.]+)\]/', $matches[0], $ratio_matches) > 0)
313+
{
314+
$data['pixel_aspect_ratio'] = $ratio_matches[1];
315+
$data['display_aspect_ratio'] = $ratio_matches[2];
316+
}
317+
311318
// get the dimension parts
312319
if(preg_match('/([1-9][0-9]*)x([1-9][0-9]*)/', $matches[2], $dimensions_matches) > 0)
313320
{
321+
// this is a special fix for correctly reading width and height from an ffmpeg rotated video.
322+
// not entirely sure if this is the same across the board for every video or just ffmpeg rotated videos.
323+
$dimensions = $dimensions_matches;
324+
if($data['pixel_aspect_ratio'] !== null)
325+
{
326+
$pixel_ratio = explode(':', $data['pixel_aspect_ratio'], 2);
327+
if($pixel_ratio[0] < $pixel_ratio[1] && $dimensions_matches[1] > $dimensions_matches[2])
328+
{
329+
$dimensions[1] = $dimensions_matches[2];
330+
$dimensions[2] = $dimensions_matches[1];
331+
}
332+
}
314333
$data['dimensions'] = array(
315-
'width' => (float) $dimensions_matches[1],
316-
'height' => (float) $dimensions_matches[2],
334+
'width' => (float) $dimensions[1],
335+
'height' => (float) $dimensions[2],
317336
);
318337
}
319338
$dimension_match = $dimensions_matches[0];
@@ -342,13 +361,6 @@ public function getFileVideoComponent($file_path, $read_from_cache=true)
342361
$data['frames']['total'] = $duration_timecode !== null ? ceil($duration_timecode->seconds * $data['frames']['rate']) : null;
343362
}
344363

345-
// get the ratios
346-
if(preg_match('/\[[P|S]AR\s+([0-9\:\.]+)\s+DAR\s+([0-9\:\.]+)\]/', $matches[0], $ratio_matches) > 0)
347-
{
348-
$data['pixel_aspect_ratio'] = $ratio_matches[1];
349-
$data['display_aspect_ratio'] = $ratio_matches[2];
350-
}
351-
352364
// get the bit rate
353365
if(preg_match('/([0-9]{1,4})\s+(kb|mb)\/s/i', $matches[0], $bitrate_matches) > 0)
354366
{

0 commit comments

Comments
 (0)