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
|
|
View Code Duplication |
public function getDurationInSeconds(): int |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$stream = $this->media->getStreams()->first(); |
|
|
|
|
37
|
|
|
|
38
|
|
|
if ($stream->has('duration')) { |
39
|
|
|
return $stream->get('duration'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$format = $this->media->getFormat(); |
|
|
|
|
43
|
|
|
|
44
|
|
|
if ($format->has('duration')) { |
45
|
|
|
return $format->get('duration'); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
View Code Duplication |
public function getDurationInMiliseconds(): float |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$stream = $this->media->getStreams()->first(); |
|
|
|
|
52
|
|
|
|
53
|
|
|
if ($stream->has('duration')) { |
54
|
|
|
return $stream->get('duration') * 1000; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$format = $this->media->getFormat(); |
|
|
|
|
58
|
|
|
|
59
|
|
|
if ($format->has('duration')) { |
60
|
|
|
return $format->get('duration') * 1000; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function export(): MediaExporter |
65
|
|
|
{ |
66
|
|
|
return new MediaExporter($this); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function exportForHLS(): HLSPlaylistExporter |
70
|
|
|
{ |
71
|
|
|
return new HLSPlaylistExporter($this); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getFrameFromString(string $timecode): Frame |
75
|
|
|
{ |
76
|
|
|
return $this->getFrameFromTimecode( |
77
|
|
|
TimeCode::fromString($timecode) |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getFrameFromSeconds(float $quantity): Frame |
82
|
|
|
{ |
83
|
|
|
return $this->getFrameFromTimecode( |
84
|
|
|
TimeCode::fromSeconds($quantity) |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getFrameFromTimecode(TimeCode $timecode): Frame |
89
|
|
|
{ |
90
|
|
|
$frame = $this->media->frame($timecode); |
|
|
|
|
91
|
|
|
|
92
|
|
|
return new Frame($this->getFile(), $frame); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function addFilter(): Media |
96
|
|
|
{ |
97
|
|
|
$arguments = func_get_args(); |
98
|
|
|
|
99
|
|
|
if (isset($arguments[0]) && $arguments[0] instanceof Closure) { |
100
|
|
|
call_user_func_array($arguments[0], [$this->media->filters()]); |
101
|
|
|
} else { |
102
|
|
|
call_user_func_array([$this->media, 'addFilter'], $arguments); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
protected function selfOrArgument($argument) |
109
|
|
|
{ |
110
|
|
|
return ($argument === $this->media) ? $this : $argument; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function __invoke(): MediaTypeInterface |
114
|
|
|
{ |
115
|
|
|
return $this->media; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function __clone() |
119
|
|
|
{ |
120
|
|
|
if ($this->media) { |
121
|
|
|
$clonedFilters = clone $this->media->getFiltersCollection(); |
|
|
|
|
122
|
|
|
|
123
|
|
|
$this->media = clone $this->media; |
124
|
|
|
|
125
|
|
|
$this->media->setFiltersCollection($clonedFilters); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function __call($method, $parameters) |
130
|
|
|
{ |
131
|
|
|
return $this->selfOrArgument( |
132
|
|
|
call_user_func_array([$this->media, $method], $parameters) |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.