title: How to create a frame-accurate video sequencer with HTML5 <video>?
body: <p>I am attempting to build a video sequencer that is capable of playing back a list of video URLs seamlessly in series.  There cannot be a gap between the videos; the playback needs to be as close to frame-accurate as possible.</p>
  
  <p>Attempt #1
  I used a fairly obvious and straightforward approach, similar to that mentioned in this thread.
  <a href="https://stackoverflow.com/questions/2551859/html-5-video-or-audio-playlist">HTML 5 video or audio playlist</a></p>
  
  <p>The issue with this approach was that each time one video ended and the subsequent video was specified as the new source, that new video still needed to load, resulting in a potentially long gap.  If it is possible to force all the videos to preload even before they are named in video.src, this approach could still potentially work.</p>
  
  <p>Attempt #2
  I rewrote the script so each video in the list would result in a separate video element being created, but not attached to the document (using createElement).  Then as each video ended, I removed the previous  node and appended the next  element.</p>
  
  <p>Code executed on page load:</p>
  
  <pre>
  for (i in timelinePlay)
      if (timelinePlay[i] != null)        
      {
          var element = document.createElement('video');
          element.id = 'video1-' + (i);
          element.src = timelinePlay[i]['URL'];
          element.preload = 'load';
          video1Array[i] = element;
      }
  </pre>
  
  <p>Code for video 'ended' event:</p>
  
  <pre>
  videoElement.addEventListener("ended", function (event) {
      if (this.currentTime > 0)
      {
          if (player.hasChildNodes())
              while (player.childNodes.length > 0)
                  player.removeChild(player.firstChild);
  
          player = document.getElementById('canvas-player');
          player.appendChild(video1Array[timelineCurrent]);
  
          nextVideo = document.getElementById('video1-' + timelineCurrent);
  
          nextVideo.play();
  
          timelineCurrent++;
      }
  }, false);
  </pre>
  
  <p>(Note that these code examples are partial and somewhat simplified.  Also, I do realize that my use of Objects as Associative Arrays is not best practice)</p>
  
  <p>The result of this modification was MUCH closer, because the videos were now loaded by the time they were required to play, but there was still a short and inconsistent gap between the videos.</p>
  
  <p>Attempt #3
  I replaced the 'ended' event listener with a 'timeupdate' listener, beginning as follows:</p>
  
  <pre>
  nextVideo.addEventListener("timeupdate", function (event)
  {
      if (this.currentTime > (this.duration - 0.1)  && this.currentTime > 1)
      {
          this.removeEventListener("timeupdate", function () { return; }, false);
  
  </pre>
  
  <p>('this.currentTime > 1' is simply to ensure that the previous video actually plays)</p>
  
  <p>My hope was that the gap between videos was close enough to being consistent that starting the next video an arbitrary 0.1 seconds before the previous video ended would correct the gap to an acceptable extent.  The result was that the timing was indeed better, but it was skipping videos.  I attribute the skipped videos to misfiring of the 'timeupdate' event, but I could be mistaken.</p>
  
  <p>Other alternative options I had also considered:
  SMIL Script (seems to be obsolete, and would likely have the same syncing issues anyway)
  ffmpeg on the backend to concatenate the videos (my host will not allow shell scripts)</p>
  
  <p>FYI I am developing for Safari 5.0.3 at the moment</p>