You are here

public function PHPVideoToolkit::extractSegment in Video 7

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

* 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 $extract_begin_timecode='00:00:00' and $extract_end_timecode='00:00:01' you might expect because the time span is * 1 second only to get one frame if you set $frames_per_second=1. However this is not correct. The timecode you set in $extract_begin_timecode * acts as the beginning frame. Thus in this example the first frame exported will be from the very beginning of the video, the video will * then move onto the next frame and export a frame there. Therefore if you wish to export just one frame from one position in the video, * say 1 second in you should set $extract_begin_timecode='00:00:01' and set $extract_end_timecode='00:00:01'.) * * @access public *

Parameters

string $extract_begin_timecode A timecode (hh:mm:ss.fn - you can change the timecode format by changing the $timecode_format param: * it obeys the formatting of PHPVideoToolkit::formatTimecode(), see below for more info) * @param string|integer|boolean $extract_end_timecode A timecode (hh:mm:ss.fn - you can change the timecode format by changing the $timecode_format param * it obeys the formatting of PHPVideoToolkit::formatTimecode(), see below for more info) * @param integer $timecode_format The format of the $extract_begin_timecode and $extract_end_timecode timecodes are 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). * - %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 boolean $check_frames_exist Determines if a frame exists check should be made to ensure the timecode given by $extract_end_timecode * actually exists.

File

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

Class

PHPVideoToolkit

Code

public function extractSegment($extract_begin_timecode, $extract_end_timecode, $timecode_format = '%hh:%mm:%ss.%fn', $frames_per_second = false, $check_frames_exist = true) {

  // 			check for frames per second, if it's not set auto set it.
  if ($frames_per_second === false) {
    $info = $this
      ->getFileInfo();
    $frames_per_second = $info['duration']['timecode']['frames']['frame_rate'];
  }

  // 			check if frame exists
  if ($check_frames_exist) {
    if ($info['duration']['seconds'] < floatval($this
      ->formatTimecode($extract_end_timecode, $timecode_format, '%ss.%ms', $frames_per_second))) {

      // 					the input has not returned any video data so the frame rate can not be guessed
      return $this
        ->_raiseError('extractSegment_end_timecode');
    }
    else {
      if ($extract_end_timecode !== false && $info['duration']['seconds'] < floatval($this
        ->formatTimecode($extract_begin_timecode, $timecode_format, '%ss.%ms', $frames_per_second))) {

        // 					the input has not returned any video data so the frame rate can not be guessed
        return $this
          ->_raiseError('extractSegment_begin_timecode');
      }
    }
  }

  // 			format the begin timecode if the timecode format is not already ok.
  $begin_position = (double) $this
    ->formatTimecode($extract_begin_timecode, $timecode_format, '%ss.%ms', $frames_per_second);
  if ($timecode_format !== '%hh:%mm:%ss.%ms') {
    $extract_begin_timecode = $this
      ->formatTimecode($extract_begin_timecode, $timecode_format, '%hh:%mm:%ss.%ms', $frames_per_second);
  }
  $this
    ->addCommand('-ss', $extract_begin_timecode);

  //			allows for exporting the entire timeline
  if ($extract_end_timecode !== false) {
    $end_position = (double) $this
      ->formatTimecode($extract_end_timecode, $timecode_format, '%ss.%ms', $frames_per_second);

    // 				format the end timecode if the timecode format is not already ok.
    if ($timecode_format !== '%hh:%mm:%ss.%ms') {
      $extract_end_timecode = $this
        ->formatTimecode($extract_end_timecode, $timecode_format, '%hh:%mm:%ss.%ms', $frames_per_second);
    }
    $this
      ->addCommand('-t', $end_position - $begin_position);
  }
  return true;
}