You are here

public function PHPVideoToolkit::formatTimecode in Video 7

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

* Translates a timecode to the number of seconds * * @access public *

Parameters

integer $input_seconds The number of seconds you want to calculate the timecode for.: * @param integer $input_format The format of the timecode is being given in. * default '%hh:%mm:%ss' * - %hh (hours) representative of hours * - %mm (minutes) representative of minutes * - %ss (seconds) representative of seconds * - %fn (frame number) representative of frames (of the current second, not total frames) * - %ms (milliseconds) representative of milliseconds (of the current second, not total milliseconds) (rounded to 3 decimal places) * - %ft (frames total) representative of total frames (ie frame number) * - %st (seconds total) representative of total seconds (rounded). * - %sf (seconds floored) representative of total seconds (floored). * - %sc (seconds ceiled) representative of total seconds (ceiled). * - %mt (milliseconds total) representative of total milliseconds. (rounded to 3 decimal places) * Thus you could use an alternative, '%hh:%mm:%ss:%ms', or '%hh:%mm:%ss' dependent on your usage. * @param integer $return_format The format of the timecode to return. The default is * default '%ts' * - %hh (hours) representative of hours * - %mm (minutes) representative of minutes * - %ss (seconds) representative of seconds * - %fn (frame number) representative of frames (of the current second, not total frames) * - %ms (milliseconds) representative of milliseconds (of the current second, not total milliseconds) (rounded to 3 decimal places) * - %ft (frames total) representative of total frames (ie frame number) * - %st (seconds total) representative of total seconds (rounded). * - %sf (seconds floored) representative of total seconds (floored). * - %sc (seconds ceiled) representative of total seconds (ceiled). * - %mt (milliseconds total) representative of total milliseconds. (rounded to 3 decimal places) * Thus you could use an alternative, '%hh:%mm:%ss:%ms', or '%hh:%mm:%ss' dependent on your usage. * @param mixed|boolean|integer $frames_per_second The number of frames per second to translate for. If left false * the class automagically gets the fps from PHPVideoToolkit::getFileInfo(), but the input has to be set * first for this to work properly. * @param boolean $use_smart_values Default value is true, if a format is found (ie %ss - secs) but no higher format (ie %mm - mins) * is found then if $use_smart_values is true the value of of the format will be totaled. * @return float Returns the value of the timecode in seconds.

6 calls to PHPVideoToolkit::formatTimecode()
PHPVideoToolkit::execute in libraries/phpvideotoolkit/phpvideotoolkit.php5.php
* Commits all the commands and executes the ffmpeg procedure. This will also attempt to validate any outputted files in order to provide * some level of stop and check system. * * @access public *
PHPVideoToolkit::extractFrame in libraries/phpvideotoolkit/phpvideotoolkit.php5.php
* Extracts exactly one frame * * @access public * @uses $toolkit->extractFrames *
PHPVideoToolkit::extractFrames in libraries/phpvideotoolkit/phpvideotoolkit.php5.php
* Extracts frames from a video. * (Note; If set to 1 and the duration set by $extract_begin_timecode and $extract_end_timecode is equal to 1 you get more than one frame. * For example if you set $extract_begin_timecode='00:00:00' and…
PHPVideoToolkit::extractSegment in libraries/phpvideotoolkit/phpvideotoolkit.php5.php
* Extracts a segment of video and/or audio * (Note; If set to 1 and the duration set by $extract_begin_timecode and $extract_end_timecode is equal to 1 you get more than one frame. * For example if you set…
PHPVideoToolkit::getFileInfo in libraries/phpvideotoolkit/phpvideotoolkit.php5.php
* Returns information about the specified file without having to use ffmpeg-php * as it consults the ffmpeg binary directly. This idea for this function has been borrowed from * a French ffmpeg class located:…

... See full list

File

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

Class

PHPVideoToolkit

Code

public function formatTimecode($input_timecode, $input_format = '%hh:%mm:%ss', $return_format = '%ts', $frames_per_second = false, $use_smart_values = true) {

  // 			first we must get the timecode into the current seconds
  $input_quoted = preg_quote($input_format);
  $placeholders = array(
    '%hh',
    '%mm',
    '%ss',
    '%fn',
    '%ms',
    '%ft',
    '%st',
    '%sf',
    '%sc',
    '%mt',
  );
  $seconds = 0;
  $input_regex = str_replace($placeholders, '([0-9]+)', preg_quote($input_format));
  preg_match('/' . $input_regex . '/', $input_timecode, $matches);

  // 			work out the sort order for the placeholders
  $sort_table = array();
  foreach ($placeholders as $key => $placeholder) {
    if (($pos = strpos($input_format, $placeholder)) !== false) {
      $sort_table[$pos] = $placeholder;
    }
  }
  ksort($sort_table);

  // 			check to see if frame related values are in the input
  $has_frames = strpos($input_format, '%fn') !== false;
  $has_total_frames = strpos($input_format, '%ft') !== false;
  if ($has_frames || $has_total_frames) {

    // 				if the fps is false then we must automagically detect it from the input file
    if ($frames_per_second === false) {
      $info = $this
        ->getFileInfo();

      // 					check the information has been received
      if ($info === false || (isset($info['duration']) === false || isset($info['duration']['timecode']['frames']['frame_rate']) === false)) {

        // 						fps cannot be reached so return -1
        return -1;
      }
      $frames_per_second = $info['duration']['timecode']['frames']['frame_rate'];
    }
  }

  // 			increment the seconds with each placeholder value
  $key = 1;
  foreach ($sort_table as $placeholder) {
    if (isset($matches[$key]) === false) {
      break;
    }
    $value = $matches[$key];
    switch ($placeholder) {

      // 					time related ones
      case '%hh':
        $seconds += $value * 3600;
        break;
      case '%mm':
        $seconds += $value * 60;
        break;
      case '%ss':
      case '%sf':
      case '%sc':
        $seconds += $value;
        break;
      case '%ms':
        $seconds += floatval('0.' . $value);
        break;
      case '%st':
      case '%mt':
        $seconds = $value;
        break 1;
        break;

      // 					frame related ones
      case '%fn':
        $seconds += $value / $frames_per_second;
        break;
      case '%ft':
        $seconds = $value / $frames_per_second;
        break 1;
        break;
    }
    $key += 1;
  }

  // 			then we just format the seconds
  return $this
    ->formatSeconds($seconds, $return_format, $frames_per_second, $use_smart_values);
}