Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Neto committed Aug 12, 2024
1 parent 91e74d3 commit 7aa8132
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 22 deletions.
70 changes: 50 additions & 20 deletions objects/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -589,25 +589,29 @@ private static function setDownloaded($queue_id, $filePath)
return $encoder->save();
}

public static function getYoutubeDl($videoURL, $queue_id, $destinationFile)
public static function getYoutubeDl($videoURL, $queue_id, $destinationFile, $addOauthFromProvider = '')
{
global $global;
$videoURL = escapeshellarg($videoURL);
$tmpfname = _get_temp_file('youtubeDl');
//$cmd = "youtube-dl -o {$tmpfname}.mp4 -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4' {$videoURL}";
$cmd = self::getYouTubeDLCommand() . " --no-check-certificate --force-ipv4 --no-playlist -k -o {$tmpfname}.mp4 -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4' {$videoURL}";
$cmd = self::getYouTubeDLCommand($addOauthFromProvider) . " --no-check-certificate --force-ipv4 --no-playlist -k -o {$tmpfname}.mp4 -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4' {$videoURL}";
//echo "\n**Trying Youtube DL **".$cmd;
$progressFile = "{$global['systemRootPath']}videos/{$queue_id}_tmpFile_downloadProgress.txt";
_error_log("getYoutubeDl: Getting from Youtube DL {$cmd} progressFile={$progressFile} " . json_encode(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)));

exec($cmd . " 1> {$progressFile} 2>&1", $output, $return_val);
_error_log("getYoutubeDl: Getting from Youtube DL {$cmd} done {$progressFile} ");
if ($return_val !== 0) {

if(empty($addOauthFromProvider) && self::hasSigninError($output)){
return self::getDescriptionFromLink($videoURL, $queue_id, $destinationFile, 'youtube');
}
//echo "\n**ERROR Youtube DL **".$code . "\n" . print_r($output, true);
$error = $cmd . PHP_EOL . print_r($output, true);
_error_log($error);
self::setStreamerLog($queue_id, 'Fail to download line=' . __LINE__ . ' ' . $error, Encoder::LOG_TYPE_ERROR);
$cmd = self::getYouTubeDLCommand() . " --no-check-certificate --force-ipv4 --no-playlist -k -o {$tmpfname}.mp4 {$videoURL}";
$cmd = self::getYouTubeDLCommand($addOauthFromProvider) . " --no-check-certificate --force-ipv4 --no-playlist -k -o {$tmpfname}.mp4 {$videoURL}";
//echo "\n**Trying Youtube DL **".$cmd;
_error_log("getYoutubeDl: Getting from Youtube other option DL {$cmd}");
exec($cmd . " 1> {$global['systemRootPath']}videos/{$queue_id}_tmpFile_downloadProgress.txt 2>&1", $output, $return_val);
Expand All @@ -616,7 +620,7 @@ public static function getYoutubeDl($videoURL, $queue_id, $destinationFile)
$error = $cmd . PHP_EOL . print_r($output, true);
_error_log($error);
self::setStreamerLog($queue_id, 'Fail to download line=' . __LINE__ . ' ' . $error, Encoder::LOG_TYPE_ERROR);
$cmd = self::getYouTubeDLCommand() . " --no-check-certificate --no-playlist -k -o {$tmpfname}.mp4 {$videoURL}";
$cmd = self::getYouTubeDLCommand($addOauthFromProvider) . " --no-check-certificate --no-playlist -k -o {$tmpfname}.mp4 {$videoURL}";
//echo "\n**Trying Youtube DL **".$cmd;
_error_log("getYoutubeDl: Getting from Youtube other option DL {$cmd}");
exec($cmd . " 1> {$global['systemRootPath']}videos/{$queue_id}_tmpFile_downloadProgress.txt 2>&1", $output, $return_val);
Expand All @@ -625,7 +629,7 @@ public static function getYoutubeDl($videoURL, $queue_id, $destinationFile)
$error = $cmd . PHP_EOL . print_r($output, true);
_error_log($error);
self::setStreamerLog($queue_id, 'Fail to download line=' . __LINE__ . ' ' . $error, Encoder::LOG_TYPE_ERROR);
$cmd = self::getYouTubeDLCommand() . " --no-check-certificate --force-ipv4 --no-playlist -k -o '{$tmpfname}.%(ext)s' {$videoURL}";
$cmd = self::getYouTubeDLCommand($addOauthFromProvider) . " --no-check-certificate --force-ipv4 --no-playlist -k -o '{$tmpfname}.%(ext)s' {$videoURL}";
//echo "\n**Trying Youtube DL **".$cmd;
_error_log("getYoutubeDl: Getting from Youtube other option DL {$cmd}");
exec($cmd . " 1> {$global['systemRootPath']}videos/{$queue_id}_tmpFile_downloadProgress.txt 2>&1", $output, $return_val);
Expand Down Expand Up @@ -2547,18 +2551,30 @@ public static function getReverseVideosJsonListFromLink($link)
}
}

public static function getTitleFromLink($link)
static function hasSigninError($output){
foreach ($output as $value) {
if(preg_match('/Sign in to confirm/i', $value)){
return true;
}
}
return false;
}

public static function getTitleFromLink($link, $addOauthFromProvider = '')
{
$prepend = '';
if (!isWindows()) {
$prepend = 'LC_ALL=en_US.UTF-8 ';
}
$link = escapeshellarg($link);
$response = array('error' => true, 'output' => array());
$cmd = $prepend . self::getYouTubeDLCommand() . " --no-check-certificate --no-playlist --force-ipv4 --skip-download -e {$link}";
$cmd = $prepend . self::getYouTubeDLCommand($addOauthFromProvider) . " --no-check-certificate --no-playlist --force-ipv4 --skip-download -e {$link}";
exec($cmd . " 2>&1", $output, $return_val);
if ($return_val !== 0) {
_error_log("getTitleFromLink: Get Title Error: $cmd \n" . print_r($output, true));
if(empty($addOauthFromProvider) && self::hasSigninError($output)){
return self::getTitleFromLink($link, 'youtube');
}
$response['output'] = $output;
} else {
_error_log("getTitleFromLink: Get Title: $cmd \n" . print_r($output, true));
Expand All @@ -2569,12 +2585,15 @@ public static function getTitleFromLink($link)
return $response;
}

public static function getDurationFromLink($link)
public static function getDurationFromLink($link, $addOauthFromProvider = '')
{
$link = escapeshellarg($link);
$cmd = self::getYouTubeDLCommand() . " --no-check-certificate --no-playlist --force-ipv4 --get-duration --skip-download {$link}";
$cmd = self::getYouTubeDLCommand($addOauthFromProvider) . " --no-check-certificate --no-playlist --force-ipv4 --get-duration --skip-download {$link}";
exec($cmd . " 2>&1", $output, $return_val);
if ($return_val !== 0) {
if(empty($addOauthFromProvider) && self::hasSigninError($output)){
return self::getDurationFromLink($link, 'youtube');
}
return false;
} else {
$line = end($output);
Expand All @@ -2587,18 +2606,21 @@ public static function getDurationFromLink($link)
}
}

public static function getThumbsFromLink($link, $returnFileName = false)
public static function getThumbsFromLink($link, $returnFileName = false, $addOauthFromProvider = '')
{
$link = str_replace(array('"', "'"), array('', ''), $link);
$link = escapeshellarg($link);

$tmpfname = _get_temp_file('thumbs');
$cmd = self::getYouTubeDLCommand() . " --no-check-certificate --no-playlist --force-ipv4 --write-thumbnail --skip-download -o \"{$tmpfname}.jpg\" {$link}";
$cmd = self::getYouTubeDLCommand($addOauthFromProvider) . " --no-check-certificate --no-playlist --force-ipv4 --write-thumbnail --skip-download -o \"{$tmpfname}.jpg\" {$link}";
exec($cmd . " 2>&1", $output, $return_val);
_error_log("getThumbsFromLink: {$cmd}");

if ($return_val !== 0) {
_error_log("getThumbsFromLink: Error: " . json_encode($output));
if(empty($addOauthFromProvider) && self::hasSigninError($output)){
return self::getThumbsFromLink($link, $returnFileName, 'youtube');
}
}

$returnTmpfname = $tmpfname . ".jpg";
Expand All @@ -2619,16 +2641,19 @@ public static function getThumbsFromLink($link, $returnFileName = false)
}
}

public static function getDescriptionFromLink($link)
public static function getDescriptionFromLink($link, $addOauthFromProvider = '')
{
if (empty($link)) {
return '';
}
$link = escapeshellarg($link);
$tmpfname = _get_temp_file('thumbs');
$cmd = self::getYouTubeDLCommand() . " --no-check-certificate --no-playlist --force-ipv4 --write-description --skip-download -o \"{$tmpfname}\" {$link}";
$cmd = self::getYouTubeDLCommand($addOauthFromProvider) . " --no-check-certificate --no-playlist --force-ipv4 --write-description --skip-download -o \"{$tmpfname}\" {$link}";
exec($cmd . " 2>&1", $output, $return_val);
if ($return_val !== 0) {
if(empty($addOauthFromProvider) && self::hasSigninError($output)){
return self::getDescriptionFromLink($link, 'youtube');
}
@unlink($tmpfname . ".description");
return false;
} else {
Expand All @@ -2638,18 +2663,23 @@ public static function getDescriptionFromLink($link)
}
}

public static function getYouTubeDLCommand($forceYoutubeDL = false)
public static function getYouTubeDLCommand($addOauthFromProvider = '', $forceYoutubeDL = false)
{
global $global;
$ytdl = "youtube-dl ";
if (!empty($global['youtube-dl'])) {
return $global['youtube-dl'] . ' ';
$ytdl = $global['youtube-dl'] . ' ';
} elseif (empty($forceYoutubeDL) && file_exists("/usr/local/bin/yt-dlp")) {
return "/usr/local/bin/yt-dlp ";
$ytdl = "/usr/local/bin/yt-dlp ";
} elseif (file_exists("/usr/local/bin/youtube-dl")) {
return "/usr/local/bin/youtube-dl ";
} else {
return "youtube-dl ";
}
$ytdl = "/usr/local/bin/youtube-dl ";
}
if(!empty($addOauthFromProvider)){
$streamers_id = Login::getStreamerId();
$accessToken = Streamer::getAccessToken($streamers_id, $addOauthFromProvider);
$ytdl .= " --add-header \"Authorization: Bearer {$accessToken}\" ";
}
return $ytdl;
}

public static function setStreamerLog($encoder_queue_id, $msg, $type)
Expand Down
15 changes: 13 additions & 2 deletions objects/Streamer.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,18 +333,19 @@ static function revalidateToken($streamers_id, $provider)
$json = json_decode($jsonString, true);
}

$response['accessToken'] = $json[$provider]['json']["restream.ypt.me"]['accessToken'];

$response['accessToken'] = $json[$provider]['json']["restream.ypt.me"]['access_token'];
if (empty($response['accessToken'])) {
$response['msg'] = "revalidateToken($streamers_id, $provider) access_token is empty ";
return $response;
}

/*
$response['expires_at'] = $json[$provider]['json']["restream.ypt.me"]["expires"]["expires_at"];
if (time() <= $response['expires_at']) {
$response['msg'] = "Not expired yet";
return $response;
}
*/

$access_token = base64_encode(json_encode($response['accessToken']));

Expand All @@ -367,11 +368,21 @@ static function revalidateToken($streamers_id, $provider)
if(empty($response['error'] ) && !empty($response['respJson']['new_access_token'])){
$json[$provider]['json']["restream.ypt.me"]['accessToken'] = $response['respJson']['new_access_token'];
$json[$provider]['json']["restream.ypt.me"]['expires'] = $response['respJson']['expires'];

$response['accessToken'] = $response['respJson']['new_access_token'];
$response['expires'] = $response['respJson']['expires'];

$s->setJson($json);
$response['saved'] = $s->save();
}

return $response;
}

static function getAccessToken($streamers_id, $provider){
$json = self::revalidateToken($streamers_id, $provider);
//var_dump($json);exit;
return $json['accessToken']["access_token"];
}
}
}

0 comments on commit 7aa8132

Please sign in to comment.