Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Youtube/NetFlix stream videos?

I am working on a project in which I am creating a video streaming web service. What I have created till now is a service that synchronously write video content into user stream. But, my web service doesn't work the same way Youtube/Netflix work.

I was just wondering how Youtube/Netflix stream videos. These websites don't directly send video content to users' browser. I was looking into networks tabs in developers options and saw that both of these websites make new requests to Web APIs with range header changed. Can anyone please tell me how this works exactly.

like image 335
Dinesh Verma Avatar asked Feb 07 '16 17:02

Dinesh Verma


Video Answer


1 Answers

In very high level terms, the client (browser, mobile app etc) requests the video from the server.

Because videos are large and users don't want to wait until the whole video has been downloaded to play it back, most clients are designed to start video playback as soon as there is enough of the video for the client to be able to decode and start playback.

Most clients and servers now support at the very least HTTP streaming:

  • client receives info about the video including the total file size (e.g. 75,000 bytes).
  • The client requests a subset of the video initially - e.g bytes 0 - 5000.
  • The client receives, decodes and starts playback for the initial bytes received.
  • As the playback progresses the client request the next section of the video - e.g. bytes 5001-10,000 and so on.

Adaptive Bit rate Streaming builds on this to cater for different network conditions:

  • The Server contains multiple copies of the same video, encoded in different bit rates to allow for different network speeds (higher bit rate is better quality as a general rule). The videos are all broken up into equal length (from a time point of view) chunks - e.g. 2 second chunks.
  • The client requests information about the video (fro example using the source URL in a web page) and receives an 'index' or 'manifest' file in response. This file contains pointers to all the video, audio, subtitle streams etc for this video
  • The client requests the first chunk of the video from one of the bit rate streams listed in the manifest. For example it might start with the lowest bit rate at the beginning to make sure the video starts quickly and in case there are network problems.
  • If the first chunk downloads successfully then the client will request a chunk form the next highest bit rate for the next 2 seconds of the video.
  • The client repeats this until it reaches the highest bit rate or it sees a delay downloading a chunk, implying that this bit rate is too high for current network conditions.
  • The client continues to monitor the connection throughout playback and step up or down the bit rates depending on the network conditions.

You can actually see the ABR effect in YouTube, Netflix etc: when you start a video you will often see the quality is not as good of the first 30seconds to a minute as it steps up the bit rates.

YouTube also has some nice stats which you can access by right clicking the video and checking out 'stats for nerds'.

like image 179
Mick Avatar answered Sep 28 '22 22:09

Mick