fixed locale issue for youtube-dl;updated regex rules for matching filename

This commit is contained in:
huangjx
2021-09-15 13:18:19 +08:00
parent 2e834d4f9c
commit 7427f395f5
4 changed files with 18 additions and 13 deletions

View File

@@ -16,6 +16,7 @@ class Youtube
private $timeout = 60 * 60 * 15;
private $outTpl = "/%(id)s-%(title)s.%(ext)s";
private $defaultDir = "/tmp/downloads";
private $env = [];
public function __construct($config)
{
@@ -23,14 +24,21 @@ class Youtube
$this->bin = Helper::findBinaryPath('youtube-dl');
$this->init();
$this->setDownloadDir($config['downloadDir']);
$this->helper = YoutubeHelper::create();
}
public function init()
{
if (empty($lang = getenv('LANG')) || strpos(strtolower($lang), 'utf-8') === false) {
$lang = 'C.UTF-8';
}
$this->setEnv('LANG', $lang);
$this->addOption("--no-mtime");
}
public function setEnv($key, $val)
{
$this->env[$key] = $val;
}
public function GetUrlOnly()
{
$this->addOption('--get-filename');
@@ -66,7 +74,7 @@ class Youtube
$this->setUrl($url);
$this->prependOption($this->bin);
// $this->buildCMD();
$process = new Process($this->options);
$process = new Process($this->options, null, $this->env);
//the maximum time required to download the file
$process->setTimeout($this->timeout);
try {
@@ -81,12 +89,13 @@ class Youtube
public function download($url)
{
$this->helper = YoutubeHelper::create();
$this->downloadDir = $this->downloadDir ?? $this->defaultDir;
$this->prependOption($this->downloadDir . $this->outTpl);
$this->prependOption("-o");
$this->setUrl($url);
$this->prependOption($this->bin);
$process = new Process($this->options);
$process = new Process($this->options, null, $this->env);
$process->setTimeout($this->timeout);
$process->run(function ($type, $buffer) use ($url) {
if (Process::ERR === $type) {

View File

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