55use Islandora \Crayfish \Commons \CmdExecuteService ;
66use Psr \Log \LoggerInterface ;
77use Symfony \Component \HttpFoundation \BinaryFileResponse ;
8+ use Symfony \Component \HttpFoundation \HeaderBag ;
89use Symfony \Component \HttpFoundation \Request ;
910use Symfony \Component \HttpFoundation \Response ;
1011
@@ -112,12 +113,20 @@ public function convert(Request $request)
112113 $ content_types = $ request ->getAcceptableContentTypes ();
113114 [$ content_type , $ format ] = $ this ->getFfmpegFormat ($ content_types );
114115
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+ };
121130
122131 $ temp_file_path = $ this ->tempDirectory . basename (parse_url ($ source , PHP_URL_PATH )) . ". " . $ format ;
123132 $ this ->log ->debug ('Tempfile: ' . $ temp_file_path );
@@ -139,13 +148,31 @@ public function convert(Request $request)
139148 // Add -loglevel error so large files can be processed.
140149 $ args .= ' -loglevel error ' ;
141150 $ 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 ]);
146173
147174 // 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 );
149176 }
150177
151178 /**
@@ -176,21 +203,21 @@ private function getFfmpegFormat(array $content_types): array
176203 }
177204
178205 /**
179- * @param string $cmd_string
206+ * @param array $cmd
180207 * @param string $source
181208 * @param string $path
182209 * @param string $content_type
183210 *
184211 * @return \Symfony\Component\HttpFoundation\Response|\Symfony\Component\HttpFoundation\BinaryFileResponse
185212 */
186213 public function generateDerivativeResponse (
187- string $ cmd_string ,
214+ array $ cmd ,
188215 string $ source ,
189216 string $ path ,
190217 string $ content_type
191218 ) {
192219 try {
193- $ this ->cmd ->execute ($ cmd_string , $ source );
220+ $ this ->cmd ->execute ($ cmd , $ source );
194221 return (new BinaryFileResponse (
195222 $ path ,
196223 200 ,
0 commit comments