Skip to content

Commit 096a60b

Browse files
author
Daniel Neto
committed
fallback to default 16:9 width if FFprobe fails or returns invalid dimensions
1 parent b371f3a commit 096a60b

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

objects/HLSProcessor.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,28 @@ private static function getAudioTracks($pathFileName)
227227
private static function getScaledWidth($pathFileName, $targetHeight)
228228
{
229229
$command = get_ffprobe() . " -v error -select_streams v:0 -show_entries stream=width,height -of csv=p=0:s=x {$pathFileName}";
230-
$output = shell_exec($command);
231-
list($originalWidth, $originalHeight) = explode('x', trim($output));
230+
$output = trim(shell_exec($command));
232231

233-
// Calculate proportional width based on the target height
234-
$width = intval(($targetHeight / $originalHeight) * $originalWidth);
232+
if (empty($output) || strpos($output, 'x') === false) {
233+
_error_log("FFprobe failed to get dimensions: output=[$output], using default 16:9 fallback");
234+
return self::getDefaultWidth($targetHeight);
235+
}
236+
237+
list($originalWidth, $originalHeight) = explode('x', $output);
238+
239+
if (empty($originalWidth) || empty($originalHeight) || $originalHeight == 0) {
240+
_error_log("Invalid dimensions received: width=[$originalWidth], height=[$originalHeight], using default 16:9 fallback");
241+
return self::getDefaultWidth($targetHeight);
242+
}
235243

236-
// Round down to the nearest multiple of 2 (required by H.264 codec)
244+
$width = intval(($targetHeight / $originalHeight) * $originalWidth);
237245
return $width - ($width % 2);
238246
}
247+
248+
private static function getDefaultWidth($targetHeight)
249+
{
250+
// 16:9 ratio → width = height * (16 / 9)
251+
$width = intval($targetHeight * (16 / 9));
252+
return $width - ($width % 2); // ensure even number
253+
}
239254
}

0 commit comments

Comments
 (0)