Completed
Push — master ( 99ade0...004bdb )
by Nicolas
04:25 queued 02:00
created

ImageTranscoder::_updateOutputExtension()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 18
rs 9.4285
cc 2
eloc 13
nc 2
nop 2
1
<?php
2
3
/**
4
 * This class handled Images transcoding
5
 * Here we the input image
6
 * We transcode and generate an output image using ImageMagic (convert)
7
 */
8
9
10
require_once __DIR__.'/BasicTranscoder.php';
11
12
use SA\CpeSdk;
13
14
class ImageTranscoder extends BasicTranscoder
15
{
16
    /***********************
17
     * TRANSCODE INPUT IMAGE
18
     * Below is the code used to transcode images based on $outputWanted. 
19
     **********************/
20
21
    // $metadata should contain the ffprobe video stream array.
22
23
    // Start FFmpeg for output transcoding
24
    public function transcode_asset(
25
        $tmpPathInput,
26
        $pathToInputFile, 
27
        $pathToOutputFiles,
28
        $metadata = null, 
29
        $outputWanted)
30
    {
31
        if ($metadata) {
32
            // Extract an sanitize metadata
33
            $metadata = $this->_extractFileInfo($metadata);
0 ignored issues
show
Bug introduced by
The method _extractFileInfo() does not seem to exist on object<ImageTranscoder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
34
        }
35
36
        $this->cpeLogger->log_out(
37
            "INFO", 
38
            basename(__FILE__), 
39
            "Start Transcoding Asset '$pathToInputFile' ...",
40
            $this->activityLogKey
41
        );
42
43 View Code Duplication
        if ($metadata)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
44
            $this->cpeLogger->log_out(
45
                "INFO", 
46
                basename(__FILE__), 
47
                "Input Video metadata: " . print_r($metadata, true),
48
                $this->activityLogKey
49
            );
50
51
        try {
52
            $convertCmd = "";
53
54
            // Update output extension file if it ends with '.*'
55
            // Output will take the same extension as input
56
            $this->_updateOutputExtension(
57
                $pathToInputFile,
58
                $outputWanted);
59
            
60
            // Custom command
61
            if (isset($outputWanted->{'custom_cmd'}) &&
62
                $outputWanted->{'custom_cmd'}) {
63
                $convertCmd = $this->craft_convert_custom_cmd(
0 ignored issues
show
Bug introduced by
The method craft_convert_custom_cmd() does not seem to exist on object<ImageTranscoder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
64
                    $tmpPathInput,
65
                    $pathToInputFile,
66
                    $pathToOutputFiles,
67
                    $metadata, 
68
                    $outputWanted
69
                );
70
            }
71
            else {
72
                $convertCmd = $this->craft_convert_cmd(
73
                    $tmpPathInput,
74
                    $pathToInputFile,
75
                    $pathToOutputFiles,
76
                    $metadata, 
77
                    $outputWanted
78
                );
79
            }
80
81
            $this->cpeLogger->log_out(
82
                "INFO",
83
                basename(__FILE__),
84
                "CONVERT CMD:\n$convertCmd\n",
85
                $this->activityLogKey
86
            );
87
88
            // Use executer to start FFMpeg command
89
            // Use 'capture_progression' function as callback
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
90
            // Pass video 'duration' as parameter
91
            // Sleep 1sec between turns and callback every 10 turns
92
            // Output progression logs (true)
93
            $this->executer->execute(
94
                $convertCmd, 
95
                1, 
96
                array(2 => array("pipe", "w")),
97
                array($this, "capture_progression"), 
98
                $metadata['duration'], 
99
                true, 
100
                10
101
            );
102
103
            // Test if we have an output file !
104 View Code Duplication
            if (!file_exists($pathToOutputFiles) || 
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
105
                $this->is_dir_empty($pathToOutputFiles)) {
106
                throw new CpeSdk\CpeException(
107
                    "Output file '$pathToOutputFiles' hasn't been created successfully or is empty !",
108
                    self::TRANSCODE_FAIL
109
                );
110
            }
111
112
            // FFProbe the output file and return its information
113
            $output_info =
114
                $this->get_asset_info($pathToOutputFiles."/".$outputWanted->{'output_file_info'}['basename']);
115
        }
116
        catch (\Exception $e) {
117
            $this->cpeLogger->log_out(
118
                "ERROR", 
119
                basename(__FILE__), 
120
                "Execution of command '".$convertCmd."' failed: " . print_r($metadata, true). ". ".$e->getMessage(),
121
                $this->activityLogKey
122
            );
123
            return false;
124
        }
125
126
        // No error. Transcode successful
127
        $this->cpeLogger->log_out(
128
            "INFO", 
129
            basename(__FILE__), 
130
            "Transcoding successfull !",
131
            $this->activityLogKey
132
        );
133
134
        return $output_info;
135
    }
136
137
    // Craft command based on JSON input
138
    private function craft_convert_cmd(
139
        $tmpPathInput,
0 ignored issues
show
Unused Code introduced by
The parameter $tmpPathInput is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
140
        $pathToInputFile,
141
        $pathToOutputFiles,
142
        $metadata, 
0 ignored issues
show
Unused Code introduced by
The parameter $metadata is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
143
        $outputWanted)
144
    {
145
        $convertArgs = "$pathToInputFile ";
146
147
        if (isset($outputWanted->{'quality'})) {
148
            $quality = $outputWanted->{'quality'};
149
            $convertArgs .= "-quality $quality ";
150
        }
151
152
        if (isset($outputWanted->{'crop'})) {
153
            $crop = $outputWanted->{'crop'};
154
            $convertArgs .= "-crop $crop ";
155
        }
156
        
157
        if (isset($outputWanted->{'resize'})) {
158
            $resize = $outputWanted->{'resize'};
159
            $convertArgs .= "-resize $resize ";
160
        }
161
        
162
        // Append output filename to path
163
        $pathToOutputFiles .=
164
            "/" . $outputWanted->{'output_file_info'}['basename'];
165
        
166
        $convertCmd = "convert $convertArgs $pathToOutputFiles";
167
168
        return ($convertCmd);
169
    }
170
    
171
    // Craft custom command
172
    private function craft_ffmpeg_custom_cmd(
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
173
        $tmpPathInput,
0 ignored issues
show
Unused Code introduced by
The parameter $tmpPathInput is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
174
        $pathToInputFile,
175
        $pathToOutputFiles,
176
        $metadata, 
0 ignored issues
show
Unused Code introduced by
The parameter $metadata is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
177
        $outputWanted)
178
    {
179
        $convertCmd = $outputWanted->{'custom_cmd'};
180
181
        // Replace ${input_file} by input file path
182
        $pathToInputFile = escapeshellarg($pathToInputFile);
183
        $convertCmd = preg_replace('/\$\{input_file\}/',
184
            $pathToInputFile, $convertCmd);
185
186
        // Append output filename to path
187
        $pathToOutputFiles .= "/" . $outputWanted->{'output_file_info'}['basename'];
188
        // Replace ${output_file} by output filename and path to local disk
189
        $convertCmd = preg_replace('/\$\{output_file\}/',
190
            $pathToOutputFiles, $convertCmd);
191
        
192
        return ($convertCmd);
193
    }
194
195
    // Check if output file need an update on the extension
196
    private function _updateOutputExtension(
197
        $pathToInputFile,
198
        &$outputWanted)
199
    {
200
        $inputExtension =
201
            pathinfo($pathToInputFile)['extension'];
202
        
203
        // REplace output extension if == * with the input extension
204
        $outputPathInfo =
205
            pathinfo($outputWanted->{'output_file_info'}['basename']);
206
        $outputExtension = $outputPathInfo['extension'];
207
        if ($outputExtension == "*") {
208
            $outputWanted->{'output_file_info'}['basename'] = preg_replace(
209
                '/\*/',
210
                $inputExtension,
211
                $outputWanted->{'output_file_info'}['basename']);
212
        }
213
    }
214
}