owncast/ffmpeg.go

113 lines
3.6 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"
2020-06-12 23:14:05 +02:00
"strconv"
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-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)
pipePath := getTempPipePath()
2020-06-09 10:52:15 +02:00
var videoMaps = make([]string, 0)
var streamMaps = make([]string, 0)
var audioMaps = make([]string, 0)
var videoMapsString = ""
var audioMapsString = ""
var streamMappingString = ""
if configuration.VideoSettings.EnablePassthrough || len(configuration.VideoSettings.StreamQualities) == 0 {
fmt.Println("Enabling passthrough video")
// videoMaps = append(videoMaps, fmt.Sprintf("-map 0:v -c:v copy"))
streamMaps = append(streamMaps, fmt.Sprintf("v:%d,a:%d", 0, 0))
} else {
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))
videoMapsString = strings.Join(videoMaps, " ")
audioMaps = append(audioMaps, "-map a:0")
audioMapsString = strings.Join(audioMaps, " ") + " -c:a copy" // Pass through audio for all the variants, don't reencode
}
2020-06-09 10:52:15 +02:00
}
streamMappingString = "-var_stream_map \"" + strings.Join(streamMaps, " ") + "\""
2020-06-09 10:52:15 +02:00
ffmpegFlags := []string{
"-hide_banner",
"-re",
2020-06-09 10:52:15 +02:00
"-i pipe:",
2020-06-12 02:34:20 +02:00
// "-vf scale=900:-2", // Re-enable in the future with a config to togging resizing?
// "-sws_flags fast_bilinear",
videoMapsString, // All the different video variants
audioMapsString,
2020-06-09 10:52:15 +02:00
"-master_pl_name stream.m3u8",
2020-06-12 02:34:20 +02:00
"-g 60", "-keyint_min 60", // create key frame (I-frame) every 48 frames (~2 seconds) - will later affect correct slicing of segments and alignment of renditions
2020-06-14 03:00:26 +02:00
// "-framerate 30",
// "-r 25",
2020-06-09 22:15:44 +02:00
"-preset " + configuration.VideoSettings.EncoderPreset,
2020-06-12 02:34:20 +02:00
"-sc_threshold 0", // don't create key frames on scene change - only according to -g
2020-06-14 03:00:26 +02:00
"-profile:v high", // Main for standard definition (SD) to 640×480, High for high definition (HD) to 1920×1080
"-movflags +faststart",
"-pix_fmt yuv420p",
"-maxrate 2048k -bufsize 2048k",
2020-06-09 10:52:15 +02:00
"-f hls",
"-hls_list_size " + strconv.Itoa(configuration.Files.MaxNumberInPlaylist),
2020-06-12 23:14:05 +02:00
"-hls_time " + strconv.Itoa(configuration.VideoSettings.ChunkLengthInSeconds),
2020-06-09 10:52:15 +02:00
"-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",
"-tune zerolatency",
streamMappingString,
2020-06-09 10:52:15 +02:00
variantPlaylistName,
}
ffmpegFlagsString := strings.Join(ffmpegFlags, " ")
ffmpegCmd := "cat " + pipePath + " | " + configuration.FFMpegPath + " " + ffmpegFlagsString
// fmt.Println(ffmpegCmd)
2020-06-10 03:28:07 +02:00
_, err := exec.Command("sh", "-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)
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
}
}