public function PHPVideoToolkit::formatTimecode in Video 7.2
Same name and namespace in other branches
- 7 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.:
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.
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.
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.
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 value
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.
- PHPVideoToolkit::extractFrame in libraries/
phpvideotoolkit/ phpvideotoolkit.php5.php - Extracts exactly one frame
- 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 $extract_begin_timecode='00:00:00' and…
- PHPVideoToolkit::parseFileInfo in libraries/
phpvideotoolkit/ phpvideotoolkit.php5.php - Parses file information returned by ffmpeg -i
File
- libraries/
phpvideotoolkit/ phpvideotoolkit.php5.php, line 2524 - Libary to access FFmpeg
Class
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);
}