owncast/ffmpeg.go

53 lines
1.3 KiB
Go
Raw Normal View History

2020-05-30 03:08:33 +02:00
package main
import (
"fmt"
"log"
2020-05-30 03:08:33 +02:00
"os"
"os/exec"
2020-06-02 01:53:31 +02:00
"path"
"strconv"
2020-05-30 03:08:33 +02:00
)
2020-06-02 01:53:31 +02:00
func startFfmpeg(configuration Config) {
var outputDir = configuration.PublicHLSPath
var hlsPlaylistName = path.Join(configuration.PublicHLSPath, "stream.m3u8")
2020-05-30 03:08:33 +02:00
2020-06-02 01:53:31 +02:00
if configuration.IPFS.Enabled {
outputDir = configuration.PrivateHLSPath
hlsPlaylistName = path.Join(outputDir, "temp.m3u8")
}
2020-06-02 01:53:31 +02:00
log.Printf("Starting transcoder saving to /%s.", outputDir)
pipePath := getTempPipePath()
ffmpegCmd := "cat " + pipePath + " | " + configuration.FFMpegPath +
2020-06-02 01:53:31 +02:00
" -hide_banner -i pipe: -vf scale=" + strconv.Itoa(configuration.VideoSettings.ResolutionWidth) + ":-2 -g 48 -keyint_min 48 -preset ultrafast -f hls -hls_list_size 30 -hls_time " +
strconv.Itoa(configuration.VideoSettings.ChunkLengthInSeconds) + " -strftime 1 -use_localtime 1 -hls_segment_filename '" +
outputDir + "/stream-%Y%m%d-%s.ts' -hls_flags delete_segments -segment_wrap 100 " + hlsPlaylistName
_, err := exec.Command("bash", "-c", ffmpegCmd).Output()
2020-05-30 03:08:33 +02:00
verifyError(err)
}
func verifyError(e error) {
if e != nil {
panic(e)
}
}
func writePlaylist(data string, filePath string) {
f, err := os.Create(filePath)
defer f.Close()
2020-05-30 03:08:33 +02:00
if err != nil {
fmt.Println(err)
return
}
_, err = f.WriteString(data)
if err != nil {
fmt.Println(err)
return
}
}