ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
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  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  static $last_return = array();
44 
51  static function enabled()
52  {
53  if (defined("PATH_TO_FFMPEG") && PATH_TO_FFMPEG != "")
54  {
55  return true;
56  }
57  return false;
58  }
59 
69  static function getTargetMimeTypes()
70  {
71  $ttypes = array();
72  foreach (self::$formats as $k => $f)
73  {
74  if ($f["target"] == true)
75  {
76  $ttypes[] = $k;
77  }
78  }
79  return $ttypes;
80  }
81 
88  static function getSourceMimeTypes()
89  {
90  $ttypes = array();
91  foreach (self::$formats as $k => $f)
92  {
93  if ($f["source"] == true)
94  {
95  $ttypes[] = $k;
96  }
97  }
98  return $ttypes;
99  }
100 
106  static function supportsImageExtraction($a_mime)
107  {
108  if (in_array($a_mime, self::getSourceMimeTypes()))
109  {
110  return true;
111  }
112  return false;
113  }
114 
121  function getPossibleTargetMimeTypes($a_source_mime_type)
122  {
123  $pt = array();
124  if (in_array($a_source_mime_type, self::getSourceMimeTypes()))
125  {
126  foreach (self::getTargetMimeTypes() as $tm)
127  {
128  if ($tm != $a_source_mime_type)
129  {
130  $pt[$tm] = $tm;
131  }
132  }
133  }
134  return $pt;
135  }
136 
137 
141  private static function getCmd()
142  {
143  return PATH_TO_FFMPEG;
144  }
145 
152  function exec($args)
153  {
154  return ilUtil::execQuoted(self::getCmd(), $args);
155  }
156 
162  static function getSupportedCodecsInfo()
163  {
164  $codecs = self::exec("-codecs");
165 
166  return $codecs;
167  }
168 
174  static function getSupportedFormatsInfo()
175  {
176  $formats = self::exec("-formats");
177 
178  return $formats;
179  }
180 
187  function getFileInfo()
188  {
189  //$info = `ffmpeg -i $path$file 2>&1 /dev/null`;
190  //@fields = split(/\n/, $info);
191  }
192 
203  static function convert($a_file, $a_target_mime, $a_target_dir = "", $a_target_filename = "")
204  {
205  return; // currently not supported
206 
207  if (self::$formats[$a_target_mime]["target"] != true)
208  {
209  include_once("./Services/MediaObjects/exceptions/class.ilFFmpegException.php");
210  throw new ilFFmpegException("Format ".$a_target_mime." is not supported");
211  }
212  $pars = self::$formats[$a_target_mime]["parameters"];
213  $spi = pathinfo($a_file);
214 
215  // use source directory if no target directory is passed
216  $target_dir = ($a_target_dir != "")
217  ? $a_target_dir
218  : $spi['dirname'];
219 
220  // use source filename if no target filename is passed
221  $target_filename = ($a_target_filename != "")
222  ? $a_target_filename
223  : $spi['filename'].".".self::$formats[$a_target_mime]["suffix"];
224 
225  $target_file = $target_dir."/".$target_filename;
226 
227  $cmd = "-y -i ".ilUtil::escapeShellArg($a_file)." ".$pars." ".ilUtil::escapeShellArg($target_file);
228 
229  $ret = self::exec($cmd." 2>&1");
230  self::$last_return = $ret;
231 
232  if (is_file($target_file))
233  {
234  return $target_file;
235  }
236  else
237  {
238  include_once("./Services/MediaObjects/exceptions/class.ilFFmpegException.php");
239  throw new ilFFmpegException("It was not possible to convert file ".basename($a_file).".");
240  }
241  //ffmpeg -i MOV012.3gp -vcodec libx264 -strict experimental -acodec aac -sameq -ab 64k -ar 44100 MOV012.mp4
242  }
243 
251  {
252  return self::$last_return;
253  }
254 
264  static function extractImage($a_file, $a_target_filename, $a_target_dir = "",
265  $a_sec = 1)
266  {
267 //echo "-$a_file-$a_target_filename-$a_target_dir-$a_sec-<br>";
268 
269  $spi = pathinfo($a_file);
270 
271  // use source directory if no target directory is passed
272  $target_dir = ($a_target_dir != "")
273  ? $a_target_dir
274  : $spi['dirname'];
275 
276  $target_file = $target_dir."/".$a_target_filename;
277 
278  $sec = (int) $a_sec;
279  $cmd = "-y -i ".ilUtil::escapeShellArg($a_file)." -r 1 -f image2 -vframes 1 -ss ".$sec." ".ilUtil::escapeShellArg($target_file);
280 //echo "-$cmd-"; exit;
281  $ret = self::exec($cmd." 2>&1");
282  self::$last_return = $ret;
283 
284  if (is_file($target_file))
285  {
286  return $target_file;
287  }
288  else
289  {
290  include_once("./Services/MediaObjects/exceptions/class.ilFFmpegException.php");
291  throw new ilFFmpegException("It was not possible to extract an image from ".basename($a_file).".");
292  }
293  }
294 
295 }
296 
297 ?>