Passed
Pull Request — master (#262)
by Pascal
04:38 queued 01:30
created

MediaOpener::openWithInputOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 7
rs 10
1
<?php
2
3
namespace ProtoneMedia\LaravelFFMpeg;
4
5
use FFMpeg\Coordinate\TimeCode;
6
use FFMpeg\Media\AbstractMediaType;
7
use Illuminate\Contracts\Filesystem\Filesystem;
8
use Illuminate\Support\Arr;
9
use Illuminate\Support\Collection;
10
use Illuminate\Support\Traits\ForwardsCalls;
11
use ProtoneMedia\LaravelFFMpeg\Drivers\PHPFFMpeg;
12
use ProtoneMedia\LaravelFFMpeg\Exporters\HLSExporter;
13
use ProtoneMedia\LaravelFFMpeg\Exporters\MediaExporter;
14
use ProtoneMedia\LaravelFFMpeg\Filesystem\Disk;
15
use ProtoneMedia\LaravelFFMpeg\Filesystem\Media;
16
use ProtoneMedia\LaravelFFMpeg\Filesystem\MediaCollection;
17
use ProtoneMedia\LaravelFFMpeg\Filesystem\MediaOnNetwork;
18
use ProtoneMedia\LaravelFFMpeg\Filesystem\TemporaryDirectories;
19
20
/**
21
 * @mixin \ProtoneMedia\LaravelFFMpeg\Drivers\PHPFFMpeg
22
 */
23
class MediaOpener
24
{
25
    use ForwardsCalls;
26
27
    /**
28
     * @var \ProtoneMedia\LaravelFFMpeg\Filesystem\Disk
29
     */
30
    private $disk;
31
32
    /**
33
     * @var \ProtoneMedia\LaravelFFMpeg\Drivers\PHPFFMpeg
34
     */
35
    private $driver;
36
37
    /**
38
     * @var \ProtoneMedia\LaravelFFMpeg\Filesystem\MediaCollection
39
     */
40
    private $collection;
41
42
    /**
43
     * @var \FFMpeg\Coordinate\TimeCode
44
     */
45
    private $timecode;
46
47
    /**
48
     * Uses the 'filesystems.default' disk from the config if none is given.
49
     * Gets the underlying PHPFFMpeg instance from the container if none is given.
50
     * Instantiates a fresh MediaCollection if none is given.
51
     */
52
    public function __construct($disk = null, PHPFFMpeg $driver = null, MediaCollection $mediaCollection = null)
53
    {
54
        $this->disk = Disk::make($disk ?: config('filesystems.default'));
55
56
        $this->driver = ($driver ?: app(PHPFFMpeg::class))->fresh();
57
58
        $this->collection = $mediaCollection ?: new MediaCollection;
59
    }
60
61
    public function clone(): self
62
    {
63
        return new MediaOpener(
64
            $this->disk,
65
            $this->driver,
66
            $this->collection
67
        );
68
    }
69
70
    /**
71
     * Set the disk to open files from.
72
     */
73
    public function fromDisk($disk): self
74
    {
75
        $this->disk = Disk::make($disk);
76
77
        return $this;
78
    }
79
80
    /**
81
     * Alias for 'fromDisk', mostly for backwards compatibility.
82
     */
83
    public function fromFilesystem(Filesystem $filesystem): self
84
    {
85
        return $this->fromDisk($filesystem);
86
    }
87
88
    /**
89
     * Instantiates a Media object for each given path.
90
     */
91
    public function open($paths): self
92
    {
93
        foreach (Arr::wrap($paths) as $path) {
94
            $this->collection->push(Media::make($this->disk, $path));
0 ignored issues
show
Bug introduced by
The method push() does not exist on ProtoneMedia\LaravelFFMp...esystem\MediaCollection. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

94
            $this->collection->/** @scrutinizer ignore-call */ 
95
                               push(Media::make($this->disk, $path));
Loading history...
95
        }
96
97
        return $this;
98
    }
99
100
    public function openWithInputOptions(string $path, array $options = []): self
101
    {
102
        $this->collection->push(
103
            Media::make($this->disk, $path)->setInputOptions($options)
104
        );
105
106
        return $this;
107
    }
108
109
    /**
110
     * Instantiates a MediaOnNetwork object for each given url.
111
     */
112
    public function openUrl($paths, array $headers = []): self
113
    {
114
        foreach (Arr::wrap($paths) as $path) {
115
            $this->collection->push(MediaOnNetwork::make($path, $headers));
116
        }
117
118
        return $this;
119
    }
120
121
    public function get(): MediaCollection
122
    {
123
        return $this->collection;
124
    }
125
126
    public function getDriver(): PHPFFMpeg
127
    {
128
        return $this->driver->open($this->collection);
129
    }
130
131
    /**
132
     * Forces the driver to open the collection with the `openAdvanced` method.
133
     */
134
    public function getAdvancedDriver(): PHPFFMpeg
135
    {
136
        return $this->driver->openAdvanced($this->collection);
137
    }
138
139
    /**
140
     * Shortcut to set the timecode by string.
141
     */
142
    public function getFrameFromString(string $timecode): self
143
    {
144
        return $this->getFrameFromTimecode(TimeCode::fromString($timecode));
145
    }
146
147
    /**
148
     * Shortcut to set the timecode by seconds.
149
     */
150
    public function getFrameFromSeconds(float $seconds): self
151
    {
152
        return $this->getFrameFromTimecode(TimeCode::fromSeconds($seconds));
153
    }
154
155
    public function getFrameFromTimecode(TimeCode $timecode): self
156
    {
157
        $this->timecode = $timecode;
158
159
        return $this;
160
    }
161
162
    /**
163
     * Returns an instance of MediaExporter with the driver and timecode (if set).
164
     */
165
    public function export(): MediaExporter
166
    {
167
        return tap(new MediaExporter($this->getDriver()), function (MediaExporter $mediaExporter) {
168
            if ($this->timecode) {
169
                $mediaExporter->frame($this->timecode);
0 ignored issues
show
Bug introduced by
The method frame() does not exist on ProtoneMedia\LaravelFFMpeg\Exporters\MediaExporter. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

169
                $mediaExporter->/** @scrutinizer ignore-call */ 
170
                                frame($this->timecode);
Loading history...
170
            }
171
        });
172
    }
173
174
    /**
175
     * Returns an instance of HLSExporter with the driver forced to AdvancedMedia.
176
     */
177
    public function exportForHLS(): HLSExporter
178
    {
179
        return new HLSExporter($this->getAdvancedDriver());
180
    }
181
182
    public function cleanupTemporaryFiles(): self
183
    {
184
        TemporaryDirectories::deleteAll();
185
186
        return $this;
187
    }
188
189
    public function each($items, callable $callback): self
190
    {
191
        Collection::make($items)->each(function ($item, $key) use ($callback) {
192
            return $callback($this->clone(), $item, $key);
193
        });
194
195
        return $this;
196
    }
197
198
    /**
199
     * Returns the Media object from the driver.
200
     */
201
    public function __invoke(): AbstractMediaType
202
    {
203
        return $this->getDriver()->get();
204
    }
205
206
    /**
207
     * Forwards all calls to the underlying driver.
208
     * @return void
209
     */
210
    public function __call($method, $arguments)
211
    {
212
        $result = $this->forwardCallTo($driver = $this->getDriver(), $method, $arguments);
213
214
        return ($result === $driver) ? $this : $result;
215
    }
216
}
217