ILIAS  release_8 Revision v8.23
class.ilFFmpeg.php
Go to the documentation of this file.
1 <?php
2 
23 class ilFFmpeg
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 getTargetMimeTypes(): array
73  {
74  $ttypes = array();
75  foreach (self::$formats as $k => $f) {
76  if ($f["target"] == true) {
77  $ttypes[] = $k;
78  }
79  }
80  return $ttypes;
81  }
82 
86  public static function getSourceMimeTypes(): array
87  {
88  $ttypes = array();
89  foreach (self::$formats as $k => $f) {
90  if ($f["source"] == true) {
91  $ttypes[] = $k;
92  }
93  }
94  return $ttypes;
95  }
96 
100  public static function supportsImageExtraction(
101  string $a_mime
102  ): bool {
103  if (in_array($a_mime, self::getSourceMimeTypes(), true)) {
104  return true;
105  }
106  return false;
107  }
108 
109 
113  private static function getCmd(): string
114  {
115  return PATH_TO_FFMPEG;
116  }
117 
121  public static function exec(string $args): array
122  {
123  return ilShellUtil::execQuoted(self::getCmd(), $args);
124  }
125 
129  public static function getLastReturnValues(): ?array
130  {
131  return self::$last_return;
132  }
133 
144  public static function extractImage(
145  string $a_file,
146  string $a_target_filename,
147  string $a_target_dir = "",
148  int $a_sec = 1
149  ): string {
150  $spi = pathinfo($a_file);
151 
152  // use source directory if no target directory is passed
153  $target_dir = ($a_target_dir != "")
154  ? $a_target_dir
155  : $spi['dirname'];
156 
157  $target_file = $target_dir . "/" . $a_target_filename;
158 
159  $sec = $a_sec;
160  $cmd = "-y -i " . ilShellUtil::escapeShellArg(
161  $a_file
162  ) . " -r 1 -f image2 -vframes 1 -ss " . $sec . " " . ilShellUtil::escapeShellArg($target_file);
163  $ret = self::exec($cmd . " 2>&1");
164  self::$last_return = $ret;
165 
166  if (is_file($target_file)) {
167  return $target_file;
168  } else {
169  throw new ilFFmpegException("It was not possible to extract an image from " . basename($a_file) . ".");
170  }
171  }
172 }
static getTargetMimeTypes()
Get desired target mime types.
static getLastReturnValues()
Get last return values.
static array $formats
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getSourceMimeTypes()
static exec(string $args)
Execute ffmpeg.
static escapeShellArg(string $a_arg)
static getCmd()
Get ffmpeg command.
static extractImage(string $a_file, string $a_target_filename, string $a_target_dir="", int $a_sec=1)
Extract image from video file.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static supportsImageExtraction(string $a_mime)
Check if mime type supports image extraction.
static execQuoted(string $cmd, ?string $args=null)
static array $last_return
static enabled()
Checks, whether FFmpeg support is enabled (path is set in the setup)
$formats
Definition: date.php:77