Sha256: 889d17fc0a65282e524273998e1da0341d4d7cbe399c30c57a61733b6538f1b8

Contents?: true

Size: 1.72 KB

Versions: 12

Compression:

Stored size: 1.72 KB

Contents

(function($) {
	AblePlayer.prototype.computeEndTime = function(startTime, durationTime) {
		var SECONDS = 0;
		var MINUTES = 1;
		var HOURS = 2;

		var startParts = startTime
			.split(':')
			.reverse()
			.map(function(value) {
				return parseFloat(value);
			});

		var durationParts = durationTime
			.split(':')
			.reverse()
			.map(function(value) {
				return parseFloat(value);
			});

		var endTime = startParts
			.reduce(function(acc, val, index) {
				var sum = val + durationParts[index];

				if (index === SECONDS) {
					if (sum > 60) {
						durationParts[index + 1] += 1;
						sum -= 60;
					}

					sum = sum.toFixed(3);
				}

				if (index === MINUTES) {
					if (sum > 60) {
						durationParts[index + 1] += 1;
						sum -= 60;
					}
				}

				if (sum < 10) {
					sum = '0' + sum;
				}

				acc.push(sum);

				return acc;
			}, [])
			.reverse()
			.join(':');

		return endTime;
	};

	AblePlayer.prototype.ttml2webvtt = function(contents) {
		var thisObj = this;

		var xml = thisObj.convert.xml2json(contents, {
			ignoreComment: true,
			alwaysChildren: true,
			compact: true,
			spaces: 2
		});

		var vttHeader = 'WEBVTT\n\n\n';
		var captions = JSON.parse(xml).tt.body.div.p;

		var vttCaptions = captions.reduce(function(acc, value, index) {
			var text = value._text;
			var isArray = Array.isArray(text);
			var attributes = value._attributes;
			var endTime = thisObj.computeEndTime(attributes.begin, attributes.dur);

			var caption =
				thisObj.computeEndTime(attributes.begin, '00:00:0') +
				' --> ' +
				thisObj.computeEndTime(attributes.begin, attributes.dur) +
				'\n' +
				(isArray ? text.join('\n') : text) +
				'\n\n';

			return acc + caption;
		}, vttHeader);

		return vttCaptions;
	};
})(jQuery);

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
wai-website-theme-1.9.2 assets/ableplayer/scripts/ttml2webvtt.js
wai-website-theme-1.9.pre assets/ableplayer/scripts/ttml2webvtt.js
wai-website-theme-1.7 assets/ableplayer/scripts/ttml2webvtt.js
wai-website-theme-1.6 assets/ableplayer/scripts/ttml2webvtt.js
wai-website-theme-1.5.6 assets/ableplayer/scripts/ttml2webvtt.js
wai-website-theme-1.5.5 assets/ableplayer/scripts/ttml2webvtt.js
wai-website-theme-1.5.2 assets/ableplayer/scripts/ttml2webvtt.js
wai-website-theme-1.5.1 assets/ableplayer/scripts/ttml2webvtt.js
wai-website-theme-1.5 assets/ableplayer/scripts/ttml2webvtt.js
wai-website-theme-1.4.5 assets/ableplayer/scripts/ttml2webvtt.js
wai-website-theme-1.4.1 assets/ableplayer/scripts/ttml2webvtt.js
wai-website-theme-1.4 assets/ableplayer/scripts/ttml2webvtt.js