From 67f19166ac92d5adc03c9421300149260a6424ac Mon Sep 17 00:00:00 2001 From: Guru Prasad Srinivasa Date: Tue, 16 Feb 2016 19:52:08 -0500 Subject: [PATCH 1/2] Some WebVTT fixes to allow manual captions WebVTT allows additional parameters along with the line that contains the start and end times. These were not being filtered out while attempting to manually display captions. --- src/js/plyr.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/js/plyr.js b/src/js/plyr.js index fce746db..2f50bb69 100644 --- a/src/js/plyr.js +++ b/src/js/plyr.js @@ -820,15 +820,21 @@ } // Utilities for caption time codes - function _timecodeMin(tc) { + function _timecodeCommon(tc, pos) { var tcpair = []; tcpair = tc.split(' --> '); - return _subTcSecs(tcpair[0]); + for(var i = 0; i < tcpair.length; i++) { + // WebVTT allows for extra meta data after the timestamp line + // So get rid of this if it exists + tcpair[i] = tcpair[i].replace(/(\d+:\d+:\d+\.\d+).*/, "$1"); + } + return _subTcSecs(tcpair[pos]); + } + function _timecodeMin(tc) { + return _timecodeCommon(tc, 0); } function _timecodeMax(tc) { - var tcpair = []; - tcpair = tc.split(' --> '); - return _subTcSecs(tcpair[1]); + return _timecodeCommon(tc, 1); } function _subTcSecs(tc) { if (tc === null || tc === undefined) { From 402c45ee2bf1d528ea1c3ea333dfa7fcb7dd79af Mon Sep 17 00:00:00 2001 From: Guru Prasad Srinivasa Date: Tue, 16 Feb 2016 19:53:07 -0500 Subject: [PATCH 2/2] Updated manual caption split pattern Previously, Plyr was using a fixed pattern of '\n\n' to split contents into captions. This does not always work as some VTT files contain '\r\n'. This commit checks for both. --- src/js/plyr.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/js/plyr.js b/src/js/plyr.js index 2f50bb69..b2b9c964 100644 --- a/src/js/plyr.js +++ b/src/js/plyr.js @@ -1523,12 +1523,18 @@ record, req = xhr.responseText; - records = req.split('\n\n'); - + var pattern = '\n'; + records = req.split(pattern + pattern); + if(records.length === 1) { + // The '\n' pattern didn't work + // Try '\r\n' + pattern = '\r\n'; + records = req.split(pattern + pattern); + } for (var r = 0; r < records.length; r++) { record = records[r]; plyr.captions[r] = []; - plyr.captions[r] = record.split('\n'); + plyr.captions[r] = record.split(pattern); } // Remove first element ('VTT')