All files / utils time.js

98.73% Statements 78/79
90.12% Branches 73/81
100% Functions 4/4
98.73% Lines 78/79
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                  39x   13x   13x 13x 13x 13x   13x 13x 13x 13x   13x                     18x   9x 1x     8x 1x       7x 7x 7x 7x 7x 7x     7x     1x 1x   2x 2x 2x     4x 4x 4x 4x 4x       7x 7x                       4x   4x     4x 4x 3x 3x 3x 3x 3x 3x 3x 3x 3x               3x 12x 6x   6x 3x 3x 3x 1x 1x       3x 2x     1x     1x 1x 1x   1x       3x                     2x 1x     1x     1x 1x 1x     1x   1x 2x 2x 1x   2x   1x  
/**
 * Format a numeric time in format '00:00:00'
 *
 * @param {Number} time - Ideally a number, but if not or less than zero, is defaulted to zero
 * @param {Boolean} forceHours
 * @param {Boolean} showFrameCount
 * @param {Number} fps - Frames per second
 * @return {String}
 */
export function secondsToTimeCode (time, forceHours = false, showFrameCount = false, fps = 25) {
 
	time = !time || typeof time !== 'number' || time < 0 ? 0 : time;
 
	let hours = Math.floor(time / 3600) % 24;
	let minutes = Math.floor(time / 60) % 60;
	let seconds = Math.floor(time % 60);
	let frames = Math.floor(((time % 1) * fps).toFixed(3));
 
	let result = (forceHours || hours > 0) ? `${(hours < 10 ? `0${hours}` : hours)}:` : '';
	result += `${(minutes < 10 ? `0${minutes}` : minutes)}:`;
	result += `${(seconds < 10 ? `0${seconds}` : seconds)}`;
	result += `${((showFrameCount) ? `:${(frames < 10 ? `0${frames}` : frames)}` : '')}`;
 
	return result;
}
 
/**
 * Convert a '00:00:00' time string into seconds
 *
 * @param {String} time
 * @param {Boolean} showFrameCount
 * @param {Number} fps - Frames per second
 * @return {Number}
 */
export function timeCodeToSeconds (time, showFrameCount = false, fps = 25) {
 
	if (typeof time !== 'string') {
		throw new Error('Time must be a string');
	}
 
	if (!time.match(/\d{2}(\:\d{2}){0,3}/)) {
		throw new Error('Time code must have the format `00:00:00`');
	}
 
	let
		parts = time.split(':'),
		hours = 0,
		minutes = 0,
		frames = 0,
		seconds = 0,
		output
		;
 
	switch (parts.length) {
		default:
		case 1:
			seconds = parseInt(parts[0], 10);
			break;
		case 2:
			minutes = parseInt(parts[0], 10);
			seconds = parseInt(parts[1], 10);
			break;
		case 3:
		case 4:
			hours = parseInt(parts[0], 10);
			minutes = parseInt(parts[1], 10);
			seconds = parseInt(parts[2], 10);
			frames = showFrameCount ? parseInt(parts[3]) / fps : 0;
			break;
 
	}
 
	output = ( hours * 3600 ) + ( minutes * 60 ) + seconds + frames;
	return parseFloat((output).toFixed(3));
}
 
/**
 * Calculate the time format to use
 *
 * There is a default format set in the options but it can be incomplete, so it is adjusted according to the media
 * duration. Format: 'hh:mm:ss:ff'
 * @param {*} time - Ideally a number, but if not or less than zero, is defaulted to zero
 * @param {Object} options
 * @param {Number} fps - Frames per second
 */
export function calculateTimeFormat (time, options, fps = 25) {
 
	time = !time || typeof time !== 'number' || time < 0 ? 0 : time;
 
	let
		required = false,
		format = options.timeFormat,
		firstChar = format[0],
		firstTwoPlaces = (format[1] === format[0]),
		separatorIndex = firstTwoPlaces ? 2 : 1,
		separator = format.length < separatorIndex ? format[separatorIndex] : ':',
		hours = Math.floor(time / 3600) % 24,
		minutes = Math.floor(time / 60) % 60,
		seconds = Math.floor(time % 60),
		frames = Math.floor(((time % 1) * fps).toFixed(3)),
		lis = [
			[frames, 'f'],
			[seconds, 's'],
			[minutes, 'm'],
			[hours, 'h']
		]
		;
 
	for (let i = 0, len = lis.length; i < len; i++) {
		if (format.indexOf(lis[i][1]) > -1) {
			required = true;
		}
		else if (required) {
			let hasNextValue = false;
			for (let j = i; j < len; j++) {
				if (lis[j][0] > 0) {
					hasNextValue = true;
					break;
				}
			}
 
			if (!hasNextValue) {
				break;
			}
 
			Iif (!firstTwoPlaces) {
				format = firstChar + format;
			}
			format = lis[i][1] + separator + format;
			Eif (firstTwoPlaces) {
				format = lis[i][1] + format;
			}
			firstChar = lis[i][1];
		}
	}
 
	options.currentTimeFormat = format;
}
 
/**
 * Convert Society of Motion Picture and Television Engineers (SMTPE) time code into seconds
 *
 * @param {String} SMPTE
 * @return {Number}
 */
export function convertSMPTEtoSeconds (SMPTE) {
 
	if (typeof SMPTE !== 'string') {
		throw new Error('Argument must be a string value');
	}
 
	SMPTE = SMPTE.replace(',', '.');
 
	let
		secs = 0,
		decimalLen = (SMPTE.indexOf('.') > -1) ? SMPTE.split('.')[1].length : 0,
		multiplier = 1
		;
 
	SMPTE = SMPTE.split(':').reverse();
 
	for (let i = 0; i < SMPTE.length; i++) {
		multiplier = 1;
		if (i > 0) {
			multiplier = Math.pow(60, i);
		}
		secs += Number(SMPTE[i]) * multiplier;
	}
	return Number(secs.toFixed(decimalLen));
}