/** * This file reads cues from a .vtt file using XMLHttpRequest and then * stores them in a new track. * * Reference for adding a track and cues: * http://msdn.microsoft.com/en-us/library/ie/dn265043%28v=vs.85%29.aspx * * /2/4/15 */ Object oReq; String aSourceFile; String aLabel; String aLanguageCode; /** * obtain names of track files for a given videoNo and languageNo. * and then load and process that file. */ void readCuesFromVttFile(String label, String languageCode, String sourceFile) { Object trackInHTML; try { aSourceFile = sourceFile; aLabel = label; aLanguageCode = languageCode; loadVTTPage(sourceFile); } catch (Exception e) { msgStr += NEW_LINE + "***Error in readCuesFromVttFile: " + e; } } // readCuesFromVttFile /** * Loads a page using XMLHttpRequest and then process that * file */ void loadVTTPage(String url) { oReq = new XMLHttpRequest(); // oReq.onload = reqListener(); // oReq.addEventListener("progress", updateProgress, false); oReq.addEventListener("load", transferComplete, false); oReq.addEventListener("error", transferFailed, false); oReq.open("get", url, true); oReq.send(); } // loadVTTPage /** * This method is called as soon as the oReq.open is used. */ /* void reqListener() { } // reqListener */ /** * progress on transfers from the server to the client (downloads) */ /*id updateProgress(Object oEvent) { if (oEvent.lengthComputable) { float percentComplete = oEvent.loaded / oEvent.total; } else { } } // transferComplete */ /** * this method is called when the reading of the vtt file is complete */ void transferComplete(Object evt) { try { // process the file processWebVTT_File(oReq.responseText); // some bookkeeping required for KISVidT.pde // (trackNo must be set to allow modifyCues to work) trackNo = video.textTracks.length - 1; KISVidTBookKeeping(trackNo, aSourceFile + " (XMLHttpRequest)"); } catch (Exception e) { alert("Error in transferComplete: " + e); } } // transferComplete /** * This method will be called should there be an error while reading * the vtt file. */ void transferFailed(Object evt) { int i; i = (int)(evt.total / 1000); getAllProperties(evt, "transferFailed event"); // for debugging alert("Error: An error occurred while transferring the file."); } // transferFailed /** * This routine process a string "fileText" and separates out * the cues from that file. */ void processWebVTT_File(String fileText) { String[] lines; // the fileText after it is split into individual liens int cueNo; int lineNo; int loc; String s, t; String lineMarker; String[] id = new String[1]; // array of cue ids int[] startTime = new int[1]; // array of cue start times int[] endTime = new int[1]; // array of cue end times String[] cueText = new String[1]; // text of the cues try { // split fileText into an array of lines lines = fileText.split("\n"); // make sure that the file begins with "WEBVTT" and a blank line if (lines[0].indexOf("WEBVTT") < 0) { alert("Error: This is not a WEBVTT file. Line 0 is:\n" + lines[0]); return; } if (lines[1].trim().length != 0) { alert("Error: Invalid WEBVTT file. Line 1 is not blank. \n" + lines[1]); return; } // process individual cues cueNo= 0; lineNo = 2; // check for EOF while (true) { // skip blank lines checking for EOF while (lineNo < lines.length && lines[lineNo].trim().length == 0) { lineNo++; } if (lineNo >= lines.length) break; // Ignore comments (i.e. NOTEs) if (lines[lineNo].substr(0, 4) == "NOTE") { lineNo++; while (lineNo < lines.length && lines[lineNo].trim().length > 0) { lineNo++; } if (lineNo >= lines.length) break; } else { // check for an identifier if (lines[lineNo].indexOf(" --> ") < 0) { id[cueNo] = lines[lineNo]; lineNo++; } else { id[cueNo] = ""; } // process times if (lines[lineNo].indexOf(" --> ") < 0) { alert("Error: Invalid WEBVTT file. Invalid time line " + lineNo + "\n" + lines[lineNo]); return; } else { startTime[cueNo] = getTime(lines[lineNo], 0); loc = lines[lineNo].indexOf(" --> ") + 5; while (lines[lineNo].charAt(loc) == "") loc++; endTime[cueNo] = getTime(lines[lineNo], loc); lineNo++; } // process cueText // Note: End of line markers seem to be treated in strange ways. // After experimentation, the following code works but it is not clear // why some of it is needed. s = ""; lineMarker = ""; while (lineNo < lines.length && lines[lineNo].trim().length() != 0) { s += lineMarker + lines[lineNo]; lineMarker = "\n"; lineNo++; } if (s.charAt(s.length()-1) < " ") { t = s.substring(0, s.length()-1); } else t = s; cueText[cueNo] = t; cueNo++; } // while } // end processing a cue makeANewTrack(id, startTime, endTime, cueText); } catch (Exception e) { alert("Error in processWebVTT_File: " + e); } } // processWebVTT_file /** * This function method given a aStr with a time starting at "start" * returns the time in seconds. The times in the string must have the format * hh:mm:ss.sss or mm:ss.sss */ int getTime(String aStr, int start) { int seconds; // Initially this is in hours, then minutes, finally seconds int loc; loc = start; if (aStr.charAt(8) == ".") seconds = parseInt(aStr.substr(loc,2)); // hours else { // hours section was omitted loc -= 3; // skip missing hours section seconds = 0; } seconds = 60 * seconds + parseInt(aStr.substr(loc+3, 2)); // minutes seconds = 60 * seconds + parseFloat(aStr.substr(loc+6, 6)); // seconds return seconds; } // getTime /** * creates a new track and its cues using the info provided in result. */ void makeANewTrack(String[] id, int[] startTime, int[] endTime, String[] cueText) { Track newTextTrack; int i; showInfo("starting makeANewTrack"); newTextTrack = video.addTextTrack("subtitles", aLabel, aLanguageCode); newTextTrack.mode = "hidden"; // temporarily hide the track try { for (i = 0; i < id.length; i++) { newTextTrack.addCue(newCue(startTime[i], endTime[i], cueText[i])); try { // the id must be added independently newTextTrack.cues[i].id = id[i]; } catch (Exception e) { // ignore } } } catch (Exception e) { alert ("Error: while creating a track and adding ques. " + e); } // debug information cueStr = "Finished adding " + trackLangs[videoNo][languageNo] + " cues"; showInfo("makeANewTrack trackId: " + trackId + " languageNo: " + languageNo + " trackNo: " + trackNo+ "
"); } // makeANewTrack