1
0
Fork 0
mirror of https://github.com/DanielnetoDotCom/YouPHPTube synced 2025-10-03 01:39:24 +02:00
Daniel Neto 2025-01-08 14:36:17 -03:00
parent a3c03256e4
commit 068efceefa
2 changed files with 93 additions and 9 deletions

View file

@ -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";
}

View file

@ -38,63 +38,87 @@ function get_ffprobe()
function convertVideoToMP3FileIfNotExists($videos_id, $forceTry = 0) function convertVideoToMP3FileIfNotExists($videos_id, $forceTry = 0)
{ {
global $global; 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'])) { if (!empty($global['disableMP3'])) {
$global['convertVideoToMP3FileIfNotExistsSteps'][] = '$global[disableMP3] isset';
_error_log('convertVideoToMP3FileIfNotExists: $global[disableMP3] isset'); _error_log('convertVideoToMP3FileIfNotExists: $global[disableMP3] isset');
return false; return false;
} }
$video = Video::getVideoLight($videos_id); $video = Video::getVideoLight($videos_id);
if (empty($video)) { if (empty($video)) {
$global['convertVideoToMP3FileIfNotExistsSteps'][] = "videos_id=$videos_id not found";
_error_log("convertVideoToMP3FileIfNotExists: videos_id=$videos_id not found"); _error_log("convertVideoToMP3FileIfNotExists: videos_id=$videos_id not found");
return false; return false;
} }
$global['convertVideoToMP3FileIfNotExistsSteps'][] = "Video loaded successfully";
$types = [Video::$videoTypeVideo, Video::$videoTypeAudio]; $types = [Video::$videoTypeVideo, Video::$videoTypeAudio];
if (!in_array($video['type'], $types)) { if (!in_array($video['type'], $types)) {
$global['convertVideoToMP3FileIfNotExistsSteps'][] = "Invalid type: {$video['type']}";
_error_log("convertVideoToMP3FileIfNotExists: invalid type {$video['type']}"); _error_log("convertVideoToMP3FileIfNotExists: invalid type {$video['type']}");
return false; return false;
} }
$global['convertVideoToMP3FileIfNotExistsSteps'][] = "Valid video type: {$video['type']}";
$paths = Video::getPaths($video['filename']); $paths = Video::getPaths($video['filename']);
$mp3HLSFile = "{$paths['path']}index.mp3"; $mp3HLSFile = "{$paths['path']}index.mp3";
$mp3File = "{$paths['path']}{$video['filename']}.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 ($forceTry) {
if (file_exists($mp3HLSFile)) { if (file_exists($mp3HLSFile)) {
unlink($mp3HLSFile); unlink($mp3HLSFile);
$global['convertVideoToMP3FileIfNotExistsSteps'][] = "Force delete MP3 HLS File";
} }
if (file_exists($mp3File)) { if (file_exists($mp3File)) {
unlink($mp3File); unlink($mp3File);
$global['convertVideoToMP3FileIfNotExistsSteps'][] = "Force delete MP3 File";
} }
} }
if (file_exists($mp3HLSFile) || file_exists($mp3File)) { 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 { } else {
$f = convertVideoFileWithFFMPEGIsLockedInfo($mp3File); $f = convertVideoFileWithFFMPEGIsLockedInfo($mp3File);
if ($f['isUnlocked']) { if ($f['isUnlocked']) {
$global['convertVideoToMP3FileIfNotExistsSteps'][] = "Starting FFmpeg conversion";
_error_log("convertVideoToMP3FileIfNotExists: start videos_id=$videos_id try=$forceTry "); _error_log("convertVideoToMP3FileIfNotExists: start videos_id=$videos_id try=$forceTry ");
$sources = getVideosURLOnly($video['filename'], false); $sources = getVideosURLOnly($video['filename'], false);
if (!empty($sources)) { if (!empty($sources)) {
if (!empty($sources['m3u8'])) { $global['convertVideoToMP3FileIfNotExistsSteps'][] = "Sources found";
$source = $sources['m3u8']; $source = !empty($sources['m3u8']) ? $sources['m3u8'] : end($sources);
} else { $global['convertVideoToMP3FileIfNotExistsSteps'][] = "Using source URL: {$source['url']}";
$source = end($sources);
}
convertVideoFileWithFFMPEG($source['url'], $mp3File, '', $forceTry); convertVideoFileWithFFMPEG($source['url'], $mp3File, '', $forceTry);
if (file_exists($mp3File)) { if (file_exists($mp3File)) {
return Video::getSourceFile($video['filename'], ".mp3", true); $global['convertVideoToMP3FileIfNotExistsSteps'][] = "MP3 file successfully created";
return true; // Conversion successful
} else { } else {
$global['convertVideoToMP3FileIfNotExistsSteps'][] = "MP3 file creation failed: File does not exist";
_error_log("convertVideoToMP3FileIfNotExists: file not exists {$mp3File}"); _error_log("convertVideoToMP3FileIfNotExists: file not exists {$mp3File}");
} }
} else { } else {
$global['convertVideoToMP3FileIfNotExistsSteps'][] = "Sources not found";
_error_log("convertVideoToMP3FileIfNotExists: sources not found"); _error_log("convertVideoToMP3FileIfNotExists: sources not found");
} }
} else { } else {
$global['convertVideoToMP3FileIfNotExistsSteps'][] = "Conversion is locked";
_error_log("convertVideoToMP3FileIfNotExists: 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. * 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: * To schedule this function as a cron job, add the following line to the crontab file:
@ -503,7 +527,12 @@ function convertVideoFileWithFFMPEG($fromFileLocation, $toFileLocation, $logFile
return false; return false;
} }
$command .= " > {$logFile} 2>&1"; if(!empty($logFile)){
$command .= " > {$logFile} 2>&1";
}
_error_log("convertVideoFileWithFFMPEG: Command start $command");
exec($command, $output, $return); exec($command, $output, $return);
_error_log("convertVideoFileWithFFMPEG: Command executed with return code {$return}"); _error_log("convertVideoFileWithFFMPEG: Command executed with return code {$return}");