public function PHPVideoToolkit::formatSeconds in Video 7.2
Same name and namespace in other branches
- 7 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.:
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.
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
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.
- PHPVideoToolkit::formatTimecode in libraries/
phpvideotoolkit/ phpvideotoolkit.php5.php - Translates a timecode to the number of seconds
- PHPVideoToolkit::parseFileInfo in libraries/
phpvideotoolkit/ phpvideotoolkit.php5.php - Parses file information returned by ffmpeg -i
- 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.
File
- libraries/
phpvideotoolkit/ phpvideotoolkit.php5.php, line 2379 - Libary to access FFmpeg
Class
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);
}