If you are running an eLearning site or a site where majority content is provided in the form of videos. The biggest question you face is where to host those videos.
Back in 2018 we created a site Videoencrypt and used the same technology powered by a server to encrypt and deliver videos to thousands of clients. So here I am, spilling the beans.
The Problem
If you host the videos on Youtube, Vimeo or any other public platform then you are unable to create a brand for your site. Although Youtube is really good option it does not provide any way to make your videos private, if you make them private you can only share with 50 Google accounts which is a serious limitation for any site looking to run a video based business.
While some video platforms like Vimeo & Wistia do provide private videos for a small fee usually they charge too much which is mostly on basis of video size and bandwidth. Bandwidth in case of Videos is consumed really, really fast which makes them a non-viable option for some projects.
Another clever way is to use services where the videos are encoded in run time like Amazon elastic transcode or Bunny.net , while they do provide a hassle free option of transcoding videos on the fly, they turn out to be an expensive option as they charge on bandwidth or minutes of videos watched. Honestly, think about it that a user watching a video and repeating it costs you money. That is really not suitable if you want to grow your business in future as the costs will go exponential.
Why not host videos Locally ?
When you plan to host Videos locally, the main concerns when you are self hosting videos locally are :
- Load on server. Video files typically runs into GB’s, the user needs to be connected with 1 file for so long and very frequently downloads break. Since files are constantly loading and using server’s RAM there is a very likely chance that server would stop responding to other processes.
- Un-optimised Delivery: You’ve recorded a high quality 4k video for your consumers, but when a user is loading the video from a mobile phone, a video of 720p might have been enough for a great display.
The Solution : Convert & Host Locally, YES !
What if I say:
How about, you break your Huge 2gb video into smaller fragments of 2mb each and load each fragment as required by the user on demand.
How about, you create multiple version of your videos, like a 4k video for desktop users, a 720p video for mobile users and dynamically load the correct video for the user on Demand.
How about you only need to do this once and store the converted videos in your server or WordPress !
Great news, this has been there since 2009 and all the big companies ( vimeo , wistia ) use it to deliver video content online.
No Transcoding, No Excessive Bandwidth consumption, No server Load !
Convert the Video in HLS ! it generates a video format m3u8 which is like a playlist of smaller videos.
So your 2gb video is broken down in 2mb fragments and combined in a
Converting an MP4 video into HLS (HTTP Live Streaming) format is a great way to optimize video playback for different devices, such as mobile, tablet, and desktop. HLS is a streaming protocol that breaks videos into small chunks and serves them to viewers over HTTP. This allows viewers to watch videos on devices with varying internet speeds and bandwidth. By converting an MP4 video into HLS format, you can create different versions of the video with different resolution and bit rates that are optimized for different devices. In this article, we will explain the steps of converting an MP4 video into HLS format using the FFmpeg script.
Step 1: Download and install FFmpeg.
FFmpeg is a command-line tool that is widely used for video and audio conversions. It can be downloaded for free from the official website (https://ffmpeg.org/download.html). Once downloaded, install FFmpeg on your computer.
Step 2: Open the command prompt and navigate to the folder where the MP4 video is located.
Open the command prompt on your computer and navigate to the folder where the MP4 video is located. To navigate to the folder, you can use the “cd” command. For example, if the MP4 video is located in the “Videos” folder on the C drive, you would type “cd C:\Videos” and press enter.
Step 3: Create a new folder to store the HLS files.
In the same folder where the MP4 video is located, create a new folder to store the HLS files. You can use the “mkdir” command to create a new folder. For example, you can type “mkdir hls” and press enter to create a new folder named “hls”.
Step 4: Run the FFmpeg command to convert the MP4 video into HLS format.
To convert the MP4 video into HLS format, you will need to run the FFmpeg command. The basic command for converting an MP4 video into HLS format is:
ffmpeg -i input.mp4 -hls_time 10 -hls_list_size 0 -f hls output.m3u8
In this command, “input.mp4” is the name of the MP4 video that you want to convert, “hls_time” is the duration of each segment in seconds, “hls_list_size” is the number of segments in the playlist, and “output.m3u8” is the name of the HLS playlist file.
Create different versions of the video with different resolutions and bit rates.
To create different versions of the video with different resolutions and bit rates, you will need to run the FFmpeg command multiple times with different parameters. For example, to create a version of the video optimized for mobile devices, you can run the FFmpeg command with the following parameters:
ffmpeg -i input.mp4 -s 640x360 -b:v 350k -hls_time 10 -hls_list_size 0 -f hls mobile.m3u8
This command will create a version of the video with a resolution of 640×360 and a bit rate of 350kbps.
Similarly, you can create a version of the video optimized for tablet devices with the following parameters:
ffmpeg -i input.mp4 -s 1024x576 -b:v 800k -hls_time 10 -hls_list_size 0 -f hls tablet.m3u8
Now lets sum up in 1 single line of FFmpeg , that is to create multiple variations of the video and combine everything in 1 m3u8 file which we can upload on our server and run the Video.
Here is an FFmpeg command to convert an input.mp4 file into 3 streams: one optimized for desktop, one for tablet, and one for mobile devices, with a maximum chunk size of 2 MB:
ffmpeg -i input.mp4 -c:v libx264 -b:v 1000k -s:v 1280x720 -aspect 16:9 -c:a aac -b:a 128k -f hls -hls_time 10 -hls_playlist_type vod -hls_segment_filename desktop/output_%03d.ts desktop/output.m3u8 ffmpeg -i input.mp4 -c:v libx264 -b:v 500k -s:v 768x1024 -aspect 4:3 -c:a aac -b:a 128k -f hls -hls_time 10 -hls_playlist_type vod -hls_segment_filename tablet/output_%03d.ts tablet/output.m3u8 ffmpeg -i input.mp4 -c:v libx264 -b:v 300k -s:v 360x640 -aspect 9:16 -c:a aac -b:a 128k -f hls -hls_time 10 -hls_playlist_type vod -hls_segment_filename mobile/output_%03d.ts mobile/output.m3u8 In the above command: -i input.mp4 specifies the input file. -c:v libx264 specifies the video codec as H.264. -b:v 1000k, -b:v 500k, and -b:v 300k specify the video bitrates for desktop, tablet, and mobile, respectively. -s:v 1280x720, -s:v 768x1024, and -s:v 360x640 specify the video resolutions for desktop, tablet, and mobile, respectively. -aspect 16:9, -aspect 4:3, and -aspect 9:16 specify the aspect ratios for desktop, tablet, and mobile, respectively. -c:a aac specifies the audio codec as AAC. -b:a 128k specifies the audio bitrate. -f hls specifies the output format as HLS (HTTP Live Streaming). -hls_time 10 specifies the segment length to be 10 seconds. -hls_playlist_type vod specifies the playlist type as video on demand. -hls_segment_filename specifies the segment filenames, with the %03d placeholder for the segment number. The last argument for each FFmpeg command specifies the output file, which is a playlist file in this case (.m3u8). With this command, the output.mp4 file will be divided into 2 MB chunks, each with a resolution and bitrate optimized for either desktop, tablet, or mobile devices.
Note that the video player you are using must also support HLS encrypted files. One of our projects we use a customised player which supports HLS. See video :