1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pbmedia\LaravelFFMpeg; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use FFMpeg\Coordinate\TimeCode; |
7
|
|
|
use FFMpeg\Media\MediaTypeInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @method mixed save(\FFMpeg\Format\FormatInterface $format, $outputPathfile) |
11
|
|
|
*/ |
12
|
|
|
class Media |
13
|
|
|
{ |
14
|
|
|
protected $file; |
15
|
|
|
|
16
|
|
|
protected $media; |
17
|
|
|
|
18
|
|
|
public function __construct(File $file, MediaTypeInterface $media) |
19
|
|
|
{ |
20
|
|
|
$this->file = $file; |
21
|
|
|
$this->media = $media; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function isFrame(): bool |
25
|
|
|
{ |
26
|
|
|
return $this instanceof Frame; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function getFile(): File |
30
|
|
|
{ |
31
|
|
|
return $this->file; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getDurationInSeconds(): int |
35
|
|
|
{ |
36
|
|
|
return $this->media->getStreams()->first()->get('duration'); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function export(): MediaExporter |
40
|
|
|
{ |
41
|
|
|
return new MediaExporter($this); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function exportForHLS(): HLSPlaylistExporter |
45
|
|
|
{ |
46
|
|
|
return new HLSPlaylistExporter($this); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getFrameFromString(string $timecode): Frame |
50
|
|
|
{ |
51
|
|
|
return $this->getFrameFromTimecode( |
52
|
|
|
TimeCode::fromString($timecode) |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getFrameFromSeconds(float $quantity): Frame |
57
|
|
|
{ |
58
|
|
|
return $this->getFrameFromTimecode( |
59
|
|
|
TimeCode::fromSeconds($quantity) |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getFrameFromTimecode(TimeCode $timecode): Frame |
64
|
|
|
{ |
65
|
|
|
$frame = $this->media->frame($timecode); |
|
|
|
|
66
|
|
|
|
67
|
|
|
return new Frame($this->getFile(), $frame); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function addFilter(): Media |
71
|
|
|
{ |
72
|
|
|
$arguments = func_get_args(); |
73
|
|
|
|
74
|
|
|
if (isset($arguments[0]) && $arguments[0] instanceof Closure) { |
75
|
|
|
call_user_func_array($arguments[0], [$this->media->filters()]); |
76
|
|
|
} else { |
77
|
|
|
call_user_func_array([$this->media, 'addFilter'], $arguments); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function selfOrArgument($argument) |
84
|
|
|
{ |
85
|
|
|
return ($argument === $this->media) ? $this : $argument; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function __invoke(): MediaTypeInterface |
89
|
|
|
{ |
90
|
|
|
return $this->media; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function __clone() |
94
|
|
|
{ |
95
|
|
|
if ($this->media) { |
96
|
|
|
$clonedFilters = clone $this->media->getFiltersCollection(); |
|
|
|
|
97
|
|
|
|
98
|
|
|
$this->media = clone $this->media; |
99
|
|
|
|
100
|
|
|
$this->media->setFiltersCollection($clonedFilters); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function __call($method, $parameters) |
105
|
|
|
{ |
106
|
|
|
return $this->selfOrArgument( |
107
|
|
|
call_user_func_array([$this->media, $method], $parameters) |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: