Easily deal with SMPTE Timecode format in PHP. If you need a Javascript lib, check out fireworkweb/smpte.js.
You can install the package via composer:
composer require fireworkweb/smpteInclude the Timecode or Validations classes:
useFireworkWeb\SMPTE\Timecode;
useFireworkWeb\SMPTE\Validations;You can instantiate it directly using new:
// passing frame count$timecode = newTimecode(360);
// passing a timecode string$timecode = newTimecode('00:00:01:10');
// passing a Datetime object$timecode = newTimecode(new \DateTime('01:34:12'));Or you can use the static helper:
$timecode = Timecode::fromSeconds(10);| Property | Type | Description |
|---|---|---|
getFrameCount() | int | Total number of frames |
getHours() | int | Hours number |
getMinutes() | int | Minutes number |
getSeconds() | int | Seconds number |
getFrames() | int | Frames number |
durationInSeconds() | int | Timecode duration in seconds |
$time:int|String|Timecodetime to start with.$frameRate:floatframe rate to calculate the timecode.$dropFrame:boolindicates if is drop frame. ONLY WITH 29.97 FPS
$time as int is the frame count to be setted with. To deal with seconds, use fromSeconds.
Note: if $frameRate or $dropFrame are null, it will use the default.
Returns a timecode string representation.
(newTimecode(360))->toString();
// "00:00:15:00"Adds a timecode or a frame count to the current Timecode object.
$time:int|String|Timecodeindicating the value to be added.$operation:intused to get the sign oftime.return:TimecodeReference to theTimecodeobject.
$tc = newTimecode('00:01:00:00');
// Adding from string$tc->add('00:00:30:00')->toString();
// 00:01:30:00// Adding frame count$tc->add(1)->toString();
// 00:01:30:01// Adding from another object$tc2 = newTimecode('00:01:00:00');
$tc->add($tc2)->toString();
// 00:02:30:01Substracts a timecode or a frame count to the current Timecode object.
$time:int|String|Timecodeindicating the value to be added.return:TimecodeReference to theTimecodeobject.
$tc = newTimecode('00:03:00:00');
// Subtracting from string$tc->subtract('00:00:30:00')->toString();
// 00:02:30:00// Subtracting frame count$tc->subtract(1)->toString();
// 00:02:29:23// Subtracting from another object$tc2 = newTimecode('00:01:00:00');
$tc->subtract($tc2)->toString();
// 00:01:29:23Directly set object hours.
$hours:intindicating the value to be setted.
$tc = newTimecode('00:03:00:00');
$tc->setHours(1)->toString();
// 01:03:00:00Directly set object minutes.
$minutes:intindicating the value to be setted.
$tc = newTimecode('00:03:00:00');
$tc->setMinutes(1)->toString();
// 00:01:00:00Directly set object seconds.
$seconds:intindicating the value to be setted.
$tc = newTimecode('00:03:00:00');
$tc->setSeconds(1)->toString();
// 00:03:01:00Directly set object frames.
$frames:intindicating the value to be setted.
$tc = newTimecode('00:03:00:00');
$tc->setFrames(1)->toString();
// 00:03:00:01Directly set object frame count. This will recalculate all other attributes, so use it with care.
$frameCount:intindicating the value to be setted.
$tc = newTimecode('00:03:00:00');
$tc->setFrameCount(360)->toString();
// 00:00:15:00Returns the frame count from a time.
$time:Stringtime as string to calculate.$frameRate:floatframe rate to calculate the timecode.$dropFrame:boolindicates if is drop frame.return:intreturns the frame count
Instantiate a new object from seconds instead of timecode/framecount.
$seconds:intseconds to convert$frameRate:floatframe rate to calculate the timecode.$dropFrame:boolindicates if is drop frame.return:TimecodeReturns the newly created object
$tc = Timecode::fromSeconds(15);
$tc->toString();
// 00:00:15:00Change default frame rate to instantiate objects with.
$frameRate:floatNew default frame rate.
$tc = newTimecode();
$tc->getFrameRate();
// 24
Timecode::setDefaultFrameRate(25);
$tc2 = newTimecode();
$tc2->getFrameRate();
// 25Change default drop frame to instantiate objects with.
$dropFrame:floatNew default drop frame.
$tc = newTimecode();
$tc->getDropFrame();
// false
Timecode::setDefaultDropFrame(true);
$tc2 = newTimecode();
$tc2->getDropFrame();
// trueAll contribution is welcome, please feel free to open tickets and pull requests.