How To Code A LIVE Streaming Server: Convert RTMP to HLS #2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to part two on how to code a live streaming server if you haven't seen part one then this probably won't make a lot of sense so make sure you go and watch part one i'll put a link in the description below uh so in part one we created a server that could take a stream from obs and deliver that stream over rtmp and we secured that stream by requiring the user put in the right stream key in obs in part two we will be converting that rtmp stream into hls so that you can watch the stream on a website or on a phone natively with that said if you have any trouble throughout this video uh make sure to stop by on the discord and either me or someone else can help you out and of course all the code will be available on github link in the description below let's get started okay so before we get started i want to show you exactly what you can expect to see at the end of this tutorial so let's get our code started and we'll do that like in our last video using docker compose up all right now that the code is started we now also have a website so we can watch our live stream and that website is on localhost 8080. okay so here's our live stream so i'm currently not streaming so once i start streaming the live stream should show up on our website so i'll just hit start streaming okay now the rtmp stream is going to nginx and then nginx is converting that to hls and then when it converts to hls it's stored in many different clips uh and is played as a live playlist on our website okay i just pulled it up so you can actually see the clips that were converted from rtmp in the data folder of live stream part two so here is the hls playlist test m3ue and then as this stream is going there are multiple clips being made here so now if we refresh the live stream here you can see it um and there it is and you can you can seek through the video go the beginning um so right now the live stream is capped i believe to you can go back around 15 minutes but you can change that um and we'll go into more details later in the video all right so we're gonna go over what exactly has changed since part one and we're gonna start with the nginx configuration so here is where we're gonna tell the the module to convert our rtmp stream to hls and how do we do this so first remember that a github page for this docker image if we go down to here and the documentation there's documentation on how to convert to hls so it's as simple as just adding these on and then you'll start converting your stream to hls so let's see how that's done so here this is just a regular old internetx configuration and so first we turn hls on so now it is converting the rtmp stream but where did these video clips go we tell it the path so we want to store it on slash temp slash hls on the image we want each fragment to be 10 seconds it's going to take 10 seconds of live video and then convert it and we want the playlist length to be 5 minutes long so what this means exactly is there are only a certain number of fragments we can create and so i set the limit to five minutes so i don't fill my computer with all this live video so after five minutes of fragments so about 60 fragments given each one is or 30 fragments given each one is um uh 10 seconds um uh it'll start deleting the older fragments so what this translates to is in the live stream [Music] uh you can only look back up to five minutes um and you can change this to whatever you want it could be an hour if you want or like what youtube does you can make it so it goes back 12 hours all right so next we're going to go over creating our web server so we need this web server so we can host a website uh that will show our stream and it's a website that can serve the video files that we're generating uh right here so first we'll create an http server and this will run alongside our rtmp server on the same nginx container it'll be on port 8080 so to go to our website we'll go to localhost 8080. so this is what's going to happen when you go to the root of the site so just localhost 8080 what's going to happen is it's going to look in this folder slash www this is where we're going to put our index.html file later on so that you'll see our home page and then we're going to define what's going to happen if you go to the slash hls endpoint so here we're going to serve two different kind of files first is our hls playlist file which is m3u8 what this types directive does is it tells you what type to assign to this file extension so when we're serving this file extension it'll give it this type and reserve this file extension it'll give this type so when you go to slash hls it'll look inside the slash temp folder and we're going to add the header no cache so the browser does not cache our live stream what this header basically does is tell the browser any website can receive these video files and if this wasn't here then none of the video files would work and your browser would reject all the requests okay next we're going to go over our docker compose file so here's what's changed so remember in the nginx file uh nginx configuration file we added a http server and it listened on port 8080 so we're going to have to expose that port from the docker container to our computer so on this rtmp service we add for 8080 from our computer and we map it to the or 8080 in the docker image now another piece is the hls videos will be stored on slash temp slash hls within the docker container and i want to take a look inside there or be able to see what videos are there for debugging purposes so i added volumes and then we mapped this data folder within the project to slash temp hls within the docker container you don't need this to have the video file stored there but this is just helpful so you can take a look at all these different video files that are made and you can debug all right so that wraps it up for part two i think for the next part what we're going to do is we're going to take our what we've got so far and host it on a actual server so it's behind like a website um so it's not just running on your local machine if you have more video ideas feel free to suggest them in discord there's been a ton of ideas added so far and i can't wait to make them into actual videos so that's it for now thanks for watching and i'll catch you in the next one peace
Info
Channel: AbdisalanCodes
Views: 10,353
Rating: undefined out of 5
Keywords:
Id: gDSbQvmEmLY
Channel Id: undefined
Length: 7min 51sec (471 seconds)
Published: Fri Feb 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.