From dcdbf84c8c9dc7f36e48b316b5699012f48f9990 Mon Sep 17 00:00:00 2001 From: nite07 Date: Sun, 27 Oct 2024 22:53:25 +0800 Subject: [PATCH] breaking change: remove default ffmpeg args --- README.md | 23 +++++++-------- config/config.go | 69 +++++--------------------------------------- streamer/helper.go | 16 ++-------- streamer/streamer.go | 2 -- 4 files changed, 21 insertions(+), 89 deletions(-) diff --git a/README.md b/README.md index c0098a5..60f45fd 100644 --- a/README.md +++ b/README.md @@ -30,18 +30,17 @@ } ], "play": { - "video_codec": "libx264", - "preset": "medium", - "crf": 23, - "max_rate": "1000k", - "buf_size": "2000k", - "scale": "1920:1080", - "frame_rate": 30, - "audio_codec": "aac", - "audio_bitrate": "128k", - "audio_sample_rate": 44100, - "output_format": "flv", - "custom_args": [] + "-c:v": "libx264", + "-preset": "medium", + "-crf": 23, + "-maxrate": "1000k", + "-bufsize": "2000k", + "-vf": "1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2", + "-r": 30, + "-c:a": "aac", + "-b:a": "128k", + "-ar": 44100, + "-f": "flv" }, "output": { "rtmp_server": "rtmp://live-push.example.com/live", diff --git a/config/config.go b/config/config.go index 7b7a32a..21e4994 100644 --- a/config/config.go +++ b/config/config.go @@ -23,21 +23,6 @@ type InputItem struct { ItemType string `json:"-"` } -type PlayConfig struct { - VideoCodec string `json:"video_codec"` - Preset string `json:"preset"` - CRF int `json:"crf"` - MaxRate string `json:"max_rate"` - BufSize string `json:"buf_size"` - Scale string `json:"scale"` - FrameRate int `json:"frame_rate"` - AudioCodec string `json:"audio_codec"` - AudioBitrate string `json:"audio_bitrate"` - AudioSampleRate int `json:"audio_sample_rate"` - OutputFormat string `json:"output_format"` - CustomArgs []string `json:"custom_args"` -} - type LogConfig struct { PlayState bool `json:"play_state"` } @@ -48,13 +33,13 @@ type ServerConfig struct { } type Config struct { - Input []any `json:"input"` - InputItems []InputItem `json:"-"` // contains video file or dir - VideoList []InputItem `json:"-"` // only contains video file - Play PlayConfig `json:"play"` - Output OutputConfig `json:"output"` - Log LogConfig `json:"log"` - Server ServerConfig `json:"server"` + Input []any `json:"input"` + InputItems []InputItem `json:"-"` // contains video file or dir + VideoList []InputItem `json:"-"` // only contains video file + Play map[string]any `json:"play"` + Output OutputConfig `json:"output"` + Log LogConfig `json:"log"` + Server ServerConfig `json:"server"` } var GlobalConfig Config @@ -103,9 +88,6 @@ func validateConfig() error { if err := validateOutputConfig(); err != nil { return err } - if err := validatePlayConfig(); err != nil { - return err - } if err := validateServerConfig(); err != nil { return err } @@ -195,43 +177,6 @@ func validateOutputConfig() error { return nil } -func validatePlayConfig() error { - if GlobalConfig.Play.VideoCodec == "" { - GlobalConfig.Play.VideoCodec = "libx264" - } - if GlobalConfig.Play.Preset == "" { - GlobalConfig.Play.Preset = "fast" - } - if GlobalConfig.Play.CRF == 0 { - GlobalConfig.Play.CRF = 23 - } - if GlobalConfig.Play.MaxRate == "" { - GlobalConfig.Play.MaxRate = "8000k" - } - if GlobalConfig.Play.BufSize == "" { - GlobalConfig.Play.BufSize = "12000k" - } - if GlobalConfig.Play.Scale == "" { - GlobalConfig.Play.Scale = "1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2" - } - if GlobalConfig.Play.FrameRate == 0 { - GlobalConfig.Play.FrameRate = 30 - } - if GlobalConfig.Play.AudioCodec == "" { - GlobalConfig.Play.AudioCodec = "aac" - } - if GlobalConfig.Play.AudioBitrate == "" { - GlobalConfig.Play.AudioBitrate = "192k" - } - if GlobalConfig.Play.AudioSampleRate == 0 { - GlobalConfig.Play.AudioSampleRate = 48000 - } - if GlobalConfig.Play.OutputFormat == "" { - GlobalConfig.Play.OutputFormat = "flv" - } - return nil -} - func validateServerConfig() error { if GlobalConfig.Server.Addr == "" { GlobalConfig.Server.Addr = ":8080" diff --git a/streamer/helper.go b/streamer/helper.go index f8b2fa5..b43424d 100644 --- a/streamer/helper.go +++ b/streamer/helper.go @@ -20,21 +20,11 @@ func buildFFmpegArgs(videoItem config.InputItem) []string { args = append(args, "-i", videoPath, - "-c:v", config.GlobalConfig.Play.VideoCodec, - "-preset", config.GlobalConfig.Play.Preset, - "-crf", fmt.Sprintf("%d", config.GlobalConfig.Play.CRF), - "-maxrate", config.GlobalConfig.Play.MaxRate, - "-bufsize", config.GlobalConfig.Play.BufSize, - "-vf", fmt.Sprintf("scale=%s", config.GlobalConfig.Play.Scale), - "-r", fmt.Sprintf("%d", config.GlobalConfig.Play.FrameRate), - "-c:a", config.GlobalConfig.Play.AudioCodec, - "-b:a", config.GlobalConfig.Play.AudioBitrate, - "-ar", fmt.Sprintf("%d", config.GlobalConfig.Play.AudioSampleRate), - "-f", config.GlobalConfig.Play.OutputFormat, ) - if len(config.GlobalConfig.Play.CustomArgs) != 0 { - args = append(args, config.GlobalConfig.Play.CustomArgs...) + for k, v := range config.GlobalConfig.Play { + args = append(args, k) + args = append(args, fmt.Sprint(v)) } args = append(args, fmt.Sprintf("%s/%s", config.GlobalConfig.Output.RTMPServer, config.GlobalConfig.Output.StreamKey)) diff --git a/streamer/streamer.go b/streamer/streamer.go index 73385be..f0d100c 100644 --- a/streamer/streamer.go +++ b/streamer/streamer.go @@ -56,11 +56,9 @@ func NewStreamer(videoList []config.InputItem) *Streamer { func (s *Streamer) actorLoop() { for msg := range s.mailbox { if msg.messageType() != CloseMessage.messageType(CloseMessage{}) { - log.Printf("handle %s start\n", msg.messageType()) s.wg.Add(1) s.handleMessage(msg) s.wg.Done() - log.Printf("handle %s end\n", msg.messageType()) } else { s.handleMessage(msg) }