ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
class.ilFFmpeg.php
Go to the documentation of this file.
1<?php
2
24{
25 public static ?array $last_return = array();
26
34 public static array $formats = array(
35 "video/3pgg" => array(
36 "source" => true,
37 "target" => false
38 ),
39 "video/x-flv" => array(
40 "source" => true,
41 "target" => false
42 ),
43 "video/mp4" => array(
44 "source" => true,
45 "target" => true,
46 "parameters" => "-vcodec libx264 -strict experimental -acodec aac -sameq -ab 56k -ar 48000",
47 "suffix" => "mp4"
48 ),
49 "video/webm" => array(
50 "source" => true,
51 "target" => true,
52 "parameters" => "-strict experimental -vcodec libvpx -acodec vorbis -ac 2 -sameq -ab 56k -ar 48000",
53 "suffix" => "webm"
54 )
55 );
56
57
61 public static function enabled(): bool
62 {
63 if (defined("PATH_TO_FFMPEG") && PATH_TO_FFMPEG != "") {
64 return true;
65 }
66 return false;
67 }
68
72 public static function getSourceMimeTypes(): array
73 {
74 $ttypes = array();
75 foreach (self::$formats as $k => $f) {
76 if ($f["source"] == true) {
77 $ttypes[] = $k;
78 }
79 }
80 return $ttypes;
81 }
82
86 public static function supportsImageExtraction(
87 string $a_mime
88 ): bool {
89 if (in_array($a_mime, self::getSourceMimeTypes(), true)) {
90 return true;
91 }
92 return false;
93 }
94
95
99 private static function getCmd(): string
100 {
101 return PATH_TO_FFMPEG;
102 }
103
104 protected static function exec(string $args): array
105 {
106 $win = (stripos(php_uname(), "win") === 0);
107 $cmd = self::getCmd();
108 if ($win && str_contains($cmd, " ") && $cmd[0] !== '"') {
109 $cmd = '"' . $cmd . '"';
110 if ($args) {
111 $cmd .= " " . $args;
112 }
113 } elseif ($args) {
114 $cmd .= " " . $args;
115 }
116 $arr = [];
117 exec($cmd, $arr);
118 return $arr;
119 }
120
121 protected static function escapeShellArg(string $a_arg): string
122 {
123 setlocale(
124 LC_CTYPE,
125 "UTF8",
126 "en_US.UTF-8"
127 ); // fix for PHP escapeshellcmd bug. See: http://bugs.php.net/bug.php?id=45132
128 // see also ilias bug 5630
129 return escapeshellarg($a_arg);
130 }
131
132
136 public static function getLastReturnValues(): ?array
137 {
138 return self::$last_return;
139 }
140
151 public static function extractImage(
152 string $a_file,
153 string $a_target_filename,
154 string $a_target_dir = "",
155 int $a_sec = 1
156 ): string {
157 $spi = pathinfo($a_file);
158
159 // use source directory if no target directory is passed
160 $target_dir = ($a_target_dir != "")
161 ? $a_target_dir
162 : $spi['dirname'];
163
164 $target_file = $target_dir . "/" . $a_target_filename;
165
166 $sec = $a_sec;
167 $cmd = "-y -i " . ilShellUtil::escapeShellArg(
168 $a_file
169 ) . " -r 1 -f image2 -vframes 1 -ss " . $sec . " " . ilShellUtil::escapeShellArg($target_file);
170 $ret = self::exec($cmd . " 2>&1");
171 self::$last_return = $ret;
172
173 if (is_file($target_file)) {
174 return $target_file;
175 } else {
176 throw new ilFFmpegException("It was not possible to extract an image from " . basename($a_file) . ".");
177 }
178 }
179
180 public static function extractPNGFromVideoInZip(
181 string $zip,
182 string $path,
183 int $sec = 1
184 ): string {
185 $zip_archive = new \ZipArchive();
186 if ($zip_archive->open($zip) !== true) {
187 return "";
188 }
189
190 $tfile = ilFileUtils::ilTempnam();
191
192 $path = ltrim($path, "/");
193 $video_stream = false;
194 foreach ([$path, "/" . $path] as $path_in_zip) {
195 $video_stream = $zip_archive->getStream($path_in_zip);
196 if (is_resource($video_stream)) {
197 break;
198 }
199 }
200
201 if (!is_resource($video_stream)) {
202 $zip_archive->close();
203 return "";
204 }
205
206 $tmp_stream = fopen($tfile, "wb");
207 if (!is_resource($tmp_stream)) {
208 fclose($video_stream);
209 $zip_archive->close();
210 return "";
211 }
212 stream_copy_to_stream($video_stream, $tmp_stream);
213 fclose($tmp_stream);
214 fclose($video_stream);
215 $zip_archive->close();
216
217 clearstatcache(true, $tfile);
218 if (!is_file($tfile) || filesize($tfile) === 0) {
219 unlink($tfile);
220 return "";
221 }
222
223 $tmp_file = self::escapeShellArg($tfile);
224 $command2 = self::getCmd() . " -i $tmp_file -f image2 -vframes 1 -ss $sec -vcodec png pipe:1";
225
226 $ret = (string) shell_exec($command2);
227 unlink($tfile);
228 return $ret;
229 }
230}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static array $formats
static array $last_return
static extractPNGFromVideoInZip(string $zip, string $path, int $sec=1)
static getCmd()
Get ffmpeg command.
static enabled()
Checks, whether FFmpeg support is enabled (path is set in the setup)
static getLastReturnValues()
Get last return values.
static extractImage(string $a_file, string $a_target_filename, string $a_target_dir="", int $a_sec=1)
Extract image from video file.
static exec(string $args)
static escapeShellArg(string $a_arg)
static getSourceMimeTypes()
static supportsImageExtraction(string $a_mime)
Check if mime type supports image extraction.
static ilTempnam(?string $a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
static escapeShellArg(string $a_arg)
if(!file_exists('../ilias.ini.php'))