How To Code A Video Stream using MongoDB

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi so this video was inspired by a comment on one of my last videos called how to code a video streaming server using node.js i got a comment from jyram and they said really nice one learned a lot is it possible to do the same but from mongodb yes it is so first i'm going to go over the architecture of how this would work and then go step by step through the code if you just want to skip to the code i put a timestamp in the description so you can click on that and skip right to it so let's get started here's what we're gonna make it's one video that's streaming from mongodb through node.js and to this browser you can see that the video is buffering so the user doesn't need to download the entire video to run it mongodb is a nosql database that lets you store json-like documents we can store the binary of our video into a document however there is a size limit of 16 megabytes in our video could be much larger than that to get around this there's a feature called grid fs that can store files greater than 16 megabytes by splitting them into several document chunks another useful feature of gridifs is that we can ask it for a file stream from the database this is super important because if we can't stream the file we would need to load the entire file into memory and that would be very bad when you stream you can use a very small fraction of ram and deliver the video much faster to your users now let's move on to the quoting portion first i'll show you how to upload your video to mongodb and then how to download it and stream it from your server to your client and of course all code is available on github so you can run this yourself and i've included a docker compose file so you don't need to install a bunch of packages to get this running you can just do docker compose up apologies for this upload method i'm about to show you this is purely for demo purposes and not a particularly great setup to upload videos but it should be able to showcase how easy it is to upload large files using mongodb and gridifs first we import mongodb and create our connection url with username and password next we make an init video endpoint that connects to mongodb and checks if there is an error using the client i connect to the videos database and with this db we create a grid fs bucket that we can upload our file to from there we create an upload stream and name it big buck and create a read stream from our big buck mp4 file which is stored locally and lastly we upload the video to our database by typing the read stream to the upload stream and we're done this should upload our file to mongodb and if we check inside our database you can see our file and it has been split into this many chunks for downloading we'll do the same and connect to our mongodb client creating a new client per endpoint may not be best practice but this is purely for demo purposes so bear with me next we check for range headers in our request this tells us where we should start the video from if you're curious and want more detail i do a deep dive in a previous video i'll make sure to link it in the description below next we connect to the videos database which has our file access the fs.files collection which is set by gridifs when we upload the video and make sure it returns a video this is just metadata for our video file and not the actual file we just need to find out how large the file is to create our headers i go into deep detail on why we need these headers in my previous video linked down below but in short we need to tell the client what part of the video we are sending in this case we're sending start they request to the very end of the video we write those headers the response and make sure to give a status of 206 because we are sending partial content lastly we get our bucket from gridifs create a download stream of our big book file starting from where the video was requested and finally using the download stream we pipe the video to the response and this streams it to the user's browser alright so that should wrap up the video if you liked it make sure to like comment and subscribe and i'll see you in the next one peace
Info
Channel: AbdisalanCodes
Views: 7,060
Rating: undefined out of 5
Keywords: video stream, nodejs, mongodb, vscode
Id: y6Z-SZt-Xvw
Channel Id: undefined
Length: 4min 49sec (289 seconds)
Published: Wed Nov 25 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.