-
Notifications
You must be signed in to change notification settings - Fork 1
/
ThumbnailMetadata.js
415 lines (361 loc) · 15.2 KB
/
ThumbnailMetadata.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
ThumbnailMetadata.PREVIOUS_TIME_VALUE = 'previousTimeValue';
ThumbnailMetadata.PREVIOUS_TIME_FORMAT = 'previousTimeFormat';
ThumbnailMetadata.TIME_FORMAT = 'timeFormat';
ThumbnailMetadata.TIME_VALUE = 'timeValue';
ThumbnailMetadata.TIME_CODE_STRUCT = {
'start': 'startTimecode',
'alt': 'altTimecode'
};
ThumbnailMetadata.DEFAULT_FRAMERATE = 25;
ThumbnailMetadata.ALLOWED_MEDIA_TYPES = [
'mp4', 'av1', 'mov', 'ogg', 'ogv', 'mkv', 'webm',
'wav', 'bwf', 'rf64', 'amb', 'acc', 'aif', 'aiff', 'aifc', 'mp2', 'mp3', '3gp', 'wma', 'flac', 'ape'
];
ThumbnailMetadata.METADATA_DATE = {
'created': 0,
'lastChanged': 1
};
/**
* Creates a new thumbnail metadata object.
* @param {Thumbnail} thumbnail Bridge folder element, see CEP reference for Thumbnail object
* @return {object} returns empty object if thumbnail is not a file or the file has no metadata.
*/
function ThumbnailMetadata(thumbnail) {
if (thumbnail.type !== CacheThumbnails.THUMBNAIL_TYPES.file || !thumbnail.hasMetadata) {
return {};
}
this.thumb = thumbnail;
this.filename = thumbnail.name;
this.mimeType = thumbnail.mimeType;
}
/**
* Generates a shortened version of the Thumbnail metadata. For this use case only this.timecodeMetadata.startTime is really relevant.
* @returns {string}
*/
ThumbnailMetadata.prototype.toString = function () {
var text = this.filename + " ";
if (this.timecodeMetadata) {
text += '[' + this.timecodeMetadata.startTime.toString() + '@' + this.timecodeMetadata.framerate.toString() + 'fps]'
}
return text;
}
/**
* Generates a minimal csv row containing file name, start time and framerate.
* @returns {string}
*/
ThumbnailMetadata.prototype.toTimecodeCSV = function () {
if (this.timecodeMetadata == null) {
return '';
}
return [this.filename, this.timecodeMetadata.startTime, this.timecodeMetadata.framerate].join(',');
}
/**
* Extracts timecode metadata from a thumbnails xmp data and sets these values for the object instance.
* @return {boolean} on true, contains no errors, on false has values that errored / are invalid.
*/
ThumbnailMetadata.prototype.extractMetadata = function () {
this.xmp = new XMPMeta(this.thumb.synchronousMetadata.serialize());
if (this.xmp.doesPropertyExist(XMPConst.NS_BWF, "codingHistory")) {
this.audioMetadata = ThumbnailMetadata.extractAudioMetadata(this.xmp);
}
this.timecodeMetadata = ThumbnailMetadata.extractTimecodeMetadata(this.xmp);
if (this.timecodeMetadata.startTime.toFrames() === 0 && this.audioMetadata) {
this.timecodeMetadata.startTime = new Timecode(
Timecode.createTimecodeFromSamples(
this.audioMetadata.samples,
this.audioMetadata.sampleFrequency,
ThumbnailMetadata.DEFAULT_FRAMERATE
),
ThumbnailMetadata.DEFAULT_FRAMERATE
);
this.timecodeMetadata.framerate = ThumbnailMetadata.DEFAULT_FRAMERATE;
}
if (this.timecodeMetadata.framerate === 0) {
return false;
}
return true;
}
/**
* Extracts relevant audio metadata from a thumbnail's metadata.
* @param {XMPMetaInstance} xmp XMP object - see XMP specification
* @returns {object} containing ending, samples, sample frequency and bitrate
*/
ThumbnailMetadata.extractAudioMetadata = function (xmp) {
var sampleFrequency = 0;
if (xmp.doesPropertyExist(XMPConst.NS_DM, 'audioSampleRate')) {
sampleFrequency = xmp.getProperty(XMPConst.NS_DM, 'audioSampleRate').value || '';
} else if (xmp.doesPropertyExist(XMPConst.NS_BWF, 'codingHistory')) {
var audioEncoding = xmp.getProperty(XMPConst.NS_BWF, 'codingHistory').value || '';
audioEncoding = audioEncoding.match(/F=\d+/g).toString();
if (audioEncoding.length === 0) {
sampleFrequency = 0;
} else {
sampleFrequency = Number(audioEncoding.replace('F=', ''));
}
}
var bitRate = audioEncoding.match(/W=\d+/g);
if (bitRate == null || !bitRate.length) {
bitRate = 0;
} else {
bitRate = Number(bitRate[0].replace('W=', ''));
}
var samples = xmp.getProperty(XMPConst.NS_BWF, "timeReference") || 0;
return {
audioEncoding: audioEncoding,
sampleFrequency: sampleFrequency,
bitRate: bitRate,
samples: samples
};
}
/**
* Extracts timecode metadata from xmp metadata.
* @param {XMPMetaInstance} xmp
* @returns {object} containing start time, previous start time, framerate and whether the framerate is dropframe
*/
ThumbnailMetadata.extractTimecodeMetadata = function (xmp) {
var timecodeStruct = '';
if (xmp.doesPropertyExist(XMPConst.NS_DM, this.TIME_CODE_STRUCT.start)) { //prioritice start over alt timecode entries
timecodeStruct = this.TIME_CODE_STRUCT.start;
} else if (xmp.doesPropertyExist(XMPConst.NS_DM, this.TIME_CODE_STRUCT.alt)) {
timecodeStruct = this.TIME_CODE_STRUCT.alt;
}
var framerate = 0;
if (timecodeStruct !== '' && xmp.doesStructFieldExist(XMPConst.NS_DM, timecodeStruct, XMPConst.NS_DM, this.TIME_FORMAT)) {
framerate = this.checkMetadataFramerate(xmp.getStructField(XMPConst.NS_DM, timecodeStruct, XMPConst.NS_DM, this.TIME_FORMAT).value || '');
}
var startTime = new Timecode();
if (timecodeStruct !== '' && xmp.doesStructFieldExist(XMPConst.NS_DM, timecodeStruct, XMPConst.NS_DM, this.TIME_VALUE)) {
startTime = new Timecode(xmp.getStructField(XMPConst.NS_DM, timecodeStruct, XMPConst.NS_DM, this.TIME_VALUE).value, framerate);
}
var prevFramerate = 0;
if (timecodeStruct !== '' && xmp.doesStructFieldExist(XMPConst.NS_DM, timecodeStruct, XMPConst.NS_DM, this.PREVIOUS_TIME_FORMAT)) {
prevFramerate = this.checkMetadataFramerate(xmp.getStructField(XMPConst.NS_DM, timecodeStruct, XMPConst.NS_DM, this.PREVIOUS_TIME_FORMAT).value || '');
}
var prevStartTime = new Timecode();
if (timecodeStruct !== '' && xmp.doesStructFieldExist(XMPConst.NS_DM, timecodeStruct, XMPConst.NS_DM, this.PREVIOUS_TIME_VALUE)) {
prevStartTime = new Timecode(xmp.getStructField(XMPConst.NS_DM, timecodeStruct, XMPConst.NS_DM, this.PREVIOUS_TIME_VALUE).value, prevFramerate);
}
var isDropFrame = Timecode.isDropFrame(framerate);
return {
startTime: startTime,
prevStartTime: prevStartTime,
framerate: framerate,
prevFramerate: prevFramerate,
isDropFrame: isDropFrame,
timecodeStruct: timecodeStruct
}
}
/**
* Checks ThumbnailMetadata object is it contains any invalid framerate or timecode structure.
* It will than update the object with the targetFramerate.
* @param {number} targetFramerate check if input object contains targetFramerate
* @param {boolean} errorOnly
* @returns {boolean|string} true on success
*/
ThumbnailMetadata.prototype.fixFaultyTimecodeMetadata = function (targetFramerate, errorOnly) {
targetFramerate = Number(targetFramerate || '');
if (isNaN(targetFramerate)) {
return false;
}
var hasError = false;
if (this.timecodeMetadata.framerate <= 0) {
hasError = true;
}
if (this.timecodeMetadata.timecodeStruct === '') {
hasError = true;
}
if (errorOnly && !hasError) {
return 'skipped';
}
this.timecodeMetadata.framerate = targetFramerate;
return this.updateTimecodeMetadata(this.timecodeMetadata.startTime.convertByFramerate(this.timecodeMetadata.framerate));
}
/**
* Reverts to the previously stored timecode.
* @returns {boolean} true on success
*/
ThumbnailMetadata.prototype.revertTimecodeChange = function () {
if (this.timecodeMetadata.prevStartTime.toValue() === 0 || this.timecodeMetadata.prevFramerate === 0) {
return false;
}
return this.updateTimecodeMetadata(this.timecodeMetadata.prevStartTime);
}
/**
* Update start time of thumbnail by creation date or time of last modification.
* @param {number|string} targetFramerate
* @param {boolean} overrideFramerate
* @param {number} metadataSelector
* @returns {boolean} true on success
*/
ThumbnailMetadata.prototype.updateFromMetadataDate = function (targetFramerate, overrideFramerate, metadataSelector) {
var dateUpdate = {};
targetFramerate = ThumbnailMetadata.checkMetadataFramerate(targetFramerate);
if (targetFramerate === 0) {
return false;
}
if (this.timecodeMetadata.framerate > 0 && !overrideFramerate) { //add for files with missing time values
targetFramerate = this.timecodeMetadata.framerate;
}
switch (metadataSelector) {
case ThumbnailMetadata.METADATA_DATE.created:
dateUpdate = this.thumb.bestCreationDate;
break;
case ThumbnailMetadata.METADATA_DATE.lastChanged:
dateUpdate = this.thumb.lastModifiedDate;
break;
default:
return false;
}
return this.updateTimecodeMetadata(new Timecode(dateUpdate, targetFramerate));
}
/**
* Updates / changes the starttime of a thumbnail.
* @param {Timecode} timecode
* @param {boolean} overrideFramerate
* @returns true on success
*/
ThumbnailMetadata.prototype.updateFromTimecode = function (timecode, overrideFramerate) {
if (!(timecode instanceof Timecode)) {
return false;
}
if (!overrideFramerate) {
timecode = timecode.convertByFramerate(this.timecodeMetadata.framerate);
}
return this.updateTimecodeMetadata(timecode);
}
/**
* Loops through every update entitiy until a matching filename and or mediastart is found and then updates the object.
* @param {array} updates
* @param {boolean} enableMediaStartComparison
* @param {boolean} overrideFramerate
* @returns true on success
*/
ThumbnailMetadata.prototype.updateFromTimecodes = function (updates, enableMediaStartComparison, overrideFramerate) {
enableMediaStartComparison = enableMediaStartComparison || false; //default false
if (!(updates instanceof Array)) {
return false;
}
var update = this.compareInTimecodeUpdates(updates, enableMediaStartComparison);
if (update) {
return this.updateFromTimecode(update.audioTC, overrideFramerate);
}
return false;
}
/**
* Update a ThumbnailMetadata startTime timecode with a new Timecode object.
* @param {Timecode} newStartTime
* @return {boolean} exits the function with false then the input is not a Timecode object
*/
ThumbnailMetadata.prototype.updateTimecodeMetadata = function (newStartTime) {
if (!(newStartTime instanceof Timecode) || !(this.xmp instanceof XMPMeta)) {
return false;
}
this.timecodeMetadata.prevStartTime = this.timecodeMetadata.startTime;
this.timecodeMetadata.prevFramerate = this.timecodeMetadata.framerate;
this.timecodeMetadata.startTime = newStartTime;
this.timecodeMetadata.framerate = newStartTime.framerate;
if (this.timecodeMetadata.timecodeStruct === '') {
this.timecodeMetadata.timecodeStruct = ThumbnailMetadata.TIME_CODE_STRUCT.alt;
}
this.xmp.setStructField(XMPConst.NS_DM, this.timecodeMetadata.timecodeStruct, XMPConst.NS_DM,
ThumbnailMetadata.TIME_FORMAT, Timecode.createTimeFormat(this.timecodeMetadata.framerate));
this.xmp.setStructField(XMPConst.NS_DM, this.timecodeMetadata.timecodeStruct, XMPConst.NS_DM,
ThumbnailMetadata.TIME_VALUE, this.timecodeMetadata.startTime.toString());
this.xmp.setStructField(XMPConst.NS_DM, this.timecodeMetadata.timecodeStruct, XMPConst.NS_DM,
ThumbnailMetadata.PREVIOUS_TIME_FORMAT, Timecode.createTimeFormat(this.timecodeMetadata.prevFramerate));
this.xmp.setStructField(XMPConst.NS_DM, this.timecodeMetadata.timecodeStruct, XMPConst.NS_DM,
ThumbnailMetadata.PREVIOUS_TIME_VALUE, this.timecodeMetadata.prevStartTime.toString());
if (this.audioMetadata) {
this.audioMetadata.samples = this.timecodeMetadata.startTime.toSamples(this.audioMetadata.sampleFrequency);
this.xmp.setProperty(XMPConst.NS_BWF, "timeReference",
this.audioMetadata.samples.toString(), XMPConst.STRING);
}
if (this.updateThumbnailMetadata()) {
return true;
}
return false;
}
/**
* Loops through every update entitiy until a matching filename and or mediastart is found and returns matching timecode structure (not object)
* @param {array} timecodes
* @param {boolean} enableMediaStartComparison
* @returns {object|undefined} on successfull match returns the matching object in the timecodes array
*/
ThumbnailMetadata.prototype.compareInTimecodeUpdates = function (timecodes, enableMediaStartComparison) {
for (var i = 0; i < timecodes.length; i++) {
if (timecodes[i].fileTC
&& timecodes[i].fileTC instanceof Timecode
&& timecodes[i].name
&& timecodes[i].name.toUpperCase() === this.filename.toUpperCase()
&& (enableMediaStartComparison ?
timecodes[i].fileTC === this.timecodeMetadata.startTime :
true) &&
timecodes[i].audioTC instanceof Timecode) {
return timecodes[i];
}
}
return undefined;
}
/**
* If an XMPMeta object is attached to a ThumbnailMetadata object it attempts to write the new metadata to the thumbnail.
* Basically a wrapper.
* @returns {boolean} true on success
*/
ThumbnailMetadata.prototype.updateThumbnailMetadata = function () {
if (!(this.xmp instanceof XMPMeta)) {
return false;
}
try {
if (this.thumb.locked) {
throw "Thumbnail is locked!";
}
this.thumb.synchronousMetadata = new Metadata(this.xmp.serialize(XMPConst.SERIALIZE_OMIT_PACKET_WRAPPER | XMPConst.SERIALIZE_USE_COMPACT_FORMAT));
} catch (e) {
alert(e);
return false;
}
return true;
}
/**
* Checks the thumbnail metadata for matching timecodes. Returns an object if a matching filename is contained within the timecodes array.
* @param {array} timecodes
* @returns {object} containing essential data for reprocessing
*/
ThumbnailMetadata.prototype.checkMatchingStartTimecodes = function (timecodes) {
var matchedTimecode = this.compareInTimecodeUpdates(timecodes, false); //enableMediaStartComparison has to be disabled, otherwise no errors will be detected
if (matchedTimecode === undefined) {
return undefined;
}
return {
filename: this.filename,
isMatching: matchedTimecode.audioTC.convertByFramerate(this.timecodeMetadata.startTime.framerate).toString() === this.timecodeMetadata.startTime.toString(),
fileTC: {
text: this.timecodeMetadata.startTime.toString(),
framerate: this.timecodeMetadata.startTime.framerate
},
audioTC: {
text: matchedTimecode.audioTC.toString(),
framerate: matchedTimecode.audioTC.framerate
}
}
}
/**
* Validates framerates read from metadata.
* Returns 0 if the framerate was not a number.
* @param {number|string} framerate
* @returns {number} parsed framerate
*/
ThumbnailMetadata.checkMetadataFramerate = function (framerate) {
var parsedFramerate = framerate.toString().match(/\d+/g);
if (parsedFramerate == null || parsedFramerate.length < 1) {
return 0;
}
var newFramerate = Number(parsedFramerate[0]);
if (newFramerate === 23976) {
return 23.976;
}
if (newFramerate >= 1000) {
return newFramerate / 100;
}
return newFramerate;
}