You are here

public function PHPVideoToolkit::getFFmpegInfo in Video 7

Same name and namespace in other branches
  1. 7.2 libraries/phpvideotoolkit/phpvideotoolkit.php5.php \PHPVideoToolkit::getFFmpegInfo()

* Returns information about the specified file without having to use ffmpeg-php * as it consults the ffmpeg binary directly. * NOTE: calling this statically for caching to work you must set the temp directory. * * @access public *

Return value

mixed false on error encountered, true otherwise *

8 calls to PHPVideoToolkit::getFFmpegInfo()
PHPVideoToolkit::getAvailableCodecs in libraries/phpvideotoolkit/phpvideotoolkit.php5.php
* Returns the available codecs. * @access public *
PHPVideoToolkit::getAvailableFormats in libraries/phpvideotoolkit/phpvideotoolkit.php5.php
* Returns the available formats. * @access public *
PHPVideoToolkit::hasCodecSupport in libraries/phpvideotoolkit/phpvideotoolkit.php5.php
* Determines if your ffmpeg has particular codec support for encode or decode. * * @access public *
PHPVideoToolkit::hasVHookSupport in libraries/phpvideotoolkit/phpvideotoolkit.php5.php
* Determines if the ffmpeg binary has been compiled with vhook support. * * @access public *
PHPVideoToolkit::setAudioCodec in libraries/phpvideotoolkit/phpvideotoolkit.php5.php
* Sets the audio format for audio outputs * * @access public *

... See full list

File

libraries/phpvideotoolkit/phpvideotoolkit.php5.php, line 600

Class

PHPVideoToolkit

Code

public function getFFmpegInfo($read_from_cache = true, $tmp_dir = false) {
  $cache_file = isset($this) === true || $tmp_dir !== false ? true : false;
  if ($read_from_cache === true && $cache_file !== false) {
    $cache_file = ($tmp_dir === false ? $this->_tmp_directory : $tmp_dir) . '_ffmpeg_info.php';
    if (is_file($cache_file) === true) {
      require_once $cache_file;
      if (isset($info) === true && $info['_cache_date'] > time() - 2678400) {
        $info['reading_from_cache'] = true;
        PHPVideoToolkit::$ffmpeg_info = $info;
      }
    }
  }

  //check to see if the info has already been cached
  if (PHPVideoToolkit::$ffmpeg_info !== false) {
    return PHPVideoToolkit::$ffmpeg_info;
  }

  //check to see if this is a static call
  if (isset($this) === false) {
    $toolkit = new PHPVideoToolkit();
    return $toolkit
      ->getFFmpegInfo($read_from_cache, $tmp_dir);
  }
  $format = '';
  $data = array(
    'reading_from_cache' => false,
  );

  // 			execute the ffmpeg lookup
  $buffer = self::_captureExecBuffer($this->_ffmpeg_binary . ' -formats', $tmp_dir);
  $codecs = self::_captureExecBuffer($this->_ffmpeg_binary . ' -codecs', $tmp_dir);
  $filters = self::_captureExecBuffer($this->_ffmpeg_binary . ' -bsfs', $tmp_dir);
  $protocols = self::_captureExecBuffer($this->_ffmpeg_binary . ' -protocols', $tmp_dir);
  self::$ffmpeg_found = $data['ffmpeg-found'] = !(strpos($buffer[0], 'command not found') !== false || strpos($buffer[0], 'No such file or directory') !== false);
  $data['compiler'] = array();
  $data['binary'] = array();
  $data['ffmpeg-php-support'] = self::hasFFmpegPHPSupport();
  $data['raw'] = implode("\r\n", $buffer) . "\r\n" . implode("\r\n", $codecs) . "\r\n" . implode("\r\n", $filters) . implode("\r\n", $protocols);
  if (!self::$ffmpeg_found) {
    self::$ffmpeg_info = $data;
    return $data;
  }
  $buffer = $data['raw'];

  // 			start building the info array
  $look_ups = array(
    'formats' => 'File formats:',
    'configuration' => 'configuration: ',
    'codecs' => 'Codecs:',
    'filters' => 'Bitstream filters:',
    'protocols' => 'Supported file protocols:',
    'abbreviations' => 'Frame size, frame rate abbreviations:',
    'Note:',
  );
  $total_lookups = count($look_ups);
  $pregs = array();
  $indexs = array();

  // 			search for the content
  foreach ($look_ups as $key => $reg) {
    if (strpos($buffer, $reg) !== false) {
      $index = array_push($pregs, $reg);
      $indexs[$key] = $index;
    }
  }
  preg_match('/' . implode('(.*)', $pregs) . '(.*)/s', $buffer, $matches);
  $configuration = trim($matches[$indexs['configuration']]);

  // 			grab the ffmpeg configuration flags
  preg_match_all('/--[a-zA-Z0-9\\-]+/', $configuration, $config_flags);
  $data['binary']['configuration'] = $config_flags[0];
  $data['binary']['vhook-support'] = in_array('--enable-vhook', $config_flags[0]) || !in_array('--disable-vhook', $config_flags[0]);

  // 			grab the versions
  $data['binary']['versions'] = array();
  preg_match_all('/([a-zA-Z0-9\\-]+) version: ([0-9\\.]+)/', $configuration, $versions);
  for ($i = 0, $a = count($versions[0]); $i < $a; $i++) {
    $data['binary']['versions'][strtolower(trim($versions[1][$i]))] = $versions[2][$i];
  }

  // 			grab the ffmpeg compile info
  preg_match('/built on (.*), gcc: (.*)/', $configuration, $conf);
  if (count($conf) > 0) {
    $data['compiler']['gcc'] = $conf[2];
    $data['compiler']['build_date'] = $conf[1];
    $data['compiler']['build_date_timestamp'] = strtotime($conf[1]);
  }

  // 			grab the file formats available to ffmpeg
  preg_match_all('/ (DE|D|E) (.*) {1,} (.*)/', $matches[$indexs['formats']], $formats);
  $data['formats'] = array();

  // 			loop and clean
  // Formats:
  //  D. = Demuxing supported
  //  .E = Muxing supported
  for ($i = 0, $a = count($formats[0]); $i < $a; $i++) {
    $data['formats'][strtolower(trim($formats[2][$i]))] = array(
      'mux' => $formats[1][$i] == 'DE' || $formats[1][$i] == 'E',
      'demux' => $formats[1][$i] == 'DE' || $formats[1][$i] == 'D',
      'fullname' => $formats[3][$i],
    );
  }

  // 			grab the codecs available
  preg_match_all('/ ([DEVAST ]{0,6}) ([A-Za-z0-9\\_]*) (.*)/', $matches[$indexs['codecs']], $codecs);
  $data['codecs'] = array(
    'video' => array(),
    'audio' => array(),
    'subtitle' => array(),
  );

  // Codecs:
  //  D..... = Decoding supported
  //  .E.... = Encoding supported
  //  ..V... = Video codec
  //  ..A... = Audio codec
  //  ..S... = Subtitle codec
  //  ...S.. = Supports draw_horiz_band
  //  ....D. = Supports direct rendering method 1
  //  .....T = Supports weird frame truncation
  for ($i = 0, $a = count($codecs[0]); $i < $a; $i++) {
    $options = preg_split('//', $codecs[1][$i], -1, PREG_SPLIT_NO_EMPTY);
    if ($options) {
      $id = trim($codecs[2][$i]);
      $type = $options[2] === 'V' ? 'video' : ($options[2] === 'A' ? 'audio' : 'subtitle');
      switch ($options[2]) {

        // 					video
        case 'V':
          $data['codecs'][$type][$id] = array(
            'encode' => isset($options[1]) === true && $options[1] === 'E',
            'decode' => isset($options[0]) === true && $options[0] === 'D',
            'draw_horizontal_band' => isset($options[3]) === true && $options[3] === 'S',
            'direct_rendering_method_1' => isset($options[4]) === true && $options[4] === 'D',
            'weird_frame_truncation' => isset($options[5]) === true && $options[5] === 'T',
            'fullname' => trim($codecs[3][$i]),
          );
          break;

        // 					audio
        case 'A':

        // 					subtitle
        case 'S':
          $data['codecs'][$type][$id] = array(
            'encode' => isset($options[1]) === true && $options[1] === 'E',
            'decode' => isset($options[0]) === true && $options[0] === 'D',
            'fullname' => trim($codecs[3][$i]),
          );
          break;
      }
    }
  }

  // 			grab the bitstream filters available to ffmpeg
  $data['filters'] = array();
  if (isset($indexs['filters']) === true && isset($matches[$indexs['filters']]) === true) {
    $filters = trim($matches[$indexs['filters']]);
    if (empty($filters) === false) {
      $data['filters'] = explode(' ', $filters);
    }
  }

  // 			grab the file prototcols available to ffmpeg
  $data['protocols'] = array();
  if (isset($indexs['protocols']) === true && isset($matches[$indexs['protocols']]) === true) {
    $protocols = trim($matches[$indexs['protocols']]);
    if (empty($protocols) === false) {
      $data['protocols'] = explode(' ', str_replace(':', '', $protocols));
    }
  }

  // 			grab the abbreviations available to ffmpeg
  $data['abbreviations'] = array();
  if (isset($indexs['abbreviations']) === true && isset($matches[$indexs['abbreviations']]) === true) {
    $abbreviations = array_shift(explode("\r", trim($matches[$indexs['abbreviations']])));
    if (empty($abbreviations) === false) {
      $data['abbreviations'] = explode(' ', $abbreviations);
    }
  }
  PHPVideoToolkit::$ffmpeg_info = $data;

  // 			cache the data
  if ($cache_file !== false && $read_from_cache === true) {
    $data['_cache_date'] = time();
    file_put_contents($cache_file, '<?php

	$info = ' . var_export($data, true) . ';');
  }
  return $data;
}