Total Complexity | 40 |
Total Lines | 237 |
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 |
||
68 | { |
||
69 | return $this->media; |
||
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 | $this->meta = FFMpegMeta::make( |
||
89 | width: $meta['width'] ?? null, |
||
90 | height: $meta['height'] ?? null, |
||
91 | duration: $meta['duration'] |
||
92 | ); |
||
93 | } |
||
94 | |||
95 | return $this->meta; |
||
96 | } |
||
97 | |||
98 | public function setMeta(FFMpegMeta $meta): self |
||
99 | { |
||
100 | $this->meta = $meta; |
||
101 | |||
102 | return $this; |
||
103 | } |
||
104 | |||
105 | public function capture(int|float|null $fromSeconds, ImageStyle $style, string $saveTo, bool $withDominantColor = false): ?string |
||
125 | } |
||
126 | |||
127 | public function audio(AudioStyle $style, string $saveTo): void |
||
128 | { |
||
129 | $saveTo = get_larupload_save_path($this->disk, $saveTo, $style->extension()); |
||
130 | |||
131 | $this->media->save($style->format, $saveTo['local']); |
||
132 | |||
133 | larupload_finalize_save($this->disk, $saveTo); |
||
134 | } |
||
135 | |||
136 | public function manipulate(VideoStyle $style, string $saveTo): void |
||
137 | { |
||
138 | $saveTo = get_larupload_save_path($this->disk, $saveTo); |
||
139 | |||
140 | $style->mode->ffmpegResizeFilter() |
||
141 | ? $this->resize($style) |
||
142 | : $this->crop($style); |
||
143 | |||
144 | $this->media->save($style->format, $saveTo['local']); |
||
145 | larupload_finalize_save($this->disk, $saveTo); |
||
146 | } |
||
147 | |||
148 | public function stream(array $styles, string $basePath, string $fileName): bool |
||
149 | { |
||
150 | $hls = new HLS($this, $this->disk); |
||
151 | |||
152 | return $hls->export($styles, $basePath, $fileName); |
||
153 | } |
||
154 | |||
155 | public function resize(VideoStyle|ImageStyle|StreamStyle $style): void |
||
156 | { |
||
157 | $dimension = $this->dimension($style); |
||
158 | |||
159 | if ($style->padding) { |
||
160 | $this->media->filters()->pad($dimension)->synchronize(); |
||
161 | } |
||
162 | else { |
||
163 | $mode = $style->mode->ffmpegResizeFilter() ?? ResizeFilter::RESIZEMODE_SCALE_HEIGHT; |
||
164 | |||
165 | $this->media->filters() |
||
166 | ->resize($dimension, $mode) |
||
167 | ->synchronize(); |
||
168 | } |
||
169 | } |
||
170 | |||
171 | public function crop(VideoStyle|ImageStyle|StreamStyle $style): void |
||
188 | // @codeCoverageIgnoreEnd |
||
189 | } |
||
190 | } |
||
191 | |||
192 | private function frame(int|float|null $fromSeconds, array $saveTo): void |
||
224 | } |
||
225 | } |
||
226 | |||
227 | public function clone(bool $withMeta = false): FFMpeg |
||
228 | { |
||
229 | $ffmpeg = new self($this->file, $this->disk, $this->dominantColorQuality); |
||
230 | |||
231 | if ($withMeta) { |
||
232 | $ffmpeg->setMeta($this->getMeta()); |
||
233 | } |
||
234 | |||
235 | return $ffmpeg; |
||
236 | } |
||
237 | |||
238 | private function logChannel(): ?LoggerInterface |
||
239 | { |
||
240 | $channel = config('larupload.ffmpeg.log-channel'); |
||
241 | |||
242 | if ($channel === false) { |
||
243 | return null; |
||
244 | } |
||
245 | |||
246 | return Log::channel($channel ?: config('logging.default')); |
||
247 | } |
||
248 | |||
249 | private function dimension(VideoStyle|ImageStyle|StreamStyle $style): Dimension |
||
255 | } |
||
256 | |||
257 | private function dominantColor($path): ?string |
||
258 | { |
||
259 | $file = new UploadedFile($path, basename($path)); |
||
263 | } |
||
264 | } |
||
265 |