fixed youtube-dl issues

This commit is contained in:
huangjx
2021-09-11 15:07:01 +08:00
parent bf539e90b7
commit cba9d22cc8
3 changed files with 53 additions and 38 deletions

View File

@@ -5,12 +5,13 @@ namespace OCA\NCDownloader\Controller;
use OCA\NCDownloader\Search\torrentSearch;
use OCA\NCDownloader\Tools\Aria2;
use OCA\NCDownloader\Tools\DBConn;
use OCA\NCDownloader\Tools\File;
use OCA\NCDownloader\Tools\Helper;
use OCA\NCDownloader\Tools\YouTube;
use OCA\NCDownloader\Tools\Youtube;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\Files\IRootFolder;
//use OCP\Files\IRootFolder;
use OCP\IL10N;
use OCP\IRequest;
use OC_Util;
@@ -25,18 +26,19 @@ class MainController extends Controller
private $aria2Opts;
private $l10n;
public function __construct($appName, IRequest $request, $UserId, IL10N $IL10N, IRootFolder $rootFolder, Aria2 $aria2)
public function __construct($appName, IRequest $request, $UserId, IL10N $IL10N, Youtube $youtube, Aria2 $aria2)
{
parent::__construct($appName, $request);
$this->appName = $appName;
$this->uid = $UserId;
$this->l10n = $IL10N;
$this->rootFolder = $rootFolder;
$this->dataDir = \OC::$server->getSystemConfig()->getValue('datadirectory');
//$this->rootFolder = $rootFolder;
OC_Util::setupFS();
$this->urlGenerator = \OC::$server->getURLGenerator();
$this->aria2 = $aria2;
$this->aria2->init();
$this->youtube = new Youtube();
$this->youtube = $youtube;
$this->dbconn = new DBConn();
}
@@ -86,17 +88,10 @@ class MainController extends Controller
return new JSONResponse(['error' => $this->l10n->t("Youtube-dl NOT installed!")]);
}
if (Helper::isGetUrlSite($inputValue)) {
if ($data = $yt->forceIPV4()->getDownloadUrl($inputValue)) {
$this->Save($data['url'], $data['filename']);
return new JSONResponse(['yt' => $data]);
} else {
return new JSONResponse(['error' => $this->l10n->t("failed to get any url!")]);
}
} else {
$yt->setDownloadDir($this->realDownloadDir);
return new JSONResponse(['yt' => $yt->download($inputValue)]);
}
$resp = $yt->forceIPV4()->download($inputValue);
File::syncFolder();
return new JSONResponse(['yt' => $resp]);
} else if ($type === 'search') {
$data = torrentSearch::go($inputValue);
$resp['title'] = ['title', 'seeders', 'info', 'actions'];

View File

@@ -5,15 +5,18 @@ use OCA\NCDownloader\Tools\Helper;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
class YouTube
class Youtube
{
private $ipv4Only;
private $audioOnly = 0;
private $audioFormat, $videoFormat = 'mp4';
private $options = [];
public function __construct()
private $downloadDir;
public function __construct($config)
{
$config += ['downloadDir' => '/tmp/downloads'];
$this->bin = Helper::findBinaryPath('youtube-dl');
$this->setDownloadDir($config['downloadDir']);
}
public function GetUrlOnly()
@@ -33,6 +36,11 @@ class YouTube
$this->downloadDir = rtrim($dir, '/');
}
public function getDownloadDir()
{
return $this->getDownloadDir;
}
public function prependOption($option)
{
array_unshift($this->options, $option);
@@ -47,6 +55,8 @@ class YouTube
$this->prependOption($this->bin);
// $this->buildCMD();
$process = new Process($this->options);
//the maximum time required to download the file
$process->setTimeout(60*60*15);
try {
$process->mustRun();
$output = $process->getOutput();
@@ -101,7 +111,7 @@ class YouTube
private function buildCMD()
{
$this->cmd = $this->bin;//. " 2>&1";
$this->cmd = $this->bin; //. " 2>&1";
foreach ($this->options as $option) {
$this->cmd .= " " . $option;