Skip to content

Instantly share code, notes, and snippets.

@bkeepers
Last active April 1, 2026 20:12
Show Gist options
  • Select an option

  • Save bkeepers/4979576 to your computer and use it in GitHub Desktop.

Select an option

Save bkeepers/4979576 to your computer and use it in GitHub Desktop.
Keep two HTML5 video elements in sync.
videos = document.getElementsByTagName('video')
new VideoSync(videos[0], videos[1])
# Keep two HTML5 video elements in sync.
#
# @primary - primary video with controls and audio
# @secondary - secondary video,
class @VideoSync
constructor: (@primary, @secondary) ->
@ready = false
@secondary.addEventListener 'canplay', => @ready = true
@primary.addEventListener 'play', => @secondary.play()
@primary.addEventListener 'pause', => @secondary.pause()
@primary.addEventListener 'timeupdate', @sync
@primary.addEventListener 'seeking', @sync
sync: =>
@secondary.currentTime = @primary.currentTime if @ready
@odinndagur
Copy link
Copy Markdown

I just used this basic idea but the secondary video lags like crazy if sync is called every single timeupdate. I just added a bit that checks if the videos are offset by some specific amount then it syncs them up (if Math.abs(primary.currentTime - secondary.currentTime) > 0.3 or whatever) and it works perfectly. Just thought I'd post it here in case someone else comes here looking for this method 🫡

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment