Advantages and disadvantages of creating golang video streaming server based on HLS


HLS is the abbreviation of HTTP live streaming. It is an adaptive bit rate streaming media transmission protocol based on HTTP developed by apple. In 2009, HLS streaming media has become the most widely used real-time video protocol. It is a format that decomposes streams into file based segments, which can be downloaded through HTTP. HLS can be downloaded through standard HTTP or proxy server, which is different from UDP based protocols (such as RTP). Since HLS is so popular now, what are its advantages and disadvantages.


  • Widely used
  • First of all, as mentioned just now, HLS is the real-time video protocol with the most frequent applications. Although Apple was originally designed for its own ecology, such as IOS and safari browser, it has strong ecology and R & D capabilities backed by apple. Now it has been implemented on almost all browsers. Although mainstream browsers now support a similar standard, called MPEG dash, I personally think HLS is a better choice because Apple Safari browser and IOS devices do not support it.
  • Adaptive bit rate
  • Another great advantage of HLS is that it allows the client to select the appropriate quality stream from various quality streams according to the available bandwidth. HLS is decomposed into small file segments of about 10 seconds. Through decomposition, the client application only needs to buffer 10 seconds in advance. It saves users a lot of potential bandwidth.


  • Bad delay
  • Although HLS is designed to efficiently process multi-quality streams, it is not designed for fast video transmission. In fact, HLS introduces a very long delay into the stream, generally about 20 seconds or even longer.
  • At this point, you may want to ask why? HLS requires three clips in the queue to allow playback, and the clips are divided by key frames in the video. The only way to create an ultra-low latency stream with HLS is to encode the video with a key frame every 250 milliseconds. The HLS playlist window will be four item long, increasing the frequency of ongoing HTTP calls and adding additional pressure to the server.
  • Unpublished
  • HLS is a user only protocol. Unlike webrtc, which has a specification for publishing from the browser, HLS only supports playing streams. If you want to publish a device’s real-time video stream, you just need to find other SDKs, such as red5 pro (the scene is relatively simple and expensive), to create publishing applications using RTP, and then relay these streams through HLS for people to view in the browser.
  • There are several mature audio and video SDKs in China, such as sound network and other platforms, which provide audio and video solutions for many scenes.

After a brief introduction to HLS, we will demonstrate a small demo. Using ffmpeg, MP3 files can be easily converted to HLS format. It consists of multiple files, one of which contains metadata (. M3u8). Metadata tells the client where to obtain each data file and what content is contained in the data file. The extended name of the data file is. Ts, which usually contains 10 seconds of audio.

First, prepare an MP3 file. Then install ffmpeg and install ffmpeg on the Mac. If the speed is very slow, you can try to switch the image.

brew install ffmpeg

After successful installation, enter the folder where MP3 is located and execute the following instructions.

Ffmpeg - I Jiangnan. MP3 - C: a libmp3lame - B: a 128K - map 0:0 - f segment - segment_ time 10 -segment_ list outputlist.m3u8 -segment_ format mpegts output%03d.ts

After execution, you should see some results, which are roughly as follows

1 output000.ts  output008.ts  output016.ts  output024.ts
2 output001.ts  output009.ts  output017.ts  output025.ts
3 output002.ts  output010.ts  output018.ts  output026.ts
4 output003.ts  output011.ts  output019.ts  outputlist.m3u8
5 output004.ts   output012.ts   output020.ts   Jiangnan.mp3
6 output005.ts  output013.ts  output021.ts
7 output006.ts  output014.ts  output022.ts
8 output007.ts  output015.ts  output023.ts

At this step, you have completed the file format conversion, and then enter the coding stage.

Project structure

Advantages and disadvantages of creating golang video streaming server based on HLS

The code is as follows

package main
import ( 
func main() { 
 //Set file directory
 const songsDir = "song" 
 const port = 8888
 http.Handle("/", http.FileServer(http.Dir(songsDir))) 
 log.Printf("Serving %s on HTTP port: %v\n", songsDir, port)
 log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", port), nil))

Run code

go run main.go

Open browser address

Congratulations, you can hear this moving melody of MP3. However, if you want to further commercial development or personal development, it is recommended to use more mature solutions. First, audio and video are not separated. Audio demand is often accompanied by video demand. Audio and video are often a sub demand in the development process. If you spend a lot of time on it, the project progress will be delayed, And when dealing with multiple platforms, there are often holes in some details. Using the SDK on the market can save the development cost to a great extent. The voice network I personally use can support cross platform, which is the most critical. I can pay a certain amount of money every month, which is very friendly to individual developers. Of course, the cost is actually on the one hand. Another important point is that you can access it with a few lines of code, which greatly reduces the amount of stepping on the pit!


As you can see, HLS is widely used in various platforms, mobile terminals and browsers, making it a good choice for developers to distribute streams to users. However, everything happens, because HLS is a relatively slow protocol. Although Apple’s rules seem quite strict, they are actually flexible when it comes to IOS streaming media requirements. If you want to build some real-time communication programs through it, it may not be suitable for you. Finally, if you want to realize a real-time communication system, you’d better rely on other mature solutions on the market. It is more suitable for applications built under Apple’s ecosystem in scenes with less high real-time requirements.

This is the end of this article about creating a golang video streaming server based on HLS. For more information about golang video streaming server, please search the previous articles of developeppaper or continue to browse the relevant articles below. I hope you will support developeppaper in the future!