Total Complexity | 41 |
Total Lines | 242 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like FFMpeg often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FFMpeg, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class FFMpeg |
||
27 | { |
||
28 | private readonly UploadedFile $file; |
||
29 | |||
30 | private readonly string $disk; |
||
31 | |||
32 | private readonly int $dominantColorQuality; |
||
33 | |||
34 | private FFMpegMeta $meta; |
||
35 | |||
36 | private Video|Audio $media; |
||
37 | |||
38 | |||
39 | /** |
||
40 | * Default scale size |
||
41 | * we use this value if width and height both were undefined |
||
42 | * |
||
43 | * @var integer |
||
44 | */ |
||
45 | private const DEFAULT_SCALE = 850; |
||
46 | |||
47 | |||
48 | public function __construct(UploadedFile $file, string $disk, int $dominantColorQuality) |
||
49 | { |
||
50 | $this->file = $file; |
||
|
|||
51 | $this->disk = $disk; |
||
52 | $this->dominantColorQuality = $dominantColorQuality; |
||
53 | |||
54 | $config = config('larupload.ffmpeg'); |
||
55 | |||
56 | $ffmpeg = FFMpegLib::create([ |
||
57 | 'ffmpeg.binaries' => $config['ffmpeg-binaries'], |
||
58 | 'ffprobe.binaries' => $config['ffprobe-binaries'], |
||
59 | 'timeout' => $config['timeout'], |
||
60 | 'ffmpeg.threads' => $config['threads'] ?? 12, |
||
61 | ], $this->logChannel()); |
||
62 | |||
63 | $this->media = $ffmpeg->open($file->getRealPath()); |
||
64 | } |
||
65 | |||
66 | |||
67 | public function getMedia(): Video|Audio |
||
70 | } |
||
71 | |||
72 | public function getMeta(): FFMpegMeta |
||
73 | { |
||
74 | if (empty($this->meta)) { |
||
75 | $meta = $this->media->getStreams()->first()->all(); |
||
76 | |||
77 | // support rotate tag in old ffmpeg versions |
||
78 | if (isset($meta['tags']['rotate'])) { |
||
79 | // @codeCoverageIgnoreStart |
||
80 | $rotate = $meta['tags']['rotate']; |
||
81 | |||
82 | if ($rotate == 90 or $rotate == 270) { |
||
83 | list($meta['height'], $meta['width']) = array($meta['width'], $meta['height']); |
||
84 | } |
||
85 | // @codeCoverageIgnoreEnd |
||
86 | } |
||
87 | |||
88 | // in some formats like webm, duration is not available in streams, so we should get it from format |
||
89 | if (!isset($meta['duration'])) { |
||
90 | $meta['duration'] = $this->media->getFormat()->get('duration', 0); |
||
91 | } |
||
92 | |||
93 | $this->meta = FFMpegMeta::make( |
||
94 | width: $meta['width'] ?? null, |
||
95 | height: $meta['height'] ?? null, |
||
96 | duration: $meta['duration'] |
||
97 | ); |
||
98 | } |
||
99 | |||
100 | return $this->meta; |
||
101 | } |
||
102 | |||
103 | public function setMeta(FFMpegMeta $meta): self |
||
104 | { |
||
105 | $this->meta = $meta; |
||
106 | |||
107 | return $this; |
||
108 | } |
||
109 | |||
110 | public function capture(int|float|null $fromSeconds, ImageStyle $style, string $saveTo, bool $withDominantColor = false): ?string |
||
111 | { |
||
112 | $dominantColor = null; |
||
113 | $saveTo = get_larupload_save_path($this->disk, $saveTo); |
||
114 | |||
115 | |||
116 | $style->mode->ffmpegResizeFilter() |
||
117 | ? $this->resize($style) |
||
118 | : $this->crop($style); |
||
119 | |||
120 | |||
121 | $this->frame($fromSeconds, $saveTo); |
||
122 | |||
123 | if ($withDominantColor) { |
||
124 | $dominantColor = $this->dominantColor($saveTo['local']); |
||
125 | } |
||
126 | |||
127 | larupload_finalize_save($this->disk, $saveTo); |
||
128 | |||
129 | return $dominantColor; |
||
130 | } |
||
131 | |||
132 | public function audio(AudioStyle $style, string $saveTo): void |
||
139 | } |
||
140 | |||
141 | public function manipulate(VideoStyle $style, string $saveTo): void |
||
151 | } |
||
152 | |||
153 | public function stream(array $styles, string $basePath, string $fileName): bool |
||
154 | { |
||
155 | $hls = new HLS($this, $this->disk); |
||
156 | |||
157 | return $hls->export($styles, $basePath, $fileName); |
||
158 | } |
||
159 | |||
160 | public function resize(VideoStyle|ImageStyle|StreamStyle $style): void |
||
161 | { |
||
162 | $dimension = $this->dimension($style); |
||
163 | |||
164 | if ($style->padding) { |
||
165 | $this->media->filters()->pad($dimension)->synchronize(); |
||
166 | } |
||
167 | else { |
||
168 | $mode = $style->mode->ffmpegResizeFilter() ?? ResizeFilter::RESIZEMODE_SCALE_HEIGHT; |
||
169 | |||
170 | $this->media->filters() |
||
171 | ->resize($dimension, $mode) |
||
172 | ->synchronize(); |
||
173 | } |
||
174 | } |
||
175 | |||
176 | public function crop(VideoStyle|ImageStyle|StreamStyle $style): void |
||
193 | // @codeCoverageIgnoreEnd |
||
194 | } |
||
195 | } |
||
196 | |||
197 | private function frame(int|float|null $fromSeconds, array $saveTo): void |
||
229 | } |
||
230 | } |
||
231 | |||
232 | public function clone(bool $withMeta = false): FFMpeg |
||
233 | { |
||
234 | $ffmpeg = new self($this->file, $this->disk, $this->dominantColorQuality); |
||
235 | |||
236 | if ($withMeta) { |
||
237 | $ffmpeg->setMeta($this->getMeta()); |
||
238 | } |
||
239 | |||
240 | return $ffmpeg; |
||
241 | } |
||
242 | |||
243 | private function logChannel(): ?LoggerInterface |
||
244 | { |
||
245 | $channel = config('larupload.ffmpeg.log-channel'); |
||
246 | |||
247 | if ($channel === false) { |
||
248 | return null; |
||
249 | } |
||
250 | |||
251 | return Log::channel($channel ?: config('logging.default')); |
||
252 | } |
||
253 | |||
254 | private function dimension(VideoStyle|ImageStyle|StreamStyle $style): Dimension |
||
260 | } |
||
261 | |||
262 | private function dominantColor($path): ?string |
||
263 | { |
||
264 | $file = new UploadedFile($path, basename($path)); |
||
270 |