Skip to content

Take control over your live stream video by running it yourself. Streaming + chat out of the box.

888 B
Raw Blame
package storage

import (
	"bufio"
	"os"
	"path/filepath"

	"github.com/grafov/m3u8"
	log "github.com/sirupsen/logrus"

	"github.com/owncast/owncast/config"
)

// rewritePlaylistLocations will take a local playlist and rewrite it to have absolute URLs to a specified location.
func rewritePlaylistLocations(localFilePath, remoteServingEndpoint, hlsPrefix string) error {
	f, err := os.Open(localFilePath) // nolint
	if err != nil {
		log.Fatalln(err)
	}

	p := m3u8.NewMasterPlaylist()
	if err := p.DecodeFrom(bufio.NewReader(f), false); err != nil {
		log.Warnln(err)
	}

	if hlsPrefix == "" {
		hlsPrefix = "hls"
	}

	for _, item := range p.Variants {
		item.URI = remoteServingEndpoint + "/" + hlsPrefix + "/" + item.URI
	}

	publicPath := filepath.Join(config.HLSStoragePath, filepath.Base(localFilePath))

	newPlaylist := p.String()

	return writePlaylist(newPlaylist, publicPath)
}