1
0

index.html 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <html>
  2. <head>
  3. <script src="vtt.js"></script>
  4. <script>
  5. function playbackCaptions (url) {
  6. var captions = document.querySelector(".captions");
  7. fetch(url)
  8. .then(function (r) { return r.text(); })
  9. .then(function (vttContent) {
  10. var parser = new WebVTT.Parser(window, WebVTT.StringDecoder()),
  11. cues = [],
  12. regions = [];
  13. parser.oncue = function(cue) {
  14. cues.push(cue);
  15. };
  16. parser.onregion = function(region) {
  17. regions.push(region);
  18. }
  19. parser.parse(vttContent);
  20. parser.flush();
  21. function removeCaption (i, div, divs) {
  22. console.log("removeCaption: " + i);
  23. div.remove();
  24. if (divs) {
  25. debugger;
  26. }
  27. }
  28. function insertCaption (i) {
  29. var c = cues[i];
  30. var div = WebVTT.convertCueToDOMTree(window, c.text);
  31. var divs = WebVTT.processCues(window, [c], captions);
  32. setTimeout(removeCaption,
  33. (c.endTime - c.startTime) * 1000,
  34. i, div, divs);
  35. var nextI = i + 1;
  36. if (nextI < cues.length) {
  37. setTimeout(insertCaption,
  38. (cues[nextI].startTime - c.startTime) * 1000,
  39. nextI);
  40. }
  41. }
  42. if (cues.length) {
  43. var i = 0;
  44. setTimeout(insertCaption, cues[i].startTime * 1000, i);
  45. }
  46. });
  47. }
  48. function playbackChat (url) {
  49. var chatLog = document.querySelector(".chat-log");
  50. fetch(url)
  51. .then(function (r) { return r.json(); })
  52. .then(function (chatEvents) {
  53. function appendNextEvent (i) {
  54. var e = chatEvents[i];
  55. var chatEntry = document.createElement("div");
  56. chatEntry.textContent = e.time_text + " - " + e.author.name + ": " + e.message;
  57. chatLog.appendChild(chatEntry);
  58. chatLog.scrollIntoView(false, {behavior: "smooth"});
  59. var nextI = i + 1;
  60. if (nextI < chatEvents.length) {
  61. setTimeout(appendNextEvent,
  62. (chatEvents[nextI].time_in_seconds - e.time_in_seconds) * 1000,
  63. nextI);
  64. }
  65. }
  66. if(chatEvents.length) {
  67. var i = 0;
  68. setTimeout(appendNextEvent, chatEvents[i].time_in_seconds * 1000, i);
  69. }
  70. });
  71. }
  72. </script>
  73. </head>
  74. <body>
  75. <div class="chat-log">
  76. </div>
  77. <div class="captions" style="position: fixed; height: 100%; width: 100%; bottom: 0;">
  78. </div>
  79. <script>
  80. playbackChat("yt-zOwzHrfkkZo-live.json");
  81. playbackCaptions("Mainstream_is_now_a_fantasy_world-zOwzHrfkkZo.en.vtt");
  82. </script>
  83. </body>
  84. </html>