轻松处理PHP中的时间码SMPTE格式

安装次数: 27,118

依赖者: 0

建议者: 0

安全性: 0

星级: 6

关注者: 3

分支: 6

开放问题: 4

类型:项目

1.3.0 2021-04-16 14:54 UTC

This package is auto-updated.

Last update: 2024-09-16 22:09:05 UTC


README

Build Status codecov

轻松处理PHP中的SMPTE时间码格式。如果您需要JavaScript库,请查看fireworkweb/smpte.js

安装

您可以通过composer安装此包

composer require fireworkweb/smpte

用法

包含Timecode或Validations类

use FireworkWeb\SMPTE\Timecode;
use FireworkWeb\SMPTE\Validations;

您可以直接使用new实例化它

// passing frame count
$timecode = new Timecode(360);

// passing a timecode string
$timecode = new Timecode('00:00:01:10');

// passing a Datetime object
$timecode = new Timecode(new \DateTime('01:34:12'));

或者您可以使用静态辅助器

$timecode = Timecode::fromSeconds(10);

属性

对象方法

__construct($time = 0, $frameRate = null, $dropFrame = null)

  • $time: int|String|Timecode 要开始的计时。
  • $frameRate: float 计算时间码的帧率。
  • $dropFrame: bool 表示是否是掉帧。 仅适用于29.97 FPS

$time作为int是设置的帧计数。要处理秒,请使用fromSeconds

注意:如果$frameRate$dropFrame为null,它将使用默认值。

toString() / __toString()

返回时间码字符串表示。

(new Timecode(360))->toString();
// "00:00:15:00"

add($time, $operation = 1)

向当前Timecode对象添加时间码或帧计数。

  • $time: int|String|Timecode 表示要添加的值。
  • $operation: int 用于获取time的符号。
  • return: Timecode Timecode对象的引用。
$tc = new Timecode('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 = new Timecode('00:01:00:00');
$tc->add($tc2)->toString();
// 00:02:30:01

subtract($time)

从当前Timecode对象减去时间码或帧计数。

  • $time: int|String|Timecode 表示要添加的值。
  • return: Timecode Timecode对象的引用。
$tc = new Timecode('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 = new Timecode('00:01:00:00');
$tc->subtract($tc2)->toString();
// 00:01:29:23

setHours($hours)

直接设置对象小时。

  • $hours: int 表示要设置的值。
$tc = new Timecode('00:03:00:00');
$tc->setHours(1)->toString();
// 01:03:00:00

setMinutes($minutes)

直接设置对象分钟。

  • $minutes: int 表示要设置的值。
$tc = new Timecode('00:03:00:00');
$tc->setMinutes(1)->toString();
// 00:01:00:00

setSeconds($seconds)

直接设置对象秒。

  • $seconds: int 表示要设置的值。
$tc = new Timecode('00:03:00:00');
$tc->setSeconds(1)->toString();
// 00:03:01:00

setFrames($frames)

直接设置对象帧。

  • $frames: int 表示要设置的值。
$tc = new Timecode('00:03:00:00');
$tc->setFrames(1)->toString();
// 00:03:00:01

setFrameCount($frameCount)

直接设置对象帧计数。这将重新计算所有其他属性,因此请谨慎使用。

  • $frameCount: int 表示要设置的值。
$tc = new Timecode('00:03:00:00');
$tc->setFrameCount(360)->toString();
// 00:00:15:00

静态方法

frameCountFromTimecode($time, $frameRate = null, $dropFrame = null)

从时间返回帧计数。

  • $time: String 要计算的时间字符串。
  • $frameRate: float 计算时间码的帧率。
  • $dropFrame: bool 表示是否是掉帧。
  • return: int 返回帧计数

fromSeconds($seconds, $frameRate = null, $dropFrame = null)

从秒而不是时间码/帧计数实例化新对象。

  • $seconds: int 要转换的秒数
  • $frameRate: float 计算时间码的帧率。
  • $dropFrame: bool 表示是否是掉帧。
  • return: Timecode 返回新创建的对象
$tc = Timecode::fromSeconds(15);
$tc->toString();
// 00:00:15:00

setDefaultFrameRate($frameRate)

更改实例化对象时使用的默认帧率。

  • $frameRate: float 新默认帧率。
$tc = new Timecode();
$tc->getFrameRate();
// 24

Timecode::setDefaultFrameRate(25);

$tc2 = new Timecode();
$tc2->getFrameRate();
// 25

setDefaultDropFrame($dropFrame)

更改实例化对象时使用的默认掉帧。

  • $dropFrame: float 新默认掉帧。
$tc = new Timecode();
$tc->getDropFrame();
// false

Timecode::setDefaultDropFrame(true);

$tc2 = new Timecode();
$tc2->getDropFrame();
// true

贡献

所有贡献都受到欢迎,请随时提交问题和拉取请求。

许可证

MIT。