Synchronizing multiple HTML5 videos
Asked Answered
S

1

7

I'm hoping to have 3 videos playing in sync, only one visible at a time with the option to toggle between them. All must be in sync with each other and an audio track (which can just be the audio track of one of the video files if that is easier).

I have only read very pessimistic things about keeping a steady and synchronized frame rate across numerous media files is near impossible. The FPS supposedly shifts slightly and is uncontrollable.(?)

Since I only need one visible at a time I am hoping that will be in my favor but I am wondering if there are any goto procedures for something of the sort that anyone can point out to me. Not looking for anyone to write any code for me, just curious if my research has been misleading me.

✌ & ♡

Sika answered 2/4, 2016 at 15:9 Comment(4)
Maybe this might help you? popcornjs.org Example usage of popcorn: rebelliouspixels.com/semanticremixLymphadenitis
is there a certain use of popcornjs that will help keep videos in sync? I've been reading the docs but haven't found anything that will help that isn't just as easy to do in regular JSSika
Why would you need to keep them all playing? You could get popcornjs.org/popcorn-docs/media-methods/#currentTime the time of video and start another video at the same time if only one needs to be visible. And a seperate audio track on the background. and/or you can control Framerate with this. popcornjs.org/popcorn-docs/media-properties/#framerateLymphadenitis
The selected answer does work, but the browser will download the source file once for each video element, even though they are pointing to the same source file. https://mcmap.net/q/830379/-how-can-i-show-only-a-section-of-a-video-frame-with-css-or-javascript/1934286 Maybe that's changed in three years, however.Ahn
J
6

There is unfortunately no sync mechanism in the browser (i.e. time-code/SMTPE or similar sinc) to allow perfect synchronization with different video sources.

Forcing currentTime updates can backfire due to the lag.

To properly sync the video parts all video must come from the same video source (the file in this case). The video can be pre-edited so that all the parts you want to show are edited together sharing the video image surface.

Then simply use canvas (or clipping with CSS) to show different parts of the video in different elements in the page.

Update: proof-of-concept demos removed for now, sorry. Content of answer still valid though.

Use a canvas element to draw in the "current" part using drawImage() clipping functionality. There is a known issue in OSX using video as image source for drawImage(); you could pre-load an image sequence for these cases.

You could set up a map for each part - in this example I will use HD720 as total size:

var map = [
        {x: 0, y:0},
        {x: 640, y:0},
        {x: 0, y:360}
    ];

Now you can use buttons/selector/radios to change index of which video to show. The width and height is known:

<canvas width=640 height=360></canvas>

And in JavaScript:

var pos = map[index]; // source rect:         Dest rect:
ctx.drawImage(video, pos.x, pos.y, 640, 360,  0, 0, 640, 360);

All you need to do now is to put the drawing into a loop:

function render() {
    var pos = map[index];
    ctx.drawImage(video, pos.x, pos.y, 640, 360, 0, 0, 640, 360);
    requestAnimationFrame(render)
}

If you want to render all videos to the screen at the same time, but to different locations, you can instead just call drawImage() three times using the same map, but with different canvases:

<div>
    <canvas width="640" height="360"></canvas>
</div>

<div style="margin-left:400px">
    <canvas width="640" height="360"></canvas>
</div>

<div>
    <canvas width="640" height="360"></canvas>
</div>

And in JavaScript:

function render() {
    for(var i = 0, pos; i < ctx.length; i++) {
        pos = map[i];
        ctx[i].drawImage(video, pos.x, pos.y, 640, 360, 0, 0, 640, 360);
    }
    requestAnimationFrame(render)
}

Tip: the canvas can be of a different size than the video part. Just update the last width and height (dest. rect) to fill the canvas.

Tip 2: you can using this technique also superimpose text or logos etc.

Jenkins answered 3/4, 2016 at 6:13 Comment(4)
Wow thanks for this, I am waiting on full res video files to test this with, but the demo is great I just hope the single video file combining three large HD isn't too muchSika
K3N, as another user already mentioned (as an answer) the demos mentioned in your answer no longer work.Christa
@g00glen00b demo links removed for now. I'll see if I get the time to upload them later. The content of the answer still remain valid though (there is btw no notifications when another answer is added, needs to addressed to user in comments as you did).Jenkins
This does work, but the browser will download the source file once for each video element, even though they are pointing to the same source file. https://mcmap.net/q/830379/-how-can-i-show-only-a-section-of-a-video-frame-with-css-or-javascript/1934286 Maybe that's changed in three years, however.Ahn

© 2022 - 2024 — McMap. All rights reserved.