2020-05-30 04:08:33 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-06-01 23:28:14 +03:00
|
|
|
"log"
|
2020-05-30 04:08:33 +03:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2020-06-02 02:53:31 +03:00
|
|
|
"path"
|
|
|
|
"strconv"
|
2020-05-30 04:08:33 +03:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-06-02 02:53:31 +03:00
|
|
|
func startFfmpeg(configuration Config) {
|
|
|
|
var outputDir = configuration.PublicHLSPath
|
|
|
|
var hlsPlaylistName = path.Join(configuration.PublicHLSPath, "stream.m3u8")
|
2020-05-30 04:08:33 +03:00
|
|
|
|
2020-06-02 02:53:31 +03:00
|
|
|
if configuration.IPFS.Enabled {
|
|
|
|
outputDir = configuration.PrivateHLSPath
|
|
|
|
hlsPlaylistName = path.Join(outputDir, "temp.m3u8")
|
|
|
|
}
|
2020-06-01 23:28:14 +03:00
|
|
|
|
2020-06-02 02:53:31 +03:00
|
|
|
log.Printf("Starting transcoder saving to /%s.", outputDir)
|
2020-06-01 23:28:14 +03:00
|
|
|
|
2020-06-02 02:53:31 +03:00
|
|
|
ffmpegCmd := "cat streampipe.flv | " + configuration.FFMpegPath +
|
|
|
|
" -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 23:28:14 +03:00
|
|
|
|
|
|
|
_, err := exec.Command("bash", "-c", ffmpegCmd).Output()
|
2020-05-30 04:08:33 +03:00
|
|
|
verifyError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifyError(e error) {
|
|
|
|
if e != nil {
|
|
|
|
panic(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 02:53:31 +03:00
|
|
|
func generateRemotePlaylist(playlist string, gateway string, segments map[string]string) string {
|
2020-05-30 04:08:33 +03:00
|
|
|
for local, remote := range segments {
|
2020-06-02 02:53:31 +03:00
|
|
|
playlist = strings.ReplaceAll(playlist, local, gateway+remote)
|
2020-05-30 04:08:33 +03:00
|
|
|
}
|
|
|
|
return playlist
|
|
|
|
}
|
|
|
|
|
|
|
|
func writePlaylist(data string, filePath string) {
|
|
|
|
f, err := os.Create(filePath)
|
2020-06-01 22:15:07 +03:00
|
|
|
defer f.Close()
|
|
|
|
|
2020-05-30 04:08:33 +03:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = f.WriteString(data)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|