Skip to content

Commit

Permalink
Now the MP4 and MP3 files if is automatic, is processed In the encode…
Browse files Browse the repository at this point in the history
…r for HLS files only
  • Loading branch information
Daniel Neto committed Jan 16, 2025
1 parent 265699b commit 9c69552
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 2 deletions.
2 changes: 1 addition & 1 deletion objects/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1259,7 +1259,7 @@ static function canEncodeNow()

public static function run($try = 0)
{
global $global;
global $global, $advancedCustom;
$maxTries = 4;
$lockFile = sys_get_temp_dir() . '/encoder_run.lock';

Expand Down
2 changes: 1 addition & 1 deletion objects/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ protected static function getFromOrder($order)
}
return $row;
}

public function run($pathFileName, $encoder_queue_id)
{
_error_log("AVideo-Encoder Format::run($pathFileName, $encoder_queue_id) " . json_encode(debug_backtrace()));
Expand Down
28 changes: 28 additions & 0 deletions objects/HLSProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,32 @@

class HLSProcessor
{

static function createMP3AndPM4IfNeed($pathFileName, $destinationFile){
global $global;

$advancedCustom = getAdvancedCustomizedObjectData();
//_error_log('createMP3AndPM4IfNeed '.json_encode($advancedCustom));
if($advancedCustom->autoConvertToMp4){
require_once __DIR__.'/MP4Processor.php';
try {
MP4Processor::createMP4($pathFileName, $destinationFile.'index.mp4');
} catch (Exception $e) {
_error_log("Error creating MP4: " . $e->getMessage());
}

}
if($advancedCustom->autoConvertVideosToMP3){
require_once __DIR__.'/MP3Processor.php';
// Usage example
try {
MP3Processor::createMP3($pathFileName, $destinationFile.'index.mp3');
} catch (Exception $e) {
_error_log("Error creating MP3: " . $e->getMessage());
}
}
}

public static function createHLSWithAudioTracks($pathFileName, $destinationFile)
{
// Detect video resolution and audio tracks
Expand Down Expand Up @@ -44,6 +70,8 @@ public static function createHLSWithAudioTracks($pathFileName, $destinationFile)
$masterPlaylist = "#EXTM3U" . PHP_EOL;
$masterPlaylist .= "#EXT-X-VERSION:3" . PHP_EOL;

self::createMP3AndPM4IfNeed($pathFileName, $destinationFile);

// Generate separate audio-only HLS streams for each audio track
foreach ($audioTracks as $key => $track) {
$language = isset($track->language) ? $track->language : "lang" . ($track->index + 1); // Assign language name, customize as needed
Expand Down
33 changes: 33 additions & 0 deletions objects/MP3Processor.php
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);
}
}
79 changes: 79 additions & 0 deletions objects/MP4Processor.php
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);
}
}

0 comments on commit 9c69552

Please sign in to comment.