How to Create an HLS Stream Using Golang and FFmpeg

Mehran
ITNEXT
Published in
2 min readMar 22, 2023

--

Photo by Jacco Rienks on Unsplash

Introduction:

HTTP Live Streaming (HLS) is a widely-used streaming protocol developed by Apple for delivering video and audio content over the internet. HLS works by segmenting media files into smaller chunks and delivering them through a playlist file with the .m3u8 extension. This tutorial will show you how to create an HLS stream using Golang and FFmpeg, a powerful open-source multimedia processing tool.

Prerequisites:

  1. Basic understanding of Golang
  2. FFmpeg installed on your system

Installing FFmpeg on macOS:

To install FFmpeg on macOS, we recommend using Homebrew, a popular package manager. If you don’t have Homebrew installed, follow the instructions on the official website: https://brew.sh/

Once you have Homebrew installed, open the terminal and run the following command:

brew install ffmpeg

This command will download and install FFmpeg and its dependencies.

Creating an HLS Stream in Golang:

Now that you have FFmpeg installed on your system, we’ll create a simple Golang function to generate an HLS playlist and segment a video file using FFmpeg. Here’s the code:

package main

import (
"fmt"
"log"
"os/exec"
"strconv"
)

func createHLS(inputFile string, outputDir string, segmentDuration int) error {
// Create the output directory if it does not exist
if err := os.MkdirAll(outputDir, 0755); err != nil {
return fmt.Errorf("failed to create output directory: %v", err)
}

// Create the HLS playlist and segment the video using ffmpeg
ffmpegCmd := exec.Command(
"ffmpeg",
"-i", inputFile,
"-profile:v", "baseline", // baseline profile is compatible with most devices
"-level", "3.0",
"-start_number", "0", // start numbering segments from 0
"-hls_time", strconv.Itoa(segmentDuration), // duration of each segment in seconds
"-hls_list_size", "0", // keep all segments in the playlist
"-f", "hls",
fmt.Sprintf("%s/playlist.m3u8", outputDir),
)

output, err := ffmpegCmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to create HLS: %v\nOutput: %s", err, string(output))
}

return nil
}

func main() {
inputFile := "path/to/your/video.mp4"
outputDir := "path/to/output/directory"
segmentDuration := 10 // duration of each segment in seconds

if err := createHLS(inputFile, outputDir, segmentDuration); err != nil {
log.Fatalf("Error creating HLS: %v", err)
}

log.Println("HLS created successfully")
}

This function, createHLS, takes three arguments: the input video file, the output directory where the HLS segments and playlist will be stored, and the duration of each segment in seconds. It uses ffmpeg to create an HLS playlist and segment the video accordingly.

Conclusion:

This tutorial showed you how to install FFmpeg on macOS and create an HLS stream using Golang and FFmpeg. This example can be expanded to handle more complex scenarios, such as encoding video in different formats, adding multiple audio tracks, or including closed captions.

--

--

Tech Team Lead | Cloud, Video & Microservices Expert | Insights on streaming innovations & programming. #ContinuousLearning