fixed #8 added option for ripping audio from youtube-dl sites;fixed bugs

This commit is contained in:
huangjx
2021-10-01 16:53:57 +08:00
parent 98e62df996
commit ac2f314119
11 changed files with 152 additions and 28 deletions

View File

@@ -86,7 +86,7 @@ class MainController extends Controller
'data' => serialize(['link' => $url]),
];
$this->dbconn->save($data);
$resp = ['gid' => $result, 'file' => $filename, 'result' => $result];
$resp = ['message' => $filename, 'result' => $result,'file' => $filename];
return $resp;
}

View File

@@ -34,7 +34,10 @@ class YoutubeController extends Controller
$this->aria2->init();
$this->tablename = $this->dbconn->queryBuilder->getTableName("ncdownloader_info");
}
/**
* @NoAdminRequired
*
*/
public function Index()
{
$data = $this->dbconn->getYoutubeByUid($this->uid);
@@ -66,12 +69,12 @@ class YoutubeController extends Controller
$resp['counter'] = ['youtube-dl' => count($data)];
return new JSONResponse($resp);
}
public function Download()
{
$params = array();
$url = trim($this->request->getParam('form_input_text'));
$yt = $this->youtube;
$yt->audioOnly = (bool) $this->request->getParam('audioOnly');
if (!$yt->isInstalled()) {
return new JSONResponse($this->installYTD());
}
@@ -102,7 +105,7 @@ class YoutubeController extends Controller
}
if ($this->dbconn->deleteByGid($gid)) {
return new JSONResponse(['message' => $gid . " deleted!"]);
return new JSONResponse(['message' => $gid . " Deleted!"]);
}
}
@@ -131,7 +134,7 @@ class YoutubeController extends Controller
'data' => serialize(['link' => $url]),
];
$this->dbconn->save($data);
$resp = ['gid' => $result, 'file' => $filename, 'result' => $result];
$resp = ['message' => $filename, 'result' => $result];
return $resp;
}

View File

@@ -284,5 +284,9 @@ class Helper
}
return sprintf('%04x%04x%04x%04x%04x%04x%04x%04x', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479));
}
public static function ffmpegInstalled()
{
return (bool) self::findBinaryPath('ffmpeg');
}
}

View File

@@ -9,8 +9,9 @@ use Symfony\Component\Process\Process;
class Youtube
{
private $ipv4Only;
private $audioOnly = 0;
private $audioFormat, $videoFormat = 'mp4';
public $audioOnly = 0;
private $audioFormat = 'm4a', $videoFormat;
private $format = 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best';
private $options = [];
private $downloadDir;
private $timeout = 60 * 60 * 15;
@@ -18,15 +19,25 @@ class Youtube
private $defaultDir = "/tmp/downloads";
private $env = [];
public function __construct($config)
public function __construct(array $options)
{
$config += ['downloadDir' => '/tmp/downloads'];
$this->bin = $config['binary'] ?? Helper::findBinaryPath('youtube-dl');
$this->init();
$this->setDownloadDir($config['downloadDir']);
$options += ['downloadDir' => '/tmp/downloads', 'settings' => []];
$this->init($options);
}
public function init()
public function init(array $options)
{
$this->bin = Helper::findBinaryPath('youtube-dl');
extract($options);
$this->setDownloadDir($downloadDir);
if (!empty($settings)) {
foreach ($settings as $key => $value) {
if (empty($value)) {
$this->addOption($key);
} else {
$this->setOption($key, $value);
}
}
}
if (empty($lang = getenv('LANG')) || strpos(strtolower($lang), 'utf-8') === false) {
$lang = 'C.UTF-8';
}
@@ -39,6 +50,25 @@ class Youtube
$this->env[$key] = $val;
}
public function audioMode()
{
if (Helper::ffmpegInstalled()) {
$this->addOption('--prefer-ffmpeg');
$this->addOption('--add-metadata');
$this->addOption('--metadata-from-title');
$this->addOption("%(artist)s - %(title)s");
$this->addOption('--audio-format');
$this->addOption($this->audioFormat);
}
$this->addOption('--extract-audio');
return $this;
}
public function setAudioQuality($value = 0)
{
$this->addOption('--audio-quality', $value);
}
public function GetUrlOnly()
{
$this->addOption('--get-filename');
@@ -61,7 +91,7 @@ class Youtube
return $this->getDownloadDir;
}
public function prependOption($option)
public function prependOption(string $option)
{
array_unshift($this->options, $option);
}
@@ -89,6 +119,13 @@ class Youtube
public function download($url)
{
if ($this->audioOnly) {
$this->audioMode();
$this->outTpl = "/%(id)s-%(title)s." . $this->audioFormat;
} else {
$this->addOption('--format');
$this->addOption($this->format);
}
$this->helper = YoutubeHelper::create();
$this->downloadDir = $this->downloadDir ?? $this->defaultDir;
$this->prependOption($this->downloadDir . $this->outTpl);
@@ -96,6 +133,7 @@ class Youtube
$this->setUrl($url);
$this->prependOption($this->bin);
$process = new Process($this->options, null, $this->env);
//\OC::$server->getLogger()->error($process->getCommandLine(), ['app' => 'PHP']);
$process->setTimeout($this->timeout);
$process->run(function ($type, $buffer) use ($url) {
if (Process::ERR === $type) {
@@ -108,16 +146,7 @@ class Youtube
$this->helper->updateStatus(Helper::STATUS['COMPLETE']);
return ['message' => $this->helper->file ?? $process->getErrorOutput()];
}
return $process->getErrorOutput();
}
public function getFilePath($output)
{
$rules = '#\[download\]\s+Destination:\s+(?<filename>.*\.(?<ext>(mp4|mp3|aac)))$#i';
preg_match($rules, $output, $matches);
return $matches['filename'] ?? null;
return ['error' => $process->getErrorOutput()];
}
private function onError($buffer)
@@ -150,7 +179,12 @@ class Youtube
//$index = array_search('-i', $this->options);
//array_splice($this->options, $index + 1, 0, $url);
}
public function setOption($key, $value)
{
$this->addOption($key);
$this->addOption($value);
return $this;
}
public function addOption($option)
{
array_push($this->options, $option);

View File

@@ -29,7 +29,7 @@ class YoutubeHelper
}
public function getFilePath($output)
{
$rules = '#\[download\]\s+Destination:\s+(?<filename>.*\.(?<ext>(mp4|mp3|aac|webm|m4a|ogg)))$#i';
$rules = '#\[download\]\s+Destination:\s+(?<filename>.*\.(?<ext>(mp4|mp3|aac|webm|m4a|ogg|3gp|mkv|wav|flv)))$#i';
preg_match($rules, $output, $matches);