You are here

public function PHPVideoToolkit::formatSeconds in Video 7

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

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

Parameters

integer $input_seconds The number of seconds you want to calculate the timecode for.: * @param integer $return_format The format of the timecode to return. The default is * 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 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 string|integer Returns the timecode, but if $frames_per_second is not set and a frame rate lookup is required * but can't be reached then -1 will be returned.

4 calls to PHPVideoToolkit::formatSeconds()
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::formatTimecode in libraries/phpvideotoolkit/phpvideotoolkit.php5.php
* Translates a timecode to the number of seconds * * @access public *
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:…
PHPVideoToolkit::secondsToTimecode in libraries/phpvideotoolkit/phpvideotoolkit.php5.php
* Translates a number of seconds to a timecode. * NOTE: this is now a depreciated, use formatSeconds() instead. * * @depreciated Use formatSeconds() instead. * @access public * @uses PHPVideoToolkit::formatSeconds() *

File

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

Class

PHPVideoToolkit

Code

public function formatSeconds($input_seconds, $return_format = '%hh:%mm:%ss', $frames_per_second = false, $use_smart_values = true) {
  $timestamp = mktime(0, 0, $input_seconds, 0, 0);
  $floored = floor($input_seconds);
  $hours = $input_seconds > 3600 ? floor($input_seconds / 3600) : 0;
  $mins = date('i', $timestamp);
  $searches = array();
  $replacements = array();

  // 			these ones are the simple replacements
  // 			replace the hours
  $using_hours = strpos($return_format, '%hh') !== false;
  if ($using_hours) {
    array_push($searches, '%hh');
    array_push($replacements, $hours);
  }

  // 			replace the minutes
  $using_mins = strpos($return_format, '%mm') !== false;
  if ($using_mins) {
    array_push($searches, '%mm');

    // 				check if hours are being used, if not and hours are required enable smart minutes
    if ($use_smart_values === true && !$using_hours && $hours > 0) {
      $value = $hours * 60 + $mins;
    }
    else {
      $value = $mins;
    }
    array_push($replacements, $value);
  }

  // 			replace the seconds
  if (strpos($return_format, '%ss') !== false) {

    // 				check if hours are being used, if not and hours are required enable smart minutes
    if ($use_smart_values === true && !$using_mins && !$using_hours && $hours > 0) {
      $mins = $hours * 60 + $mins;
    }

    // 				check if mins are being used, if not and hours are required enable smart minutes
    if ($use_smart_values === true && !$using_mins && $mins > 0) {
      $value = $mins * 60 + date('s', $timestamp);
    }
    else {
      $value = date('s', $timestamp);
    }
    array_push($searches, '%ss');
    array_push($replacements, $value);
  }

  // 			replace the milliseconds
  if (strpos($return_format, '%ms') !== false) {
    $milli = round($input_seconds - $floored, 3);
    $milli = substr($milli, 2);
    $milli = empty($milli) ? '0' : $milli;
    array_push($searches, '%ms');
    array_push($replacements, $milli);
  }

  // 			replace the total seconds (rounded)
  if (strpos($return_format, '%st') !== false) {
    array_push($searches, '%st');
    array_push($replacements, round($input_seconds));
  }

  // 			replace the total seconds (floored)
  if (strpos($return_format, '%sf') !== false) {
    array_push($searches, '%sf');
    array_push($replacements, floor($input_seconds));
  }

  // 			replace the total seconds (ceiled)
  if (strpos($return_format, '%sc') !== false) {
    array_push($searches, '%sc');
    array_push($replacements, ceil($input_seconds));
  }

  // 			replace the total seconds
  if (strpos($return_format, '%mt') !== false) {
    array_push($searches, '%mt');
    array_push($replacements, round($input_seconds, 3));
  }

  // 			these are the more complicated as they depend on $frames_per_second / frames per second of the current input
  $has_frames = strpos($return_format, '%fn') !== false;
  $has_total_frames = strpos($return_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['video']) === false || isset($info['video']['frame_rate']) === false)) {

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

    // 				replace the frames
    $excess_frames = false;
    if ($has_frames) {
      $excess_frames = ceil(($input_seconds - $floored) * $frames_per_second);
      array_push($searches, '%fn');
      array_push($replacements, $excess_frames);
    }

    // 				replace the total frames (ie frame number)
    if ($has_total_frames) {
      $round_frames = $floored * $frames_per_second;
      if (!$excess_frames) {
        $excess_frames = ceil(($input_seconds - $floored) * $frames_per_second);
      }
      array_push($searches, '%ft');
      array_push($replacements, $round_frames + $excess_frames);
    }
  }
  return str_replace($searches, $replacements, $return_format);
}