From 897b68ac107f2fb0d4dc1d31ce96ce24c862eb27 Mon Sep 17 00:00:00 2001 From: Fbenas Date: Fri, 19 Jun 2020 21:05:19 +0100 Subject: Add youtube video and audio scraping commands --- app/Console/Commands/ScrapeYoutube.php | 57 +++++++++++++ app/Youtube/Service.php | 151 +++++++++++++++++++++++++++++++++ composer.json | 3 +- composer.lock | 123 ++++++++++++++++++++++++++- 4 files changed, 332 insertions(+), 2 deletions(-) create mode 100644 app/Console/Commands/ScrapeYoutube.php create mode 100644 app/Youtube/Service.php diff --git a/app/Console/Commands/ScrapeYoutube.php b/app/Console/Commands/ScrapeYoutube.php new file mode 100644 index 0000000..ce64d3f --- /dev/null +++ b/app/Console/Commands/ScrapeYoutube.php @@ -0,0 +1,57 @@ +argument('url'); + $format = $this->argument('format'); + + $service = new Service($url, $this->output); + + if ($format == 'video') { + $video = $service->downloadVideo('video'); + } elseif ($format == 'audio') { + $video = $service->downloadAudio('audio'); + } + + $this->info('Download of ' . $video->getTitle() . ' complete!'); + + return Command::SUCCESS; + } + +} diff --git a/app/Youtube/Service.php b/app/Youtube/Service.php new file mode 100644 index 0000000..090020e --- /dev/null +++ b/app/Youtube/Service.php @@ -0,0 +1,151 @@ +url = $url; + if ($output) { + $this->progressBar = new ProgressBar($output, 100); + $this->output = $output; + } + } + + public function running($isRunning = null): bool + { + if (isset($isRunning)) { + $this->running = $isRunning; + } + + return $this->running; + } + + public function downloadVideo(string $path) + { + $this->path = $path; + + return $this->download($this->getVideoOptions()); + } + + public function downloadAudio(string $path) + { + $this->path = $path; + + return $this->download($thjis->getAudioOptions()); + } + + protected function download(array $options) + { + $dl = new YoutubeDl($options); + + $dl->onProgress( + function ($progress) { + $size = $this->formatBytes($progress['size']); + + if (!$this->running()) { + $this->progressBar->start($size); + $this->running(true); + } + + $percentage = $progress['percentage']; + + $this->progressBar->setProgress($size * round($percentage) / 100); + } + ); + + // Set the download path where you want to store downloaded data + $dl->setDownloadPath($this->getWorkingPath()); + + try { + $video = $dl->download($this->url); + $this->progressBar->finish(); + $this->running(false); + + // $this->getWorkingPath() .'/' . $video->getFilename(), + Storage::disk('local')->move( + 'tmp/youtube/video/' . $video->getFilename(), + 'youtube/video/' . $video->getFilename() + ); + + return $video; + + // $video->getFile(); // \SplFileInfo instance of downloaded file + } catch (NotFoundException $e) { + dd($e); + // Video not found + } catch (PrivateVideoException $e) { + dd($e); + // Video is private + } catch (CopyrightException $e) { + dd($e); + // The YouTube account associated with this video has been terminated due to multiple third-party notifications of copyright infringement + } catch (\Exception $e) { + dd($e); + // Failed to download + } + } + + protected function formatBytes(string $bytes) + { + $units = [ + 'GiB' => 1000, + 'MiB' => 1, + 'KiB' => 0.0001, + 'B' => 1, + ]; + + foreach ($units as $u => $size) { + if (Str::contains($bytes, $u)) { + return Str::replaceFirst($u, '', $bytes) * $size; + } + } + + var_dump("bad");die(); + return 0; + } + + protected function getWorkingPath() + { + return Storage::disk('local')->path('tmp/youtube/' . $this->path); + } + + protected function getStoragePath() + { + return Storage::disk('local')->path('youtube/' . $this->path); + } + + // For more options go to https://github.com/rg3/youtube-dl#user-content-options + protected function getVideoOptions() + { + return [ + 'prefer-free-formats' => true, + 'no-overwrites' => true, + // 'skip-download' => true + ]; + } + + // For more options go to https://github.com/rg3/youtube-dl#user-content-options + protected function getAudioOptions() + { + return [ + 'extract-audio' => true, + 'audio-format' => 'mp3', + 'audio-quality' => 0, // best + 'output' => '%(title)s.%(ext)s' + ]; + } +} diff --git a/composer.json b/composer.json index 484b58d..84922e9 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,8 @@ "guzzlehttp/guzzle": "^6.3", "laravel/framework": "^7.0", "laravel/tinker": "^2.0", - "laravel/ui": "^2.0" + "laravel/ui": "^2.0", + "norkunas/youtube-dl-php": "^1.6" }, "require-dev": { "facade/ignition": "^2.0", diff --git a/composer.lock b/composer.lock index 88242cd..f1d917e 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "882cf27d85ea148b9b1c22dc249ab7c1", + "content-hash": "643124ee878d085b3013afec8b48261d", "packages": [ { "name": "asm89/stack-cors", @@ -1487,6 +1487,57 @@ ], "time": "2020-06-03T07:24:19+00:00" }, + { + "name": "norkunas/youtube-dl-php", + "version": "v1.6.0", + "source": { + "type": "git", + "url": "https://github.com/norkunas/youtube-dl-php.git", + "reference": "15020297ab7bf6a52bdff5585aa114feac2d55c0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/norkunas/youtube-dl-php/zipball/15020297ab7bf6a52bdff5585aa114feac2d55c0", + "reference": "15020297ab7bf6a52bdff5585aa114feac2d55c0", + "shasum": "" + }, + "require": { + "ext-json": "*", + "php": ">=7.0.8", + "symfony/options-resolver": "^3.3|^4.0|^5.0", + "symfony/process": "^3.3|^4.0|^5.0" + }, + "require-dev": { + "phpunit/phpunit": "^8.2" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0-dev" + } + }, + "autoload": { + "psr-4": { + "YoutubeDl\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Tomas Norkūnas", + "email": "norkunas.tom@gmail.com" + } + ], + "description": "youtube-dl wrapper for php", + "keywords": [ + "youtube", + "youtube-dl" + ], + "time": "2020-04-23T04:45:11+00:00" + }, { "name": "opis/closure", "version": "3.5.5", @@ -3258,6 +3309,76 @@ ], "time": "2020-06-09T15:07:35+00:00" }, + { + "name": "symfony/options-resolver", + "version": "v5.1.2", + "source": { + "type": "git", + "url": "https://github.com/symfony/options-resolver.git", + "reference": "663f5dd5e14057d1954fe721f9709d35837f2447" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/options-resolver/zipball/663f5dd5e14057d1954fe721f9709d35837f2447", + "reference": "663f5dd5e14057d1954fe721f9709d35837f2447", + "shasum": "" + }, + "require": { + "php": ">=7.2.5", + "symfony/deprecation-contracts": "^2.1", + "symfony/polyfill-php80": "^1.15" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.1-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\OptionsResolver\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony OptionsResolver Component", + "homepage": "https://symfony.com", + "keywords": [ + "config", + "configuration", + "options" + ], + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2020-05-23T13:08:13+00:00" + }, { "name": "symfony/polyfill-ctype", "version": "v1.17.0", -- cgit v1.2.3