Skip to content

Commit

Permalink
https://github.com/WWBN/AVideo/issues/9760
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Neto committed Jan 8, 2025
1 parent a3c0325 commit 068efce
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 9 deletions.
55 changes: 55 additions & 0 deletions install/getMp3FromVideo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
// Streamer config
require_once __DIR__.'/../videos/configuration.php';
ob_end_flush();
if (!isCommandLineInterface()) {
die('Command Line only');
}
$global['printLogs'] = 1;
// Prompt user for video ID
echo "Enter the video ID: ";
$videos_id = trim(fgets(STDIN));

// Validate input
if (!is_numeric($videos_id) || $videos_id <= 0) {
die("Invalid video ID. Please enter a valid positive integer.\n");
}

// Load the video
$video = new Video('', '', $videos_id);

if ($video->getType() === Video::$videoTypeVideo) {
global $global;
$global['convertVideoToMP3FileIfNotExistsSteps'] = []; // Initialize steps tracker
$global['convertVideoToMP3FileIfNotExistsFileAlreadyExists'] = false; // Reset global tracker
$global['convertVideoToMP3FilePath'] = ''; // Reset global file path

// Attempt to convert the video to MP3
$converted = convertVideoToMP3FileIfNotExists($videos_id);

if ($global['convertVideoToMP3FileIfNotExistsFileAlreadyExists']) {
echo "MP3 file already exists at: {$global['convertVideoToMP3FilePath']}\n";
echo "Do you want to remove and try again? (y/n): ";
$response = trim(fgets(STDIN));
if (strtolower($response) === 'y') {
// Force conversion
$converted = convertVideoToMP3FileIfNotExists($videos_id, 1);
}
}

// Print the results
echo "Conversion Results for Video ID {$videos_id}:\n";

echo $converted ? "Conversion successful!\n" : "Conversion failed.\n";

// Print the tracked steps
echo "Execution Steps:\n";
foreach ($global['convertVideoToMP3FileIfNotExistsSteps'] as $step) {
echo "- {$step}\n";
}

$duration = getDurationFromFile($global['convertVideoToMP3FilePath']);
echo "MP3 Duration: {$duration}\n";
} else {
echo "The specified video is not of type 'Video'. No conversion attempted.\n";
}
47 changes: 38 additions & 9 deletions objects/functionsFFMPEG.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,63 +38,87 @@ function get_ffprobe()
function convertVideoToMP3FileIfNotExists($videos_id, $forceTry = 0)
{
global $global;
$global['convertVideoToMP3FileIfNotExistsSteps'] = []; // Initialize an array to track steps
$global['convertVideoToMP3FileIfNotExistsFileAlreadyExists'] = false; // Track if MP3 file already exists
$global['convertVideoToMP3FilePath'] = ''; // Store the MP3 file path

if (!empty($global['disableMP3'])) {
$global['convertVideoToMP3FileIfNotExistsSteps'][] = '$global[disableMP3] isset';
_error_log('convertVideoToMP3FileIfNotExists: $global[disableMP3] isset');
return false;
}

$video = Video::getVideoLight($videos_id);
if (empty($video)) {
$global['convertVideoToMP3FileIfNotExistsSteps'][] = "videos_id=$videos_id not found";
_error_log("convertVideoToMP3FileIfNotExists: videos_id=$videos_id not found");
return false;
}
$global['convertVideoToMP3FileIfNotExistsSteps'][] = "Video loaded successfully";

$types = [Video::$videoTypeVideo, Video::$videoTypeAudio];
if (!in_array($video['type'], $types)) {
$global['convertVideoToMP3FileIfNotExistsSteps'][] = "Invalid type: {$video['type']}";
_error_log("convertVideoToMP3FileIfNotExists: invalid type {$video['type']}");
return false;
}
$global['convertVideoToMP3FileIfNotExistsSteps'][] = "Valid video type: {$video['type']}";

$paths = Video::getPaths($video['filename']);
$mp3HLSFile = "{$paths['path']}index.mp3";
$mp3File = "{$paths['path']}{$video['filename']}.mp3";
$global['convertVideoToMP3FilePath'] = $mp3File; // Set the global variable for MP3 file path
$global['convertVideoToMP3FileIfNotExistsSteps'][] = "Paths set: MP3 HLS File={$mp3HLSFile}, MP3 File={$mp3File}";

if ($forceTry) {
if (file_exists($mp3HLSFile)) {
unlink($mp3HLSFile);
$global['convertVideoToMP3FileIfNotExistsSteps'][] = "Force delete MP3 HLS File";
}
if (file_exists($mp3File)) {
unlink($mp3File);
$global['convertVideoToMP3FileIfNotExistsSteps'][] = "Force delete MP3 File";
}
}

if (file_exists($mp3HLSFile) || file_exists($mp3File)) {
return Video::getSourceFile($video['filename'], ".mp3", true);
$global['convertVideoToMP3FileIfNotExistsSteps'][] = "MP3 file already exists";
$global['convertVideoToMP3FileIfNotExistsFileAlreadyExists'] = true; // Indicate that the file already exists
return true; // Treat as successful since the file exists
} else {
$f = convertVideoFileWithFFMPEGIsLockedInfo($mp3File);
if ($f['isUnlocked']) {
$global['convertVideoToMP3FileIfNotExistsSteps'][] = "Starting FFmpeg conversion";
_error_log("convertVideoToMP3FileIfNotExists: start videos_id=$videos_id try=$forceTry ");
$sources = getVideosURLOnly($video['filename'], false);

if (!empty($sources)) {
if (!empty($sources['m3u8'])) {
$source = $sources['m3u8'];
} else {
$source = end($sources);
}
$global['convertVideoToMP3FileIfNotExistsSteps'][] = "Sources found";
$source = !empty($sources['m3u8']) ? $sources['m3u8'] : end($sources);
$global['convertVideoToMP3FileIfNotExistsSteps'][] = "Using source URL: {$source['url']}";
convertVideoFileWithFFMPEG($source['url'], $mp3File, '', $forceTry);

if (file_exists($mp3File)) {
return Video::getSourceFile($video['filename'], ".mp3", true);
$global['convertVideoToMP3FileIfNotExistsSteps'][] = "MP3 file successfully created";
return true; // Conversion successful
} else {
$global['convertVideoToMP3FileIfNotExistsSteps'][] = "MP3 file creation failed: File does not exist";
_error_log("convertVideoToMP3FileIfNotExists: file not exists {$mp3File}");
}
} else {
$global['convertVideoToMP3FileIfNotExistsSteps'][] = "Sources not found";
_error_log("convertVideoToMP3FileIfNotExists: sources not found");
}
} else {
$global['convertVideoToMP3FileIfNotExistsSteps'][] = "Conversion is locked";
_error_log("convertVideoToMP3FileIfNotExists: is locked");
}
return false;
return false; // Conversion failed
}
}



/**
* Cleans up the specified directory by deleting files that do not match the given resolution pattern.
* To schedule this function as a cron job, add the following line to the crontab file:
Expand Down Expand Up @@ -503,7 +527,12 @@ function convertVideoFileWithFFMPEG($fromFileLocation, $toFileLocation, $logFile
return false;
}

$command .= " > {$logFile} 2>&1";
if(!empty($logFile)){
$command .= " > {$logFile} 2>&1";
}

_error_log("convertVideoFileWithFFMPEG: Command start $command");

exec($command, $output, $return);

_error_log("convertVideoFileWithFFMPEG: Command executed with return code {$return}");
Expand Down

0 comments on commit 068efce

Please sign in to comment.