Skip to content

Commit fe707bb

Browse files
author
Dimitri Tarassenko
committed
Added support for Date() conversion to timecode and adding Date()
1 parent 6b2df51 commit fe707bb

4 files changed

Lines changed: 58 additions & 7 deletions

File tree

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
# timecode.js
22

3-
This is a javascript module for manipulating SMPTE timecode.
3+
This is a JavaScript module for manipulating SMPTE timecodes.
44

55
It is primarily based on [pytimecode](http://code.google.com/p/pytimecode/).
66

7-
Theoretically it supports 60, 59.94, 50, 30, 29.97, 25, 24, 23.98 frame rates as well as milliseconds, although only 29.97 (drop and non-drop) and 23.98 have been tested. Timecodes can be created from a number representing the frame count or a string in the form "hh:mm:ss:ff".
7+
Theoretically it supports 60, 59.94, 50, 30, 29.97, 25, 24, 23.98 frame rates as well as milliseconds, although only 29.97 (drop and non-drop) and 23.98 have been tested. Timecodes can be created from:
8+
9+
- a number representing the frame count
10+
- a string in the form "hh:mm:ss:ff"
11+
- a Date() object (year, months and day part are ignored)
812

913
## Usage
1014

@@ -15,7 +19,13 @@ Theoretically it supports 60, 59.94, 50, 30, 29.97, 25, 24, 23.98 frame rates as
1519
> tc.toString();
1620
'01:02:03:04'
1721

22+
> var d = new Date();
23+
> var tc2 = timecode.init({framerate: "29.97", timecode: d, drop_frame:true});
24+
> tc2.add( new Date(0,0,0,0,1,30) );
25+
> tc2.toString();
26+
'17:31:57;22'
27+
1828
## Testing
1929

2030
$ npm install -g nodeunit
21-
$ nodeunit test
31+
$ npm test

lib/timecode.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ var Timecode = {
4646
this.frame_count = timecode;
4747
this.frameNumberToTimecode();
4848
}
49+
else if (timecode instanceof Date) {
50+
this.frame_count = this.dateToFrameNumber(timecode);
51+
this.frameNumberToTimecode();
52+
}
4953
else {
5054
// throw an error
5155
}
@@ -104,8 +108,8 @@ var Timecode = {
104108
var timecode, i, frame_count;
105109
for (i = 0; i < timecodes.length; i += 1) {
106110
timecode = timecodes[i];
107-
// if a string or number is given, convert it to a timecode
108-
if ((typeof timecode === "string") || (typeof timecode === "number")) {
111+
// if a string, number or Date is given, convert it to a timecode
112+
if ((typeof timecode === "string") || (typeof timecode === "number") || (timecode instanceof Date)) {
109113
timecode = Timecode.init({
110114
framerate: this.framerate,
111115
timecode: timecode,
@@ -195,6 +199,17 @@ var Timecode = {
195199
total_minutes = (hours * 60) + minutes,
196200
frame_number = ((hour_frames * hours) + (minute_frames * minutes) + (this.int_framerate * seconds) + frames) - (drop_frames * (total_minutes - Math.floor(total_minutes / 10)));
197201
return frame_number;
202+
},
203+
204+
/**
205+
* Converts the hour, minute, second, millisecond part of Date() object to the number of
206+
* frames using the current framerate
207+
* @param dt {Date}
208+
* @returns {number}
209+
*/
210+
dateToFrameNumber: function(dt) {
211+
var midnight = new Date( dt.getFullYear(), dt.getMonth(), dt.getDate(),0,0,0 );
212+
return Math.floor(((dt-midnight)/1000)*this.framerate);
198213
}
199214
};
200215

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name": "timecode",
3-
"description": "A javascript module for manipulating SMPTE tiemcode.",
3+
"description": "A JavaScript module for manipulating SMPTE timecodes",
44
"homepage": "https://github.com/reidransom/timecode.js",
55
"main": "./lib/timecode.js",
6-
"version": "0.0.3",
6+
"version": "0.0.4",
77
"author": {
88
"name": "Reid Ransom",
99
"email": "reid@reidransom.com",
@@ -17,6 +17,9 @@
1717
"type": "git",
1818
"url": "https://github.com/reidransom/timecode.js.git"
1919
},
20+
"scripts": {
21+
"test": "nodeunit"
22+
},
2023
"devDependencies": {
2124
"nodeunit": "*"
2225
}

test/test-timecode.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,29 @@ exports["Timecode"] = function(test) {
148148
//console.log(tc.frameNumberToDropFrameTimecode(108000));
149149
//console.log(tc.dropFrameTimecodeToFrameNumber([1, 0, 3, 18]));
150150

151+
// Tests for Date() object features.
152+
var d = new Date(0,0,0,0,8,30,200);
153+
tcd = timecode.init({
154+
framerate: "29.97",
155+
timecode: d,
156+
drop_frame: true
157+
});
158+
test.equal("00:08:30;06", tcd.toString());
159+
tcd = timecode.init({
160+
framerate: "29.97",
161+
timecode: new Date(0,0,0,0,8,0,0),
162+
drop_frame: true
163+
});
164+
test.equal("00:07:59;29", tcd.toString());
165+
tcd = timecode.init({
166+
framerate: "29.97",
167+
timecode: new Date(0,0,0,0,8,0,30),
168+
drop_frame: true
169+
});
170+
test.equal("00:08:00;02", tcd.toString());
171+
tcd.add(new Date(0,0,0,0,0,60));
172+
test.equal("00:09:00;02", tcd.toString());
173+
151174
test.done();
152175

153176

0 commit comments

Comments
 (0)