From 08079176a80e137af712987ca8fc4ec8587fa569 Mon Sep 17 00:00:00 2001 From: nopast <490964186@qq.com> Date: Mon, 18 Sep 2023 22:22:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9Effmpeg=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=88=86=E7=89=87=E5=8A=9F=E8=83=BD=20(#545)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 新增ffmpeg文件分片功能 * 修改按文件尺寸分段参数的默认值等行为 --------- Co-authored-by: kira1928 --- config.yml | 5 +++++ src/configs/config.go | 1 + src/pkg/parser/ffmpeg/ffmpeg.go | 19 +++++++++++++++---- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/config.yml b/config.yml index 021b2a4..b221cfe 100644 --- a/config.yml +++ b/config.yml @@ -25,6 +25,11 @@ out_put_tmpl: "" video_split_strategies: on_room_name_changed: false max_duration: 0s + # 仅在 use_native_flv_parser=false 时生效 + # 单位为字节 (byte) + # 有效值为正数,默认值 0 为无效 + # 负数为非法值,程序会输出 log 提醒,并无视所设定的数值 + max_file_size: 0 cookies: {} on_record_finished: convert_to_mp4: false diff --git a/src/configs/config.go b/src/configs/config.go index 5b8ac92..7f7b8a5 100644 --- a/src/configs/config.go +++ b/src/configs/config.go @@ -46,6 +46,7 @@ type Feature struct { type VideoSplitStrategies struct { OnRoomNameChanged bool `yaml:"on_room_name_changed"` MaxDuration time.Duration `yaml:"max_duration"` + MaxFileSize int `yaml:"max_file_size"` } // On record finished actions. diff --git a/src/pkg/parser/ffmpeg/ffmpeg.go b/src/pkg/parser/ffmpeg/ffmpeg.go index 082dff7..05f6ba3 100644 --- a/src/pkg/parser/ffmpeg/ffmpeg.go +++ b/src/pkg/parser/ffmpeg/ffmpeg.go @@ -8,9 +8,11 @@ import ( "net/url" "os" "os/exec" + "strconv" "sync" "time" + "github.com/hr3lxphr6j/bililive-go/src/instance" "github.com/hr3lxphr6j/bililive-go/src/live" "github.com/hr3lxphr6j/bililive-go/src/pkg/parser" "github.com/hr3lxphr6j/bililive-go/src/pkg/utils" @@ -127,8 +129,7 @@ func (p *Parser) ParseLiveStream(ctx context.Context, url *url.URL, live live.Li if err != nil { return err } - p.cmd = exec.Command( - ffmpegPath, + args := []string{ "-nostats", "-progress", "-", "-y", "-re", @@ -138,8 +139,18 @@ func (p *Parser) ParseLiveStream(ctx context.Context, url *url.URL, live live.Li "-i", url.String(), "-c", "copy", "-bsf:a", "aac_adtstoasc", - file, - ) + } + + inst := instance.GetInstance(ctx) + MaxFileSize := inst.Config.VideoSplitStrategies.MaxFileSize + if MaxFileSize < 0 { + inst.Logger.Infof("Invalid MaxFileSize: %d", MaxFileSize) + } else if MaxFileSize > 0 { + args = append(args, "-fs", strconv.Itoa(MaxFileSize)) + } + + args = append(args, file) + p.cmd = exec.Command(ffmpegPath, args...) if p.cmdStdIn, err = p.cmd.StdinPipe(); err != nil { return err }