2020-05-30 03:08:33 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-06-01 22:28:14 +02:00
|
|
|
"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-03 10:34:05 +02:00
|
|
|
if configuration.IPFS.Enabled || configuration.S3.Enabled {
|
2020-06-02 01:53:31 +02:00
|
|
|
outputDir = configuration.PrivateHLSPath
|
|
|
|
hlsPlaylistName = path.Join(outputDir, "temp.m3u8")
|
|
|
|
}
|
2020-06-01 22:28:14 +02:00
|
|
|
|
2020-06-02 01:53:31 +02:00
|
|
|
log.Printf("Starting transcoder saving to /%s.", outputDir)
|
2020-06-02 02:42:36 +02:00
|
|
|
pipePath := getTempPipePath()
|
2020-06-01 22:28:14 +02:00
|
|
|
|
2020-06-02 02:42:36 +02:00
|
|
|
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
|
2020-06-01 22:28:14 +02:00
|
|
|
|
|
|
|
_, 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)
|
2020-06-01 21:15:07 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|