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"
|
2020-06-09 10:52:15 +02:00
|
|
|
"strings"
|
2020-05-30 03:08:33 +02:00
|
|
|
)
|
|
|
|
|
2020-06-02 01:53:31 +02:00
|
|
|
func startFfmpeg(configuration Config) {
|
|
|
|
var outputDir = configuration.PublicHLSPath
|
2020-06-09 10:52:15 +02:00
|
|
|
var variantPlaylistPath = configuration.PublicHLSPath
|
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
|
2020-06-09 10:52:15 +02:00
|
|
|
variantPlaylistPath = configuration.PrivateHLSPath
|
2020-06-02 01:53:31 +02:00
|
|
|
}
|
2020-06-01 22:28:14 +02:00
|
|
|
|
2020-06-09 10:52:15 +02:00
|
|
|
outputDir = path.Join(outputDir, "%v")
|
|
|
|
|
|
|
|
// var masterPlaylistName = path.Join(configuration.PublicHLSPath, "%v", "stream.m3u8")
|
|
|
|
var variantPlaylistName = path.Join(variantPlaylistPath, "%v", "stream.m3u8")
|
|
|
|
// var variantRootPath = configuration.PublicHLSPath
|
|
|
|
|
|
|
|
// variantRootPath = path.Join(variantRootPath, "%v")
|
|
|
|
// variantPlaylistName := path.Join("%v", "stream.m3u8")
|
|
|
|
|
|
|
|
log.Printf("Starting transcoder saving to /%s.", variantPlaylistName)
|
2020-06-02 02:42:36 +02:00
|
|
|
pipePath := getTempPipePath()
|
2020-06-01 22:28:14 +02:00
|
|
|
|
2020-06-09 10:52:15 +02:00
|
|
|
var videoMaps = make([]string, 0)
|
|
|
|
var streamMaps = make([]string, 0)
|
|
|
|
var audioMaps = make([]string, 0)
|
|
|
|
for index, quality := range configuration.VideoSettings.StreamQualities {
|
|
|
|
videoMaps = append(videoMaps, fmt.Sprintf("-map v:0 -c:v:%d libx264 -b:v:%d %s", index, index, quality.Bitrate))
|
|
|
|
streamMaps = append(streamMaps, fmt.Sprintf("v:%d,a:%d", index, index))
|
|
|
|
audioMaps = append(audioMaps, "-map a:0")
|
|
|
|
}
|
|
|
|
|
|
|
|
ffmpegFlags := []string{
|
|
|
|
"-hide_banner",
|
|
|
|
"-i pipe:",
|
|
|
|
strings.Join(videoMaps, " "), // All the different video variants
|
2020-06-09 11:47:02 +02:00
|
|
|
strings.Join(audioMaps, " ") + " -c:a copy", // Audio for all the variants
|
|
|
|
// strings.Join(audioMaps, " ") + " -c:a aac -b:a 192k -ac 2", // Audio for all the variants
|
2020-06-09 10:52:15 +02:00
|
|
|
"-master_pl_name stream.m3u8",
|
|
|
|
"-g 48",
|
|
|
|
"-keyint_min 48",
|
2020-06-09 11:56:23 +02:00
|
|
|
"-preset superfast",
|
2020-06-09 10:52:15 +02:00
|
|
|
"-sc_threshold 0",
|
|
|
|
"-profile:v high",
|
|
|
|
"-f hls",
|
|
|
|
"-hls_list_size 30",
|
|
|
|
"-hls_time 10",
|
|
|
|
"-strftime 1",
|
|
|
|
"-use_localtime 1",
|
|
|
|
"-hls_playlist_type event",
|
|
|
|
"-hls_segment_filename " + path.Join(outputDir, "stream-%Y%m%d-%s.ts"),
|
|
|
|
"-hls_flags delete_segments+program_date_time+temp_file",
|
|
|
|
"-segment_wrap 100",
|
2020-06-09 11:56:23 +02:00
|
|
|
"-tune zerolatency",
|
|
|
|
|
2020-06-09 11:47:02 +02:00
|
|
|
// "-master_m3u8_publish_rate 5",
|
2020-06-09 10:52:15 +02:00
|
|
|
"-var_stream_map \"" + strings.Join(streamMaps, " ") + "\"",
|
|
|
|
variantPlaylistName,
|
|
|
|
}
|
|
|
|
|
|
|
|
ffmpegFlagsString := strings.Join(ffmpegFlags, " ")
|
|
|
|
|
|
|
|
ffmpegCmd := "cat " + pipePath + " | " + configuration.FFMpegPath + " " + ffmpegFlagsString
|
|
|
|
|
|
|
|
// fmt.Println(ffmpegCmd)
|
2020-06-01 22:28:14 +02:00
|
|
|
|
|
|
|
_, err := exec.Command("bash", "-c", ffmpegCmd).Output()
|
2020-06-09 10:52:15 +02:00
|
|
|
fmt.Println(err)
|
2020-05-30 03:08:33 +02:00
|
|
|
verifyError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|