Skip to content

Commit 9c69552

Browse files
author
Daniel Neto
committed
Now the MP4 and MP3 files if is automatic, is processed In the encoder for HLS files only
1 parent 265699b commit 9c69552

File tree

5 files changed

+142
-2
lines changed

5 files changed

+142
-2
lines changed

objects/Encoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1259,7 +1259,7 @@ static function canEncodeNow()
12591259

12601260
public static function run($try = 0)
12611261
{
1262-
global $global;
1262+
global $global, $advancedCustom;
12631263
$maxTries = 4;
12641264
$lockFile = sys_get_temp_dir() . '/encoder_run.lock';
12651265

objects/Format.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ protected static function getFromOrder($order)
113113
}
114114
return $row;
115115
}
116-
116+
117117
public function run($pathFileName, $encoder_queue_id)
118118
{
119119
_error_log("AVideo-Encoder Format::run($pathFileName, $encoder_queue_id) " . json_encode(debug_backtrace()));

objects/HLSProcessor.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,32 @@
22

33
class HLSProcessor
44
{
5+
6+
static function createMP3AndPM4IfNeed($pathFileName, $destinationFile){
7+
global $global;
8+
9+
$advancedCustom = getAdvancedCustomizedObjectData();
10+
//_error_log('createMP3AndPM4IfNeed '.json_encode($advancedCustom));
11+
if($advancedCustom->autoConvertToMp4){
12+
require_once __DIR__.'/MP4Processor.php';
13+
try {
14+
MP4Processor::createMP4($pathFileName, $destinationFile.'index.mp4');
15+
} catch (Exception $e) {
16+
_error_log("Error creating MP4: " . $e->getMessage());
17+
}
18+
19+
}
20+
if($advancedCustom->autoConvertVideosToMP3){
21+
require_once __DIR__.'/MP3Processor.php';
22+
// Usage example
23+
try {
24+
MP3Processor::createMP3($pathFileName, $destinationFile.'index.mp3');
25+
} catch (Exception $e) {
26+
_error_log("Error creating MP3: " . $e->getMessage());
27+
}
28+
}
29+
}
30+
531
public static function createHLSWithAudioTracks($pathFileName, $destinationFile)
632
{
733
// Detect video resolution and audio tracks
@@ -44,6 +70,8 @@ public static function createHLSWithAudioTracks($pathFileName, $destinationFile)
4470
$masterPlaylist = "#EXTM3U" . PHP_EOL;
4571
$masterPlaylist .= "#EXT-X-VERSION:3" . PHP_EOL;
4672

73+
self::createMP3AndPM4IfNeed($pathFileName, $destinationFile);
74+
4775
// Generate separate audio-only HLS streams for each audio track
4876
foreach ($audioTracks as $key => $track) {
4977
$language = isset($track->language) ? $track->language : "lang" . ($track->index + 1); // Assign language name, customize as needed

objects/MP3Processor.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
class MP3Processor
4+
{
5+
public static function createMP3($pathFileName, $destinationFile)
6+
{
7+
// Define encoding settings for MP3
8+
$audioBitrate = 128; // Set a standard bitrate for MP3 encoding
9+
10+
// Generate the FFmpeg command for creating an MP3 file
11+
$command = self::generateFFmpegCommand($pathFileName, $destinationFile, $audioBitrate);
12+
13+
// Execute the FFmpeg command
14+
_error_log("MP3Processor: Executing FFmpeg command: $command");
15+
exec($command, $output, $resultCode);
16+
17+
if ($resultCode !== 0) {
18+
_error_log("MP3Processor: FFmpeg failed with output: " . json_encode($output));
19+
throw new Exception("Failed to create MP3 file.");
20+
}
21+
22+
_error_log("MP3Processor: MP3 file created successfully at $destinationFile");
23+
}
24+
25+
private static function generateFFmpegCommand($inputFile, $outputFile, $audioBitrate)
26+
{
27+
$ffmpeg = get_ffmpeg() . " -i $inputFile " .
28+
"-vn -c:a libmp3lame -b:a {$audioBitrate}k " .
29+
"-movflags +faststart " .
30+
"$outputFile";
31+
return removeUserAgentIfNotURL($ffmpeg);
32+
}
33+
}

objects/MP4Processor.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
class MP4Processor
4+
{
5+
public static function createMP4($pathFileName, $destinationFile)
6+
{
7+
// Get allowed resolutions from Format::ENCODING_SETTINGS
8+
$allowedResolutions = array_keys(Format::ENCODING_SETTINGS);
9+
10+
// Get the resolution of the input file
11+
$inputResolution = self::getResolution($pathFileName);
12+
13+
// Determine the target resolution
14+
$targetResolution = self::getClosestResolution($inputResolution, $allowedResolutions);
15+
16+
if ($targetResolution === null) {
17+
throw new Exception("No valid resolution found for the input file.");
18+
}
19+
20+
// Load encoding settings for the target resolution
21+
$encodingSettings = Format::ENCODING_SETTINGS[$targetResolution];
22+
23+
// Create the FFmpeg command
24+
$command = self::generateFFmpegCommand(
25+
$pathFileName,
26+
$destinationFile,
27+
$targetResolution,
28+
$encodingSettings
29+
);
30+
31+
// Execute the FFmpeg command
32+
_error_log("MP4Processor: Executing FFmpeg command: $command");
33+
exec($command, $output, $resultCode);
34+
35+
if ($resultCode !== 0) {
36+
_error_log("MP4Processor: FFmpeg failed with output: " . json_encode($output));
37+
throw new Exception("Failed to create MP4 file.");
38+
}
39+
40+
_error_log("MP4Processor: MP4 file created successfully at $destinationFile");
41+
}
42+
43+
private static function getResolution($pathFileName)
44+
{
45+
$command = get_ffprobe() . " -v error -select_streams v:0 -show_entries stream=height -of csv=p=0 $pathFileName";
46+
return (int) shell_exec($command);
47+
}
48+
49+
private static function getClosestResolution($inputResolution, $allowedResolutions)
50+
{
51+
// Sort resolutions in descending order
52+
rsort($allowedResolutions);
53+
54+
foreach ($allowedResolutions as $resolution) {
55+
if ($inputResolution >= $resolution) {
56+
return $resolution;
57+
}
58+
}
59+
60+
// Return the lowest resolution if no match found
61+
return $allowedResolutions[count($allowedResolutions) - 1] ?? null;
62+
}
63+
64+
private static function generateFFmpegCommand($inputFile, $outputFile, $resolution, $encodingSettings)
65+
{
66+
$ffmpeg = get_ffmpeg() . " -i $inputFile " .
67+
"-vf scale=-2:$resolution " .
68+
"-b:v {$encodingSettings['maxrate']}k " .
69+
"-minrate {$encodingSettings['minrate']}k " .
70+
"-maxrate {$encodingSettings['maxrate']}k " .
71+
"-bufsize {$encodingSettings['bufsize']}k " .
72+
"-c:v h264 -pix_fmt yuv420p " .
73+
"-c:a aac -b:a {$encodingSettings['audioBitrate']}k " .
74+
"-movflags +faststart " .
75+
"$outputFile";
76+
77+
return removeUserAgentIfNotURL($ffmpeg);
78+
}
79+
}

0 commit comments

Comments
 (0)