-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Now the MP4 and MP3 files if is automatic, is processed In the encode…
…r for HLS files only
- Loading branch information
Daniel Neto
committed
Jan 16, 2025
1 parent
265699b
commit 9c69552
Showing
5 changed files
with
142 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
class MP3Processor | ||
{ | ||
public static function createMP3($pathFileName, $destinationFile) | ||
{ | ||
// Define encoding settings for MP3 | ||
$audioBitrate = 128; // Set a standard bitrate for MP3 encoding | ||
|
||
// Generate the FFmpeg command for creating an MP3 file | ||
$command = self::generateFFmpegCommand($pathFileName, $destinationFile, $audioBitrate); | ||
|
||
// Execute the FFmpeg command | ||
_error_log("MP3Processor: Executing FFmpeg command: $command"); | ||
exec($command, $output, $resultCode); | ||
|
||
if ($resultCode !== 0) { | ||
_error_log("MP3Processor: FFmpeg failed with output: " . json_encode($output)); | ||
throw new Exception("Failed to create MP3 file."); | ||
} | ||
|
||
_error_log("MP3Processor: MP3 file created successfully at $destinationFile"); | ||
} | ||
|
||
private static function generateFFmpegCommand($inputFile, $outputFile, $audioBitrate) | ||
{ | ||
$ffmpeg = get_ffmpeg() . " -i $inputFile " . | ||
"-vn -c:a libmp3lame -b:a {$audioBitrate}k " . | ||
"-movflags +faststart " . | ||
"$outputFile"; | ||
return removeUserAgentIfNotURL($ffmpeg); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
class MP4Processor | ||
{ | ||
public static function createMP4($pathFileName, $destinationFile) | ||
{ | ||
// Get allowed resolutions from Format::ENCODING_SETTINGS | ||
$allowedResolutions = array_keys(Format::ENCODING_SETTINGS); | ||
|
||
// Get the resolution of the input file | ||
$inputResolution = self::getResolution($pathFileName); | ||
|
||
// Determine the target resolution | ||
$targetResolution = self::getClosestResolution($inputResolution, $allowedResolutions); | ||
|
||
if ($targetResolution === null) { | ||
throw new Exception("No valid resolution found for the input file."); | ||
} | ||
|
||
// Load encoding settings for the target resolution | ||
$encodingSettings = Format::ENCODING_SETTINGS[$targetResolution]; | ||
|
||
// Create the FFmpeg command | ||
$command = self::generateFFmpegCommand( | ||
$pathFileName, | ||
$destinationFile, | ||
$targetResolution, | ||
$encodingSettings | ||
); | ||
|
||
// Execute the FFmpeg command | ||
_error_log("MP4Processor: Executing FFmpeg command: $command"); | ||
exec($command, $output, $resultCode); | ||
|
||
if ($resultCode !== 0) { | ||
_error_log("MP4Processor: FFmpeg failed with output: " . json_encode($output)); | ||
throw new Exception("Failed to create MP4 file."); | ||
} | ||
|
||
_error_log("MP4Processor: MP4 file created successfully at $destinationFile"); | ||
} | ||
|
||
private static function getResolution($pathFileName) | ||
{ | ||
$command = get_ffprobe() . " -v error -select_streams v:0 -show_entries stream=height -of csv=p=0 $pathFileName"; | ||
return (int) shell_exec($command); | ||
} | ||
|
||
private static function getClosestResolution($inputResolution, $allowedResolutions) | ||
{ | ||
// Sort resolutions in descending order | ||
rsort($allowedResolutions); | ||
|
||
foreach ($allowedResolutions as $resolution) { | ||
if ($inputResolution >= $resolution) { | ||
return $resolution; | ||
} | ||
} | ||
|
||
// Return the lowest resolution if no match found | ||
return $allowedResolutions[count($allowedResolutions) - 1] ?? null; | ||
} | ||
|
||
private static function generateFFmpegCommand($inputFile, $outputFile, $resolution, $encodingSettings) | ||
{ | ||
$ffmpeg = get_ffmpeg() . " -i $inputFile " . | ||
"-vf scale=-2:$resolution " . | ||
"-b:v {$encodingSettings['maxrate']}k " . | ||
"-minrate {$encodingSettings['minrate']}k " . | ||
"-maxrate {$encodingSettings['maxrate']}k " . | ||
"-bufsize {$encodingSettings['bufsize']}k " . | ||
"-c:v h264 -pix_fmt yuv420p " . | ||
"-c:a aac -b:a {$encodingSettings['audioBitrate']}k " . | ||
"-movflags +faststart " . | ||
"$outputFile"; | ||
|
||
return removeUserAgentIfNotURL($ffmpeg); | ||
} | ||
} |