Remember CSS Sprites? Let's do that with video!

Rate this content
Bookmark

Synchronizing multiple streams, particularly multiple live streams, and especially when the audio is critical too, can be incredibly difficult. How, then, could you build an experience where you have a few camera angles and you want a viewer to be able to seamlessly pick one? We'll walk through a hack technique that allows you to do just that without ever having to worry about synchronization.

This talk has been presented at JSNation Live 2021, check out the latest edition of this JavaScript Conference.

FAQ

Video sprites are similar to CSS sprites used in web development, where multiple images are combined into one single image to optimize loading times and performance. In the context of video, this technique combines multiple video feeds into a single video file. Viewers can then select which part of the combined video they wish to view, allowing them to choose between different feeds, such as different camera angles at a live event.

The main advantage of using video sprites in live streaming is feed synchronization. By combining all camera feeds into one encoder box and then broadcasting them as a single stream, it ensures that all feeds are perfectly synchronized. This eliminates the issues with audio sync and stream delays that can occur when switching between multiple independent feeds.

Viewers can select different feeds by choosing the quadrant of the video they want to watch. This is achieved by the broadcaster sending a single video that includes all feeds laid out in different quadrants. Viewers then pick the quadrant that displays the feed they are interested in, ensuring they only see their preferred video feed.

Video sprites can be particularly useful in scenarios like live sports events or concerts where multiple camera angles are available. For example, viewers can choose to continuously watch a specific performer, a particular area of the stadium, or different angles of a play without experiencing any delay or synchronization issues.

For a deeper dive into the technical implementation and examples of video sprites, you can visit the GitHub repository at github.com/muxlabs/video-sprites. Additionally, a practical demonstration is available at video-sprites.mux.dev, where you can see video sprites in action.

When broadcasting video sprites, it's crucial to consider the resolution limitations of your streaming service. If your service is limited to 1080p, then each quadrant in a video sprite will be a quarter of that resolution. As technology advances and more services support 4K streaming, each quadrant could potentially display at 1080p, offering higher quality for each feed.

Matt McClure is a co-founder of Mux, a company that provides video infrastructure for developers. He runs the Developer Experience organization at Mux, focusing on building and enhancing video APIs and other developer tools.

Matt McClure
Matt McClure
8 min
10 Jun, 2021

Comments

Sign in or register to post your comment.
Video Summary and Transcription
Today's Talk discusses the use of video sprites, which are similar to CSS sprites and are commonly used for optimization. Video sprites can be used to synchronize and select multiple video feeds, allowing viewers to choose the desired feed. Smooth streaming and broadcasting limitations are also mentioned, highlighting the need to consider resolution constraints. Overall, the Talk provides insights into the benefits and techniques of using video sprites for feed synchronization and optimization.

1. Introduction to Video Sprites

Short description:

Today we'll talk about using video sprites, similar to CSS sprites. A sprite is an image with multiple images in it, allowing the client to choose which parts to show. This technique is commonly used for optimization. For example, video game sprites combine all states of a character into one image. The same technique can be applied to videos, where multiple videos are combined into one and the desired one is shown. This technique is useful for feed synchronization, such as streaming concerts with different camera angles. Viewers can choose which feed to watch.

Hi everyone, today I want to talk about something a little hacky I've been thinking about lately, but the idea here is that we want to use video sprites in the same way that a lot of you might have used CSS sprites back in the day.

So we'll talk about what what I mean by a lot of this, but hello, I'm Matt McClure. I am one of the co-founders of a company called Mux and I run the Developer Experience org there. And in a nutshell, we build awesome video infrastructure for developers. So if you're looking for a video API, we're there so check us out.

So okay, taking a step back, what is a sprite? So in a nutshell, this is an image with a bunch of images in it. So the client gets this big combined image and then can just pick and choose which parts of it to show. So if you're relatively new, then you might not have seen this as widely used, but this is a really common optimization technique from the early aughts.

So if you had a button with hover states and depress states and active states, then you would just send one button image and then use your CSS background to decide which one of that image to show. So a little bit more common if you're kind of started there. You might remember this from back then.

But to show this a little bit more concretely, a lot of people know about this from video game sprites. So all of Mario's states are in one image. And then the client viewport then just shows the state of Mario it wants. So you might be wondering what in the world does this have to do with video? The gist here is the same technique works. You can send a few videos combined into one and then just show the one you care about in the player.

So you might be wondering, like, why in the world would we be doing this? And I would say, you know, use your imagination. There could be a bunch of examples of this, like, I think, sports, sporting events, concerts. But the biggest example that comes to mind and what we see the most from customers want to do stuff like this is around feed synchronization, particularly around, like, being able to pick between these different feeds in a synchronized way. So let's say you're streaming live concerts, live music. So you've got a bunch of different cameras streaming a bunch of different angles. So one's on the drummer, one's on the singer, one's on the audience. And a producer on site is typically deciding which one of those feeds to show at any given time.

So they might do a nice little transition, go from the drummer to the audience, et cetera. That producer then sends a feed to an encoder service or whatever else. That looks like I'm using Mux as an example here for obvious reasons. But then that service then broadcasts that to all of your end viewers. So then those viewers start saying, like, actually, I just want to watch the drummer all the time, and I hate the transitions that this producer is doing. So they want the power to be able to pick which feed they watch. So you decide, okay, how can we go about building this out.

2. Synchronizing and Selecting Video Feeds

Short description:

To synchronize multiple live video feeds, combine them into one video and allow viewers to select the desired feed. This can be achieved by sending all camera feeds to a local encoder box, which lays them out in quadrants and sends them to the encoder broadcaster service. Viewers can then choose which feed to watch, ensuring synchronized feeds and a shared audio stream. The process involves grabbing the coordinates of the desired feed, laying out the coordinates, and updating the canvas with the selected quadrant of the video. For more details and code examples, visit video-sprites.mux.dev and the GitHub repository at github.com/muxlabs/video-sprites.

So you start thinking, okay, I'll send every camera directly to that encoder or broadcast service. And then every viewer can get all the feeds. In this example, three feeds. And this is where things really get hairy if you start going down this path. So now you've got three different live streams that people can watch. But how do you switch between them? So do people just click another feed? And then you might be a few seconds off in terms of audio for all of them. So it can be tough to synchronize that in the client, or honestly, next to impossible to do that well.

So one solution would just be to send one video again. So like you were doing before, but instead of that video being produced, you just combine all the feeds at that level and then send them along. So in this example, all the cameras go into that one encoder box locally. It just lays them out in four quadrants, sends those to the encoder broadcaster service, and that goes out to all the viewers. And then from there, the viewers can then pick which one they want. So now you're guaranteed that your feeds are synced. You only have to worry about one audio stream that's shared between all of them for obvious reasons, and then you only show the quadrant of the video that the viewer selects at any given time.

So how might this work detail-wise? So all this code is on GitHub. I would suggest just checking it out there, but at a high level, you want to grab the coordinates of the feed that you want to show. So I have the feeds just named like zero through three. And you want to lay those coordinates out. Coordinates in this example of this array is the source X, source Y position, source width and source height, so what you want to chop. So zero, zero, top left. Zero, half the video width is the top right quadrant, and so on and so forth. And since these are quadrants, you just each one is half the video height and half the video width. And then you use these when you're updating the canvas. So when you draw that image to the canvas, you draw the video image or you pass the video image in, and then you pass these coordinates that we just grabbed, which will then say like, okay, only draw the top right quadrant of the video into canvas. And then, you know, call request animation frame over and over and over again.

So if you want to go have a play, it's just video-sprites.mux.dev. And then the source is all on github.com slash mux labs slash video sprites. But real quick, I wanted to just show you what this looks like. This is just video-sprites.mux.dev. I just have a bot asset in here, but this could also work with live, whatever else.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Video Editing in the Browser
React Summit 2023React Summit 2023
23 min
Video Editing in the Browser
Top Content
Watch video: Video Editing in the Browser
This Talk discusses the challenges of video editing in the browser and the limitations of existing tools. It explores image compression techniques, including Fourier transform and Huffman encoding, to reduce file sizes. The video codec and frame decoding process are explained, highlighting the importance of keyframes and delta frames. The performance bottleneck is identified as the codec, and the need for specialized hardware for efficient video editing is emphasized. The Talk concludes with a call to create a simplified API for video editing in the browser and the potential for AI-powered video editing.
Pushing the Limits of Video Encoding in Browsers With WebCodecs
JSNation 2023JSNation 2023
25 min
Pushing the Limits of Video Encoding in Browsers With WebCodecs
Top Content
Watch video: Pushing the Limits of Video Encoding in Browsers With WebCodecs
This Talk explores the challenges and solutions in video encoding with web codecs. It discusses drawing and recording video on the web, capturing and encoding video frames, and introduces the WebCodecs API. The Talk also covers configuring the video encoder, understanding codecs and containers, and the video encoding process with muxing using ffmpeg. The speaker shares their experience in building a video editing tool on the browser and showcases Slantit, a tool for making product videos.
Going Live from a Browser...with Another Browser
JSNation Live 2020JSNation Live 2020
8 min
Going Live from a Browser...with Another Browser
This Talk discusses live chat and live broadcast using WebRTC and RTMP. It explores running WebRTC on a server via Chrome and alternative approaches like using GetUserMedia and the Chrome.tabCapture API. The use of a whole Chrome instance for WebRTC and RTMP broadcast is also discussed, highlighting the pros and cons of this approach. The Talk recommends checking out Nick's talk from All Things RTC for more information.
Creating Videos Programmatically in React
React Summit Remote Edition 2021React Summit Remote Edition 2021
34 min
Creating Videos Programmatically in React
The Talk discusses the use of ReMotion, a library that allows for declarative video creation in React. It covers the process of creating videos, animating elements, and rendering multiple compositions. The Talk also mentions the features of ReMotion, such as audio support and server-side rendering. ReMotion 2.0 introduces audio support and the possibility of live streaming. The Talk concludes by highlighting the frustration with existing video editing tools and the integration of existing videos into ReMotion projects.
Getting Weird with Video Manipulation and HTML5 Canvas
React Summit 2020React Summit 2020
16 min
Getting Weird with Video Manipulation and HTML5 Canvas
Today's Talk at React Summit focused on the Canvas and HTML5 video APIs, showcasing the capabilities and possibilities they offer for video manipulation and interactivity. The speaker demonstrated how to use the HTML5 video element and canvas to manipulate and draw images, apply filters, and add real-time text overlays. They also showcased real-time object detection on video frames using machine learning. The Talk concluded with an example of enhancing a marketing website with interactive video using the canvas element. Overall, the Talk highlighted the power and potential of these APIs for video development.
Going Live from your Browser without WebRTC
React Summit Remote Edition 2020React Summit Remote Edition 2020
13 min
Going Live from your Browser without WebRTC
Mux provides an API for live streaming and aims to keep users in their own applications. Live broadcast and live chat are different, with live chat using WebRTC and live broadcast using RTMP and HLS. WebRTC can be implemented using headless Chrome or the getUserMedia process. Mux targets developers building platforms and suggests using semantic HTML. Ionic supports native apps and custom native views.

Workshops on related topic

Build Your Own Live Streaming Platform
React Summit Remote Edition 2021React Summit Remote Edition 2021
138 min
Build Your Own Live Streaming Platform
Workshop
Dylan Jhaveri
Dylan Jhaveri
In this workshop we will go over the basics of streaming video over the internet including technologies like HLS and WebRTC. We will then build our own React app to play live streams and recordings of live streams.