Skip to content

Commit 68174d1

Browse files
authored
Merge pull request #14 from seabasss/fix/rounding-duration-seconds
Add multiple duration calculation methods in Timecode class
2 parents d8ae864 + 92e5da8 commit 68174d1

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

src/Timecode.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,15 +233,60 @@ public function calculateFrameCountWithDropFrame() : float
233233
}
234234

235235
/**
236-
* Get total of seconds based on frame count
236+
* Get total duration in seconds based on frame count, rounded down to the nearest second
237237
*
238238
* @return int
239239
*/
240240
public function durationInSeconds() : int
241+
{
242+
return (int) ($this->frameCount / $this->frameRate);
243+
}
244+
245+
/**
246+
* Get total duration in seconds based on frame count, rounded to the nearest second
247+
*
248+
* @return int
249+
*/
250+
public function durationInSecondsRounded() : int
241251
{
242252
return (int) round($this->frameCount / $this->frameRate);
243253
}
244254

255+
/**
256+
* Get total duration in seconds based on frame count, rounded to the nearest second
257+
* Ensures a minimum of 1 second if frames are present
258+
*
259+
* @return int
260+
*/
261+
public function durationInSecondsRoundedMinOne() : int
262+
{
263+
if ($this->frameCount === 0) {
264+
return 0;
265+
}
266+
267+
return max(1, (int) round($this->frameCount / $this->frameRate));
268+
}
269+
270+
/**
271+
* Get total duration in seconds based on frame count, rounded up to the nearest second
272+
*
273+
* @return int
274+
*/
275+
public function durationInSecondsRoundedUp() : int
276+
{
277+
return (int) ceil($this->frameCount / $this->frameRate);
278+
}
279+
280+
/**
281+
* Get total duration in seconds with fractional precision based on frame count
282+
*
283+
* @return float
284+
*/
285+
public function durationInSecondsWithFractions() : float
286+
{
287+
return $this->frameCount / $this->frameRate;
288+
}
289+
245290
/**
246291
* Adds a timecode or a frame count to the current SMPTE object
247292
*

0 commit comments

Comments
 (0)