0
owncast/core/ffmpeg/ffmpeg.go

201 lines
7.2 KiB
Go
Raw Normal View History

package ffmpeg
2020-05-29 18:08:33 -07:00
import (
"fmt"
2020-06-14 21:17:01 -07:00
"math"
2020-05-29 18:08:33 -07:00
"os"
"os/exec"
2020-06-01 16:53:31 -07:00
"path"
2020-06-12 14:14:05 -07:00
"strconv"
2020-06-09 01:52:15 -07:00
"strings"
2020-06-17 16:29:47 -07:00
log "github.com/sirupsen/logrus"
"github.com/gabek/owncast/config"
"github.com/gabek/owncast/utils"
2020-05-29 18:08:33 -07:00
)
//ShowStreamOfflineState generates and shows the stream's offline state
func ShowStreamOfflineState() error {
log.Println("----- Stream offline! Showing offline state!")
var outputDir = config.Config.PublicHLSPath
var variantPlaylistPath = config.Config.PublicHLSPath
2020-05-29 18:08:33 -07:00
if config.Config.IPFS.Enabled || config.Config.S3.Enabled {
outputDir = config.Config.PrivateHLSPath
variantPlaylistPath = config.Config.PrivateHLSPath
2020-06-01 16:53:31 -07:00
}
2020-06-09 01:52:15 -07:00
outputDir = path.Join(outputDir, "%v")
var variantPlaylistName = path.Join(variantPlaylistPath, "%v", "stream.m3u8")
var videoMaps = make([]string, 0)
var streamMaps = make([]string, 0)
var videoMapsString = ""
var streamMappingString = ""
if config.Config.VideoSettings.EnablePassthrough || len(config.Config.VideoSettings.StreamQualities) == 0 {
log.Println("Enabling passthrough video for offline state")
videoMapsString = "-b:v 1200k -b:a 128k" // Since we're compositing multiple sources we can't infer bitrate, so pick something reasonable.
streamMaps = append(streamMaps, fmt.Sprintf("v:%d", 0))
} else {
for index, quality := range config.Config.VideoSettings.StreamQualities {
maxRate := math.Floor(float64(quality.Bitrate) * 0.8)
videoMaps = append(videoMaps, fmt.Sprintf("-map v:0 -c:v:%d libx264 -b:v:%d %dk -maxrate %dk -bufsize %dk", index, index, int(quality.Bitrate), int(maxRate), int(maxRate)))
streamMaps = append(streamMaps, fmt.Sprintf("v:%d", index))
videoMapsString = strings.Join(videoMaps, " ")
}
}
framerate := 25
streamMappingString = "-var_stream_map \"" + strings.Join(streamMaps, " ") + "\""
ffmpegFlags := []string{
"-hide_banner",
// "-stream_loop 100",
// "-fflags", "+genpts",
"-i", config.Config.VideoSettings.OfflineImage,
"-i", "webroot/thumbnail.jpg",
"-filter_complex", "\"[0:v]scale=2640:2360[bg];[bg][1:v]overlay=200:250:enable='between(t,0,3)'\"",
videoMapsString, // All the different video variants
"-f hls",
// "-hls_list_size " + strconv.Itoa(config.Config.Files.MaxNumberInPlaylist),
"-hls_time 4", // + strconv.Itoa(config.Config.VideoSettings.ChunkLengthInSeconds),
"-hls_playlist_type", "event",
"-master_pl_name", "stream.m3u8",
"-strftime 1",
"-use_localtime 1",
"-hls_flags temp_file",
"-tune", "zerolatency",
"-g " + strconv.Itoa(framerate*2), " -keyint_min " + strconv.Itoa(framerate*2), // multiply your output frame rate * 2. For example, if your input is -framerate 30, then use -g 60
"-framerate " + strconv.Itoa(framerate),
"-preset " + config.Config.VideoSettings.EncoderPreset,
"-sc_threshold 0", // don't create key frames on scene change - only according to -g
"-profile:v", "main", // Main for standard definition (SD) to 640×480, High for high definition (HD) to 1920×1080
// "-movflags +faststart",
"-pix_fmt yuv420p",
streamMappingString,
"-hls_segment_filename " + path.Join(outputDir, "offline-%s.ts"),
// "-s", "720x480", // size
variantPlaylistName,
}
ffmpegFlagsString := strings.Join(ffmpegFlags, " ")
ffmpegCmd := config.Config.FFMpegPath + " " + ffmpegFlagsString
// log.Println(ffmpegCmd)
_, err := exec.Command("sh", "-c", ffmpegCmd).Output()
return err
}
//Start starts the ffmpeg process
func Start() error {
var outputDir = config.Config.PublicHLSPath
var variantPlaylistPath = config.Config.PublicHLSPath
if config.Config.IPFS.Enabled || config.Config.S3.Enabled {
outputDir = config.Config.PrivateHLSPath
variantPlaylistPath = config.Config.PrivateHLSPath
}
outputDir = path.Join(outputDir, "%v")
var variantPlaylistName = path.Join(variantPlaylistPath, "%v", "stream.m3u8")
2020-06-09 01:52:15 -07:00
log.Printf("Starting transcoder saving to /%s.", variantPlaylistName)
pipePath := utils.GetTemporaryPipePath()
2020-06-09 01:52:15 -07:00
var videoMaps = make([]string, 0)
var streamMaps = make([]string, 0)
var audioMaps = make([]string, 0)
var videoMapsString = ""
var audioMapsString = ""
var streamMappingString = ""
2020-06-18 21:57:02 -07:00
var profileString = ""
if config.Config.VideoSettings.EnablePassthrough || len(config.Config.VideoSettings.StreamQualities) == 0 {
log.Println("Enabling passthrough video for stream")
streamMaps = append(streamMaps, fmt.Sprintf("v:%d,a:%d", 0, 0))
2020-06-18 21:57:02 -07:00
videoMaps = append(videoMaps, "-map v:0 -c:v copy")
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
} else {
for index, quality := range config.Config.VideoSettings.StreamQualities {
2020-06-14 21:17:01 -07:00
maxRate := math.Floor(float64(quality.Bitrate) * 0.8)
videoMaps = append(videoMaps, fmt.Sprintf("-map v:0 -c:v:%d libx264 -b:v:%d %dk -maxrate %dk -bufsize %dk", index, index, int(quality.Bitrate), int(maxRate), int(maxRate)))
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-18 21:57:02 -07:00
profileString = "-profile:v high" // Main for standard definition (SD) to 640×480, High for high definition (HD) to 1920×1080
}
2020-06-09 01:52:15 -07:00
}
2020-06-14 22:26:31 -07:00
framerate := 25
2020-06-14 21:17:01 -07:00
streamMappingString = "-var_stream_map \"" + strings.Join(streamMaps, " ") + "\""
2020-06-09 01:52:15 -07:00
ffmpegFlags := []string{
"-hide_banner",
// "-re",
"-fflags", "+genpts",
2020-06-09 01:52:15 -07:00
"-i pipe:",
2020-06-11 17:34:20 -07: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 01:52:15 -07:00
"-master_pl_name stream.m3u8",
2020-06-14 21:17:01 -07:00
"-framerate " + strconv.Itoa(framerate),
"-g " + strconv.Itoa(framerate*2), " -keyint_min " + strconv.Itoa(framerate*2), // multiply your output frame rate * 2. For example, if your input is -framerate 30, then use -g 60
2020-06-13 18:00:26 -07:00
// "-r 25",
"-preset " + config.Config.VideoSettings.EncoderPreset,
2020-06-11 17:34:20 -07:00
"-sc_threshold 0", // don't create key frames on scene change - only according to -g
2020-06-18 21:57:02 -07:00
profileString,
2020-06-13 18:00:26 -07:00
"-movflags +faststart",
"-pix_fmt yuv420p",
2020-06-09 01:52:15 -07:00
"-f hls",
"-hls_list_size " + strconv.Itoa(config.Config.Files.MaxNumberInPlaylist),
"-hls_delete_threshold 10", // Keep 10 unreferenced segments on disk before they're deleted.
"-hls_time " + strconv.Itoa(config.Config.VideoSettings.ChunkLengthInSeconds),
2020-06-09 01:52:15 -07:00
"-strftime 1",
"-use_localtime 1",
"-hls_segment_filename " + path.Join(outputDir, "stream-%Y%m%d-%s.ts"),
"-hls_flags delete_segments+program_date_time+temp_file",
"-tune zerolatency",
// "-s", "720x480", // size
streamMappingString,
2020-06-09 01:52:15 -07:00
variantPlaylistName,
}
ffmpegFlagsString := strings.Join(ffmpegFlags, " ")
ffmpegCmd := "cat " + pipePath + " | " + config.Config.FFMpegPath + " " + ffmpegFlagsString
2020-06-09 01:52:15 -07:00
2020-06-16 19:08:34 -07:00
// fmt.Println(ffmpegCmd)
2020-06-09 18:28:07 -07:00
_, err := exec.Command("sh", "-c", ffmpegCmd).Output()
return err
2020-05-29 18:08:33 -07:00
}
//WritePlaylist writes the playlist to disk
func WritePlaylist(data string, filePath string) error {
2020-05-29 18:08:33 -07:00
f, err := os.Create(filePath)
if err != nil {
return err
2020-05-29 18:08:33 -07:00
}
defer f.Close()
if _, err := f.WriteString(data); err != nil {
return err
2020-05-29 18:08:33 -07:00
}
return nil
2020-05-29 18:08:33 -07:00
}