5
5
use Islandora \Crayfish \Commons \CmdExecuteService ;
6
6
use Psr \Log \LoggerInterface ;
7
7
use Symfony \Component \HttpFoundation \BinaryFileResponse ;
8
+ use Symfony \Component \HttpFoundation \HeaderBag ;
8
9
use Symfony \Component \HttpFoundation \Request ;
9
10
use Symfony \Component \HttpFoundation \Response ;
10
11
@@ -112,12 +113,20 @@ public function convert(Request $request)
112
113
$ content_types = $ request ->getAcceptableContentTypes ();
113
114
[$ content_type , $ format ] = $ this ->getFfmpegFormat ($ content_types );
114
115
115
- $ cmd_params = "" ;
116
- if ($ format == "mp4 " ) {
117
- $ cmd_params = " -vcodec libx264 -preset medium -acodec aac " .
118
- "-strict -2 -ab 128k -ac 2 -async 1 -movflags " .
119
- "faststart -y " ;
120
- }
116
+ $ cmd_params = match ($ format ) {
117
+ 'mp4 ' => [
118
+ '-vcodec ' , 'libx264 ' ,
119
+ '-preset ' , 'medium ' ,
120
+ '-acodec ' , 'aac ' ,
121
+ '-strict ' , '-2 ' ,
122
+ '-ab ' , '128k ' ,
123
+ '-ac ' , '2 ' ,
124
+ '-async ' , '1 ' ,
125
+ '-movflags ' , 'faststart ' ,
126
+ '-y ' ,
127
+ ],
128
+ default => [],
129
+ };
121
130
122
131
$ temp_file_path = $ this ->tempDirectory . basename (parse_url ($ source , PHP_URL_PATH )) . ". " . $ format ;
123
132
$ this ->log ->debug ('Tempfile: ' . $ temp_file_path );
@@ -139,13 +148,31 @@ public function convert(Request $request)
139
148
// Add -loglevel error so large files can be processed.
140
149
$ args .= ' -loglevel error ' ;
141
150
$ this ->log ->debug ("X-Islandora-Args: " , ['args ' => $ args ]);
142
- $ token = $ request ->headers ->get ('Authorization ' );
143
- $ headers = "'Authorization: $ token' " ;
144
- $ cmd_string = "$ this ->executable -headers $ headers -i $ source $ args $ cmd_params -f $ format $ temp_file_path " ;
145
- $ this ->log ->debug ('Ffmpeg Command: ' , ['cmd ' => $ cmd_string ]);
151
+
152
+ $ arg_array = array_filter (explode (' ' , $ args ));
153
+
154
+ $ header_bag = new HeaderBag ();
155
+ if ($ token = $ request ->headers ->get ('Authorization ' )) {
156
+ $ header_bag ->set ('Authorization ' , $ token );
157
+ }
158
+
159
+ $ cmd = array_merge (
160
+ [
161
+ $ this ->executable ,
162
+ '-headers ' , $ header_bag ,
163
+ '-i ' , $ source ,
164
+ ],
165
+ $ arg_array ,
166
+ $ cmd_params ,
167
+ [
168
+ '-f ' , $ format ,
169
+ $ temp_file_path ,
170
+ ],
171
+ );
172
+ $ this ->log ->debug ('Ffmpeg Command: ' , ['cmd ' => $ cmd ]);
146
173
147
174
// Return response.
148
- return $ this ->generateDerivativeResponse ($ cmd_string , $ source , $ temp_file_path , $ content_type );
175
+ return $ this ->generateDerivativeResponse ($ cmd , $ source , $ temp_file_path , $ content_type );
149
176
}
150
177
151
178
/**
@@ -176,21 +203,21 @@ private function getFfmpegFormat(array $content_types): array
176
203
}
177
204
178
205
/**
179
- * @param string $cmd_string
206
+ * @param array $cmd
180
207
* @param string $source
181
208
* @param string $path
182
209
* @param string $content_type
183
210
*
184
211
* @return \Symfony\Component\HttpFoundation\Response|\Symfony\Component\HttpFoundation\BinaryFileResponse
185
212
*/
186
213
public function generateDerivativeResponse (
187
- string $ cmd_string ,
214
+ array $ cmd ,
188
215
string $ source ,
189
216
string $ path ,
190
217
string $ content_type
191
218
) {
192
219
try {
193
- $ this ->cmd ->execute ($ cmd_string , $ source );
220
+ $ this ->cmd ->execute ($ cmd , $ source );
194
221
return (new BinaryFileResponse (
195
222
$ path ,
196
223
200 ,
0 commit comments