ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilFFmpeg.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
12 class ilFFmpeg
13 {
20  public static $formats = array(
21  "video/3pgg" => array(
22  "source" => true,
23  "target" => false
24  ),
25  "video/x-flv" => array(
26  "source" => true,
27  "target" => false
28  ),
29  "video/mp4" => array(
30  "source" => true,
31  "target" => true,
32  "parameters" => "-vcodec libx264 -strict experimental -acodec aac -sameq -ab 56k -ar 48000",
33  "suffix" => "mp4"
34  ),
35  "video/webm" => array(
36  "source" => true,
37  "target" => true,
38  "parameters" => "-strict experimental -vcodec libvpx -acodec vorbis -ac 2 -sameq -ab 56k -ar 48000",
39  "suffix" => "webm"
40  )
41  );
42 
43  public static $last_return = array();
44 
51  public static function enabled()
52  {
53  if (defined("PATH_TO_FFMPEG") && PATH_TO_FFMPEG != "") {
54  return true;
55  }
56  return false;
57  }
58 
68  public static function getTargetMimeTypes()
69  {
70  $ttypes = array();
71  foreach (self::$formats as $k => $f) {
72  if ($f["target"] == true) {
73  $ttypes[] = $k;
74  }
75  }
76  return $ttypes;
77  }
78 
85  public static function getSourceMimeTypes()
86  {
87  $ttypes = array();
88  foreach (self::$formats as $k => $f) {
89  if ($f["source"] == true) {
90  $ttypes[] = $k;
91  }
92  }
93  return $ttypes;
94  }
95 
101  public static function supportsImageExtraction($a_mime)
102  {
103  if (in_array($a_mime, self::getSourceMimeTypes())) {
104  return true;
105  }
106  return false;
107  }
108 
115  public static function getPossibleTargetMimeTypes($a_source_mime_type)
116  {
117  $pt = array();
118  if (in_array($a_source_mime_type, self::getSourceMimeTypes())) {
119  foreach (self::getTargetMimeTypes() as $tm) {
120  if ($tm != $a_source_mime_type) {
121  $pt[$tm] = $tm;
122  }
123  }
124  }
125  return $pt;
126  }
127 
128 
132  private static function getCmd()
133  {
134  return PATH_TO_FFMPEG;
135  }
136 
143  public static function exec($args)
144  {
145  return ilUtil::execQuoted(self::getCmd(), $args);
146  }
147 
153  public static function getSupportedCodecsInfo()
154  {
155  $codecs = self::exec("-codecs");
156 
157  return $codecs;
158  }
159 
165  public static function getSupportedFormatsInfo()
166  {
167  $formats = self::exec("-formats");
168 
169  return $formats;
170  }
171 
178  public function getFileInfo()
179  {
180  //$info = `ffmpeg -i $path$file 2>&1 /dev/null`;
181  //@fields = split(/\n/, $info);
182  }
183 
194  public static function convert($a_file, $a_target_mime, $a_target_dir = "", $a_target_filename = "")
195  {
196  return; // currently not supported
197 
198  if (self::$formats[$a_target_mime]["target"] != true) {
199  include_once("./Services/MediaObjects/exceptions/class.ilFFmpegException.php");
200  throw new ilFFmpegException("Format " . $a_target_mime . " is not supported");
201  }
202  $pars = self::$formats[$a_target_mime]["parameters"];
203  $spi = pathinfo($a_file);
204 
205  // use source directory if no target directory is passed
206  $target_dir = ($a_target_dir != "")
207  ? $a_target_dir
208  : $spi['dirname'];
209 
210  // use source filename if no target filename is passed
211  $target_filename = ($a_target_filename != "")
212  ? $a_target_filename
213  : $spi['filename'] . "." . self::$formats[$a_target_mime]["suffix"];
214 
215  $target_file = $target_dir . "/" . $target_filename;
216 
217  $cmd = "-y -i " . ilUtil::escapeShellArg($a_file) . " " . $pars . " " . ilUtil::escapeShellArg($target_file);
218 
219  $ret = self::exec($cmd . " 2>&1");
220  self::$last_return = $ret;
221 
222  if (is_file($target_file)) {
223  return $target_file;
224  } else {
225  include_once("./Services/MediaObjects/exceptions/class.ilFFmpegException.php");
226  throw new ilFFmpegException("It was not possible to convert file " . basename($a_file) . ".");
227  }
228  //ffmpeg -i MOV012.3gp -vcodec libx264 -strict experimental -acodec aac -sameq -ab 64k -ar 44100 MOV012.mp4
229  }
230 
237  public static function getLastReturnValues()
238  {
239  return self::$last_return;
240  }
241 
251  public static function extractImage(
252  $a_file,
253  $a_target_filename,
254  $a_target_dir = "",
255  $a_sec = 1
256  ) {
257  //echo "-$a_file-$a_target_filename-$a_target_dir-$a_sec-<br>";
258 
259  $spi = pathinfo($a_file);
260 
261  // use source directory if no target directory is passed
262  $target_dir = ($a_target_dir != "")
263  ? $a_target_dir
264  : $spi['dirname'];
265 
266  $target_file = $target_dir . "/" . $a_target_filename;
267 
268  $sec = (int) $a_sec;
269  $cmd = "-y -i " . ilUtil::escapeShellArg($a_file) . " -r 1 -f image2 -vframes 1 -ss " . $sec . " " . ilUtil::escapeShellArg($target_file);
270  //echo "-$cmd-"; exit;
271  $ret = self::exec($cmd . " 2>&1");
272  self::$last_return = $ret;
273 
274  if (is_file($target_file)) {
275  return $target_file;
276  } else {
277  include_once("./Services/MediaObjects/exceptions/class.ilFFmpegException.php");
278  throw new ilFFmpegException("It was not possible to extract an image from " . basename($a_file) . ".");
279  }
280  }
281 }
static getTargetMimeTypes()
Get target mime types.
static getLastReturnValues()
Get last return values.
getFileInfo()
Get file info.
static convert($a_file, $a_target_mime, $a_target_dir="", $a_target_filename="")
Convert file to target mime type.
static getSupportedCodecsInfo()
Get all supported codecs.
Class for ffmpeg exception handling in ILIAS.
static getSourceMimeTypes()
Get source mime types.
static getSupportedFormatsInfo()
Get all supported formats.
static getCmd()
Get ffmpeg command.
FFmpeg wrapper.
static $formats
Formats handled by ILIAS.
static extractImage( $a_file, $a_target_filename, $a_target_dir="", $a_sec=1)
Extract image from video file.
static getPossibleTargetMimeTypes($a_source_mime_type)
Get possible target formats.
static $last_return
static execQuoted($cmd, $args=null)
exec command and fix spaces on windows
static exec($args)
Execute ffmpeg.
static escapeShellArg($a_arg)
$ret
Definition: parser.php:6
static enabled()
Checks, whether FFmpeg support is enabled (path is set in the setup)
$formats
Definition: date.php:77
static supportsImageExtraction($a_mime)
Check if mime type supports image extraction.