Skip to content

Commit 3502457

Browse files
docs: updated readme and added readme tests
1 parent fc04627 commit 3502457

File tree

2 files changed

+88
-3
lines changed

2 files changed

+88
-3
lines changed

README.md

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,64 @@
1-
# @spiretechnology/js-timecode
1+
# js-timecode
22

3-
SMPTE timecode parser and formatter for JavaScript and TypeScript.
3+
A TypeScript / JavaScript library for parsing and manipulating SMPTE timecodes and frame rates.
4+
5+
Development of this library is very test-driven to ensure accuracy of the frame and timecode calculations. If you'd like to contribute to this library, adding additional useful test cases is a great place to start!
6+
7+
## Installation
8+
9+
```sh
10+
npm install --save @spiretechnology/js-timecode
11+
```
12+
13+
## Usage Examples
14+
15+
### Parse a timecode (drop frame)
16+
17+
```ts
18+
import { Parse, Rate_23_976 } from '@spiretechnology/js-timecode';
19+
20+
const tc = Parse('00:01:02;23', Rate_23_976);
21+
tc.toString(); // => 00:01:02;23
22+
tc.frame; // => 1509
23+
```
24+
25+
### Parse a timecode (non-drop frame)
26+
27+
```ts
28+
const tc = Parse('00:01:02:23', Rate_24);
29+
tc.toString(); // => 00:01:02:23
30+
tc.frame; // => 1511
31+
```
32+
33+
### Create a timecode from a frame count
34+
35+
```ts
36+
const tc = new Timecode(1511, Rate_24, false /* non-drop frame */);
37+
tc.toString(); // => 00:01:02:23
38+
tc.frame; // => 1511
39+
```
40+
41+
### Algebra with timecodes and frames
42+
43+
```ts
44+
let tc = Parse('00:01:02:23', Rate_24);
45+
tc = tc.add(3);
46+
tc.toString(); // => 00:01:03:02
47+
tc.frame; // => 1514
48+
```
49+
50+
## Note: parsing timecodes that don't exist in drop frame
51+
52+
Drop frame timecodes skip the first 2 frames of each minute, unless the minute is a multiple of 10. This changes to the first 4 frames of each minute if the frame rate is 59.94.
53+
54+
For instance, in `23.976`, the timecode `00:00:59:23` is immediately followed by `00:01:00:02`. Two timecodes were dropped: `00:01:00:00` and `00:01:00:01`
55+
56+
Those dropped timecodes don't correspond to any actual frame number, and so we need to choose how to resolve those frames. The choice we have made with this library is to round up the next valid frame. If you try to parse `00:01:00:00`, the result will be rounded up to `00:01:00:02`, which is the next valid frame in the sequence.
57+
58+
## Contributing
59+
60+
We welcome contributions that make this library more reliable. To add test cases, fix bugs, or anything else, please submit a pull request.
61+
62+
## Other resources
63+
64+
- [spiretechnology/go-timecode](https://github.com/spiretechnology.com/go-timecode) - A Go library for parsing and manipulating SMPTE timecodes and frame rates.

src/timecode.test.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Parse } from './parse';
2-
import { Rate_24, Rate_59_94 } from './rate';
2+
import { Rate_23_976, Rate_24, Rate_59_94 } from './rate';
33
import { Timecode } from './timecode';
44

55
describe('testing 59.94 fps (DF) parse to frame count', () => {
@@ -86,3 +86,27 @@ describe('testing 24 fps (NDF) offsets', () => {
8686
expect(ninetyMinutesLater.toString()).toBe('16:25:41:22');
8787
});
8888
});
89+
90+
describe('readme example tests', () => {
91+
test('parse a timecode (drop frame)', () => {
92+
const tc = Parse('00:01:02;23', Rate_23_976);
93+
expect(tc.toString()).toBe('00:01:02;23');
94+
expect(tc.frame).toBe(1509);
95+
});
96+
test('parse a timecode (non-drop frame)', () => {
97+
const tc = Parse('00:01:02:23', Rate_24);
98+
expect(tc.toString()).toBe('00:01:02:23');
99+
expect(tc.frame).toBe(1511);
100+
});
101+
test('create a timecode from a frame count', () => {
102+
const tc = new Timecode(1511, Rate_24, false /* non-drop frame */);
103+
expect(tc.toString()).toBe('00:01:02:23');
104+
expect(tc.frame).toBe(1511);
105+
});
106+
test('algebra with timecodes and frames', () => {
107+
let tc = Parse('00:01:02:23', Rate_24);
108+
tc = tc.add(3);
109+
expect(tc.toString()).toBe('00:01:03:02');
110+
expect(tc.frame).toBe(1514);
111+
});
112+
});

0 commit comments

Comments
 (0)