summaryrefslogtreecommitdiff
path: root/app/Youtube/Service.php
blob: 862a27a886c9cdf1fef6a135db5a777f3f99e049 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?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;
    protected $video;

    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;
        $this->download($this->getVideoOptions());
    }

    public function downloadAudio(string $path)
    {
        $this->path = $path;
        $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()
            );

            $this->video = $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
        }
    }

    public function getTitle(): string
    {
        return $this->video->getTitle();
    }

    public function getFullPath(): string
    {
        return $this->getStoragePath() . '/' . $this->video->getFilename();
    }

    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'
        ];
    }
}