|
1 | 1 | # smpte.php
|
2 | 2 |
|
3 | 3 | [](https://travis-ci.com/fireworkweb/smpte.php)
|
| 4 | +[](https://codecov.io/gh/fireworkweb/smpte.php) |
4 | 5 |
|
5 |
| -Easily deal with Timecode SMPTE format in PHP |
| 6 | +Easily deal with SMPTE Timecode format in PHP. If you need a Javascript lib, check out [fireworkweb/smpte.js](https://github.com/fireworkweb/smpte.js). |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +You can install the package via composer: |
| 11 | + |
| 12 | +```sh |
| 13 | +composer require fireworkweb/smpte |
| 14 | +``` |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +Include the Timecode or Validations classes: |
| 19 | + |
| 20 | +```php |
| 21 | +use FireworkWeb\SMPTE\Timecode; |
| 22 | +use FireworkWeb\SMPTE\Validations; |
| 23 | +``` |
| 24 | + |
| 25 | +You can instantiate it directly using new: |
| 26 | + |
| 27 | +```php |
| 28 | +// passing frame count |
| 29 | +$timecode = new Timecode(360); |
| 30 | + |
| 31 | +// passing a timecode string |
| 32 | +$timecode = new Timecode('00:00:01:10'); |
| 33 | + |
| 34 | +// passing a Datetime object |
| 35 | +$timecode = new Timecode(new \DateTime('01:34:12')); |
| 36 | +``` |
| 37 | + |
| 38 | +Or you can use the static helper: |
| 39 | + |
| 40 | +```php |
| 41 | +$timecode = Timecode::fromSeconds(10); |
| 42 | +``` |
| 43 | + |
| 44 | +### Properties |
| 45 | + |
| 46 | +| Property | Type | Description | |
| 47 | +| --------------------- | ----- | ---------------------------- | |
| 48 | +| `getFrameCount()` | `int` | Total number of frames | |
| 49 | +| `getHours()` | `int` | Hours number | |
| 50 | +| `getMinutes()` | `int` | Minutes number | |
| 51 | +| `getSeconds()` | `int` | Seconds number | |
| 52 | +| `getFrames()` | `int` | Frames number | |
| 53 | +| `durationInSeconds()` | `int` | Timecode duration in seconds | |
| 54 | + |
| 55 | +### Object Methods |
| 56 | + |
| 57 | +#### `__construct($time = 0, $frameRate = null, $dropFrame = null)` |
| 58 | + |
| 59 | +* `$time`: `int|String|Timecode` time to start with. |
| 60 | +* `$frameRate`: `float` frame rate to calculate the timecode. |
| 61 | +* `$dropFrame`: `bool` indicates if is drop frame. **ONLY WITH 29.97 FPS** |
| 62 | + |
| 63 | +`$time` as int is the frame count to be setted with. To deal with seconds, use `fromSeconds`. |
| 64 | + |
| 65 | +**Note:** if `$frameRate` or `$dropFrame` are null, it will use the default. |
| 66 | + |
| 67 | +#### `toString()` / `__toString()` |
| 68 | + |
| 69 | +Returns a timecode string representation. |
| 70 | + |
| 71 | +```php |
| 72 | +(new Timecode(360))->toString(); |
| 73 | +// "00:00:15:00" |
| 74 | +``` |
| 75 | + |
| 76 | +#### `add($time, $operation = 1)` |
| 77 | + |
| 78 | +Adds a timecode or a frame count to the current Timecode object. |
| 79 | + |
| 80 | +* `$time`: `int|String|Timecode` indicating the value to be added. |
| 81 | +* `$operation`: `int` used to get the sign of `time`. |
| 82 | +* `return`: `Timecode` Reference to the `Timecode` object. |
| 83 | + |
| 84 | +```php |
| 85 | +$tc = new Timecode('00:01:00:00'); |
| 86 | + |
| 87 | +// Adding from string |
| 88 | +$tc->add('00:00:30:00')->toString(); |
| 89 | +// 00:01:30:00 |
| 90 | + |
| 91 | +// Adding frame count |
| 92 | +$tc->add(1)->toString(); |
| 93 | +// 00:01:30:01 |
| 94 | + |
| 95 | +// Adding from another object |
| 96 | +$tc2 = new Timecode('00:01:00:00'); |
| 97 | +$tc->add($tc2)->toString(); |
| 98 | +// 00:02:30:01 |
| 99 | +``` |
| 100 | + |
| 101 | +#### `subtract($time)` |
| 102 | + |
| 103 | +Substracts a timecode or a frame count to the current Timecode object. |
| 104 | + |
| 105 | +* `$time`: `int|String|Timecode` indicating the value to be added. |
| 106 | +* `return`: `Timecode` Reference to the `Timecode` object. |
| 107 | + |
| 108 | +```php |
| 109 | +$tc = new Timecode('00:03:00:00'); |
| 110 | + |
| 111 | +// Subtracting from string |
| 112 | +$tc->subtract('00:00:30:00')->toString(); |
| 113 | +// 00:02:30:00 |
| 114 | + |
| 115 | +// Subtracting frame count |
| 116 | +$tc->subtract(1)->toString(); |
| 117 | +// 00:02:29:23 |
| 118 | + |
| 119 | +// Subtracting from another object |
| 120 | +$tc2 = new Timecode('00:01:00:00'); |
| 121 | +$tc->subtract($tc2)->toString(); |
| 122 | +// 00:01:29:23 |
| 123 | +``` |
| 124 | + |
| 125 | +#### `setHours($hours)` |
| 126 | + |
| 127 | +Directly set object hours. |
| 128 | + |
| 129 | +* `$hours`: `int` indicating the value to be setted. |
| 130 | + |
| 131 | +```php |
| 132 | +$tc = new Timecode('00:03:00:00'); |
| 133 | +$tc->setHours(1)->toString(); |
| 134 | +// 01:03:00:00 |
| 135 | +``` |
| 136 | + |
| 137 | +#### `setMinutes($minutes)` |
| 138 | + |
| 139 | +Directly set object minutes. |
| 140 | + |
| 141 | +* `$minutes`: `int` indicating the value to be setted. |
| 142 | + |
| 143 | +```php |
| 144 | +$tc = new Timecode('00:03:00:00'); |
| 145 | +$tc->setMinutes(1)->toString(); |
| 146 | +// 00:01:00:00 |
| 147 | +``` |
| 148 | + |
| 149 | +#### `setSeconds($seconds)` |
| 150 | + |
| 151 | +Directly set object seconds. |
| 152 | + |
| 153 | +* `$seconds`: `int` indicating the value to be setted. |
| 154 | + |
| 155 | +```php |
| 156 | +$tc = new Timecode('00:03:00:00'); |
| 157 | +$tc->setSeconds(1)->toString(); |
| 158 | +// 00:03:01:00 |
| 159 | +``` |
| 160 | + |
| 161 | +#### `setFrames($frames)` |
| 162 | + |
| 163 | +Directly set object frames. |
| 164 | + |
| 165 | +* `$frames`: `int` indicating the value to be setted. |
| 166 | + |
| 167 | +```php |
| 168 | +$tc = new Timecode('00:03:00:00'); |
| 169 | +$tc->setFrames(1)->toString(); |
| 170 | +// 00:03:00:01 |
| 171 | +``` |
| 172 | + |
| 173 | +#### `setFrameCount($frameCount)` |
| 174 | + |
| 175 | +Directly set object frame count. This will recalculate all other attributes, so use it with care. |
| 176 | + |
| 177 | +* `$frameCount`: `int` indicating the value to be setted. |
| 178 | + |
| 179 | +```php |
| 180 | +$tc = new Timecode('00:03:00:00'); |
| 181 | +$tc->setFrameCount(360)->toString(); |
| 182 | +// 00:00:15:00 |
| 183 | +``` |
| 184 | + |
| 185 | +### Static Methods |
| 186 | + |
| 187 | +#### `frameCountFromTimecode($time, $frameRate = null, $dropFrame = null)` |
| 188 | + |
| 189 | +Returns the frame count from a time. |
| 190 | + |
| 191 | +* `$time`: `String` time as string to calculate. |
| 192 | +* `$frameRate`: `float` frame rate to calculate the timecode. |
| 193 | +* `$dropFrame`: `bool` indicates if is drop frame. |
| 194 | +* `return`: `int` returns the frame count |
| 195 | + |
| 196 | +#### `fromSeconds($seconds, $frameRate = null, $dropFrame = null)` |
| 197 | + |
| 198 | +Instantiate a new object from seconds instead of timecode/framecount. |
| 199 | + |
| 200 | +* `$seconds`: `int` seconds to convert |
| 201 | +* `$frameRate`: `float` frame rate to calculate the timecode. |
| 202 | +* `$dropFrame`: `bool` indicates if is drop frame. |
| 203 | +* `return`: `Timecode` Returns the newly created object |
| 204 | + |
| 205 | +```php |
| 206 | +$tc = Timecode::fromSeconds(15); |
| 207 | +$tc->toString(); |
| 208 | +// 00:00:15:00 |
| 209 | +``` |
| 210 | + |
| 211 | +#### `setDefaultFrameRate($frameRate)` |
| 212 | + |
| 213 | +Change default frame rate to instantiate objects with. |
| 214 | + |
| 215 | +* `$frameRate`: `float` New default frame rate. |
| 216 | + |
| 217 | +```php |
| 218 | +$tc = new Timecode(); |
| 219 | +$tc->getFrameRate(); |
| 220 | +// 24 |
| 221 | + |
| 222 | +Timecode::setDefaultFrameRate(25); |
| 223 | + |
| 224 | +$tc2 = new Timecode(); |
| 225 | +$tc2->getFrameRate(); |
| 226 | +// 25 |
| 227 | +``` |
| 228 | + |
| 229 | +#### `setDefaultDropFrame($dropFrame)` |
| 230 | + |
| 231 | +Change default drop frame to instantiate objects with. |
| 232 | + |
| 233 | +* `$dropFrame`: `float` New default drop frame. |
| 234 | + |
| 235 | +```php |
| 236 | +$tc = new Timecode(); |
| 237 | +$tc->getDropFrame(); |
| 238 | +// false |
| 239 | + |
| 240 | +Timecode::setDefaultDropFrame(true); |
| 241 | + |
| 242 | +$tc2 = new Timecode(); |
| 243 | +$tc2->getDropFrame(); |
| 244 | +// true |
| 245 | +``` |
| 246 | + |
| 247 | +## Contributing |
| 248 | +All contribution is welcome, please feel free to open tickets and pull requests. |
| 249 | + |
| 250 | +## License |
| 251 | + |
| 252 | +[MIT.](LICENSE) |
0 commit comments