Conditions | 8 |
Paths | 138 |
Total Lines | 69 |
Code Lines | 40 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
103 | public function generate() : void |
||
104 | { |
||
105 | $ffmpeg = $this->getFFMpeg(); |
||
106 | $videoFormat = $this->getVideoFormat(); |
||
107 | |||
108 | try { |
||
109 | $cutPoints = $this->getCutPoints(); |
||
110 | |||
111 | /** @var Video $video */ |
||
112 | $video = $ffmpeg->open($this->sourceFile); |
||
113 | |||
114 | $files = []; |
||
115 | foreach ($cutPoints as $k => $cutPoint) { |
||
116 | $partFile = $this->options->getTempFolder() . '/' . md5($this->sourceFile) . '_' . ($k + 1) . '.avi'; |
||
117 | |||
118 | //remove audio track |
||
119 | $video->addFilter(new SimpleFilter(['-an'])); |
||
120 | |||
121 | $video->addFilter(new ClipFilter( |
||
122 | TimeCode::fromSeconds($cutPoint), |
||
123 | TimeCode::fromSeconds($this->options->getOutputDuration() / count($cutPoints)) |
||
124 | )); |
||
125 | |||
126 | //FIX: https://stackoverflow.com/questions/20847674/ffmpeg-libx264-height-not-divisible-by-2 |
||
127 | $width = (int) (ceil($this->options->getDimensionWidth() / 2) * 2); |
||
128 | $height = (int) (ceil($this->options->getDimensionWidth() / $this->getRatio() / 2) * 2); |
||
129 | |||
130 | $video->addFilter(new ResizeFilter( |
||
131 | new Dimension($width, $height) |
||
132 | )); |
||
133 | |||
134 | $video->addFilter(new FrameRateFilter(new FrameRate($this->options->getFrameRate()), 1)); |
||
135 | $video->save($videoFormat, $partFile); |
||
136 | |||
137 | $files[] = $partFile; |
||
138 | $this->addTempFileToRemove($partFile); |
||
139 | } |
||
140 | |||
141 | if (count($files) == 1) { |
||
142 | rename($files[0], $this->destination); |
||
143 | } else { |
||
144 | /** @var Video $video */ |
||
145 | $video = $ffmpeg->open($files[0]); |
||
146 | |||
147 | if (is_file($this->destination)) { |
||
148 | unlink($this->destination); |
||
149 | } |
||
150 | |||
151 | $video |
||
152 | ->concat($files) |
||
153 | ->saveFromSameCodecs($this->destination); |
||
154 | } |
||
155 | |||
156 | if ($this->options->getCropRatio() and ($this->options->getCropRatio() != $this->getRatio())) { |
||
157 | $fileCropped = $this->options->getTempFolder() . '/' . md5($this->sourceFile) . '_cropped.avi'; |
||
158 | |||
159 | $video = $ffmpeg->open($this->destination); |
||
160 | $video->addFilter(new CropCenterFilter($this->options->getCropRatio())); |
||
161 | $video->save($videoFormat, $fileCropped); |
||
162 | rename($fileCropped, $this->destination); |
||
163 | } |
||
164 | } catch (RuntimeException $e) { |
||
165 | $message = $e->getMessage(); |
||
166 | |||
167 | if ($e->getPrevious() instanceof ExecutionFailureException) { |
||
168 | $message = $e->getPrevious()->getMessage(); |
||
169 | } |
||
170 | |||
171 | throw new GiffhangerException(sprintf('%s', $message)); |
||
172 | } |
||
175 |