1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pbmedia\LaravelFFMpeg; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use FFMpeg\Coordinate\TimeCode; |
7
|
|
|
use FFMpeg\Filters\Audio\SimpleFilter; |
8
|
|
|
use FFMpeg\Filters\FilterInterface; |
9
|
|
|
use FFMpeg\Media\MediaTypeInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @method mixed save(\FFMpeg\Format\FormatInterface $format, $outputPathfile) |
13
|
|
|
*/ |
14
|
|
|
class Media |
15
|
|
|
{ |
16
|
|
|
protected $file; |
17
|
|
|
|
18
|
|
|
protected $media; |
19
|
|
|
|
20
|
|
|
public function __construct(File $file, MediaTypeInterface $media) |
21
|
|
|
{ |
22
|
|
|
$this->file = $file; |
23
|
|
|
$this->media = $media; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function isFrame(): bool |
27
|
|
|
{ |
28
|
|
|
return $this instanceof Frame; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function getFile(): File |
32
|
|
|
{ |
33
|
|
|
return $this->file; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getDurationInSeconds(): int |
37
|
|
|
{ |
38
|
|
|
return $this->getDurationInMiliseconds() / 1000; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getFirstStream() |
42
|
|
|
{ |
43
|
|
|
return $this->media->getStreams()->first(); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function getDurationInMiliseconds(): float |
47
|
|
|
{ |
48
|
|
|
$stream = $this->getFirstStream(); |
49
|
|
|
|
50
|
|
|
if ($stream->has('duration')) { |
51
|
|
|
return $stream->get('duration') * 1000; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$format = $this->media->getFormat(); |
|
|
|
|
55
|
|
|
|
56
|
|
|
if ($format->has('duration')) { |
57
|
|
|
return $format->get('duration') * 1000; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function export(): MediaExporter |
62
|
|
|
{ |
63
|
|
|
return new MediaExporter($this); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function exportForHLS(): HLSPlaylistExporter |
67
|
|
|
{ |
68
|
|
|
return new HLSPlaylistExporter($this); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getFrameFromString(string $timecode): Frame |
72
|
|
|
{ |
73
|
|
|
return $this->getFrameFromTimecode( |
74
|
|
|
TimeCode::fromString($timecode) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getFrameFromSeconds(float $quantity): Frame |
79
|
|
|
{ |
80
|
|
|
return $this->getFrameFromTimecode( |
81
|
|
|
TimeCode::fromSeconds($quantity) |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getFrameFromTimecode(TimeCode $timecode): Frame |
86
|
|
|
{ |
87
|
|
|
$frame = $this->media->frame($timecode); |
|
|
|
|
88
|
|
|
|
89
|
|
|
return new Frame($this->getFile(), $frame); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function addFilter(): Media |
93
|
|
|
{ |
94
|
|
|
$arguments = func_get_args(); |
95
|
|
|
|
96
|
|
|
if (isset($arguments[0]) && $arguments[0] instanceof Closure) { |
97
|
|
|
call_user_func_array($arguments[0], [$this->media->filters()]); |
98
|
|
|
} else if (isset($arguments[0]) && $arguments[0] instanceof FilterInterface) { |
99
|
|
|
call_user_func_array([$this->media, 'addFilter'], $arguments); |
100
|
|
|
} else if (isset($arguments[0]) && is_array($arguments[0])) { |
101
|
|
|
$this->media->addFilter(new SimpleFilter($arguments[0])); |
|
|
|
|
102
|
|
|
} else { |
103
|
|
|
$this->media->addFilter(new SimpleFilter($arguments)); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
protected function selfOrArgument($argument) |
110
|
|
|
{ |
111
|
|
|
return ($argument === $this->media) ? $this : $argument; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function __invoke(): MediaTypeInterface |
115
|
|
|
{ |
116
|
|
|
return $this->media; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function __clone() |
120
|
|
|
{ |
121
|
|
|
if ($this->media) { |
122
|
|
|
$clonedFilters = clone $this->media->getFiltersCollection(); |
|
|
|
|
123
|
|
|
|
124
|
|
|
$this->media = clone $this->media; |
125
|
|
|
|
126
|
|
|
$this->media->setFiltersCollection($clonedFilters); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function __call($method, $parameters) |
131
|
|
|
{ |
132
|
|
|
return $this->selfOrArgument( |
133
|
|
|
call_user_func_array([$this->media, $method], $parameters) |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
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: