if (null === $name && null === $val) {
return;
}
- if ((stristr($val, ' ') || stristr($val, '&')) && !stristr($val, '>') && !stristr($val, '<')) {
+ if ((stristr($val, ';') || stristr($val, ' ') || stristr($val, '&')) && !stristr($val, '>') && !stristr($val, '<')) {
$val = '"' . $val . '"';
}
$this->args[] = array($name, $val);
$this->args[] = array(null, $val);
}
+ public function hashArgs()
+ {
+ return hash('sha256', json_encode($this->args));
+ }
+
protected function _preExecute()
{
class YoutubeDL
{
protected static $_cookiesFile = null;
+ protected static $_cookiesBrowser = 'chrome';
- public static function setCookiesFile($cookieFile)
+ public static function setCookiesFile($cookieFile, $cookiesBrowser = 'chrome')
{
static::$_cookiesFile = $cookieFile;
+ static::$_cookiesBrowser = $cookiesBrowser;
}
public static function downloadVideo($url, $path)
protected static function _download($url, $path, $onlyAudio = false)
{
- $ext = 'mp4';
-
- $options = Options::create()
- ->downloadPath($path)
- ->cacheDir(storage_path('cache/youtubedl'))
- ->format('bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4')
- ->url($url);
-
- if (null !== static::$_cookiesFile) {
- $options = $options->cookies(static::$_cookiesFile);
- }
+ $ext = $onlyAudio ? 'mp3' : 'mp4';
+ $cli = new CommandLine('/usr/local/bin/yt-dlp');
+ $cli->setArg('cache-dir', storage_path('cache/youtubedl'));
+ $cli->setArg('format', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4');
+ $cli->setArg('extractor-args', 'youtube:player_client=web,default,-ios');
if ($onlyAudio) {
- $ext = 'mp3';
- $options = $options->extractAudio(true)
- ->audioFormat('mp3');
+ $cli->setArg('extract-audio');
+ $cli->setArg('audio-format', 'mp3');
+ }
+ if (null !== static::$_cookiesFile) {
+ $cli->setArg('cookies', static::$_cookiesFile);
}
- $cacheKey = hash('sha256', $url . print_r($options->toArray(), true));
+ $cacheKey = hash('sha256', $url . "-" . $cli->hashArgs());
$fname = $cacheKey . '.' . $ext;
$file = Files::mkdir($path) . $fname;
- $options = $options->output($fname);
-
if (!file_exists($file)) {
- $yt = new \YoutubeDl\YoutubeDl();
- $yt->setBinPath('/usr/local/bin/yt-dlp');
- $collection = $yt->download($options);
-
- foreach ($collection->getVideos() as $video) {
- if ($video->getError() !== null) {
- Log::debug("Error downloading video: {$video->getError()}.");
- } else {
- $video->getFile();
- }
- }
-
+ $cli->setArg('output', $file);
+ $cli->setArg(null, $url);
+ $cli->execute();
}
return $file;
}