diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/Console/Commands/ScrapeYoutube.php | 57 | ||||
-rw-r--r-- | app/Youtube/Service.php | 151 |
2 files changed, 208 insertions, 0 deletions
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 @@ +<?php + +namespace App\Console\Commands; + +use App\Youtube\Service; +use Illuminate\Console\Command; + +class ScrapeYoutube extends Command +{ + /** + * The name and signature of the console command. + * + * @var string + */ + protected $signature = 'scrape:youtube { url } { format }'; + + /** + * The console command description. + * + * @var string + */ + protected $description = 'Scrape a youtube for videos'; + + /** + * Create a new command instance. + * + * @return void + */ + public function __construct() + { + parent::__construct(); + } + + /** + * Execute the console command. + * + * @return mixed + */ + public function handle() + { + $url = $this->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 @@ +<?php + +namespace App\Youtube; + +use Illuminate\Support\Facades\Storage; +use Illuminate\Support\Str; +use Symfony\Component\Console\Helper\ProgressBar; +use YoutubeDl\Exception\CopyrightException; +use YoutubeDl\Exception\NotFoundException; +use YoutubeDl\Exception\PrivateVideoException; +use YoutubeDl\YoutubeDl; + +class Service +{ + protected $url; + protected $progressBar; + protected $running = false; + + public function __construct(string $url, $output = null) + { + $this->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' + ]; + } +} |