From 955527704d3519533523bc1c5acbdfe0d3e2ffd0 Mon Sep 17 00:00:00 2001 From: qydysky Date: Sun, 9 Apr 2023 00:21:08 +0800 Subject: [PATCH] =?utf8?q?Improve=20=E5=BF=AB=E9=80=9F=E5=88=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- Reply/F.go | 45 +++++++++++------------- Reply/Reply.go | 14 +++----- Reply/stream.go | 92 ++++++++++++++++++++++--------------------------- go.mod | 2 +- go.sum | 6 ++++ 5 files changed, 73 insertions(+), 86 deletions(-) diff --git a/Reply/F.go b/Reply/F.go index 6644589..d066ffb 100644 --- a/Reply/F.go +++ b/Reply/F.go @@ -2,6 +2,7 @@ package reply import ( "bytes" + "context" "encoding/json" "errors" "fmt" @@ -196,7 +197,7 @@ func init() { } // 设定字幕文件名,为""时停止输出 -func Ass_f(save_path string, filePath string, st time.Time) { +func Ass_f(contextC context.Context, save_path string, filePath string, st time.Time) { if !IsOn(`仅保存当前直播间流`) { return } @@ -220,6 +221,9 @@ func Ass_f(save_path string, filePath string, st time.Time) { } f.Write([]byte(ass.header), true) ass.startT = st + + <-contextC.Done() + ass.file = "" } // 传入要显示的单条字幕 @@ -304,17 +308,6 @@ func StreamOStart(roomid int) { return } tmp.common.Roomid = roomid - - //录制回调,关于ass - tmp.Callback_startRec = func(ms *M4SStream) error { - StartRecDanmu(ms.Current_save_path + "0.csv") - Ass_f(ms.Current_save_path, ms.Current_save_path+"0", time.Now()) //开始ass - return nil - } - tmp.Callback_stopRec = func(_ *M4SStream) { - StopRecDanmu() - Ass_f("", "", time.Now()) //停止ass - } //实例回调,避免重复录制 tmp.Callback_start = func(ms *M4SStream) error { //流服务添加 @@ -365,6 +358,16 @@ func StreamOStop(roomid int) { } } +// 实例切断 +func StreamOCut(roomid int) { + if v, ok := streamO.Load(roomid); ok { + if v.(*M4SStream).Status.Islive() { + v.(*M4SStream).msg.PushLock_tag(`cut`, v.(*M4SStream)) + flog.L(`I: `, `已切片 `+strconv.Itoa(roomid)) + } + } +} + // type Obs struct { // c obsws.Client // Prog string //程序路径 @@ -1441,7 +1444,7 @@ var Recoder = websocket.Recorder{ Server: StreamWs, } -func StartRecDanmu(filePath string) { +func StartRecDanmu(c context.Context, filePath string) { if !IsOn(`仅保存当前直播间流`) || !IsOn("弹幕回放") { return } @@ -1451,6 +1454,9 @@ func StartRecDanmu(filePath string) { } else { f.L(`E: `, e) } + <-c.Done() + flog.Base("弹幕回放").L(`T: `, `停止`) + Recoder.Stop() } func PlayRecDanmu(filePath string) (*websocket.Server, func()) { @@ -1460,14 +1466,6 @@ func PlayRecDanmu(filePath string) (*websocket.Server, func()) { return websocket.Play(filePath) } -func StopRecDanmu() { - if !IsOn(`仅保存当前直播间流`) || !IsOn("弹幕回放") { - return - } - flog.Base("弹幕回放").L(`T: `, `停止`) - Recoder.Stop() -} - // 此次直播的交互人数 var communicate Communicate @@ -1534,10 +1532,7 @@ func (t *DanmuReLiveTriger) Check(uid, msg string) { if t.reload.CompareAndSwap(false, true) { flog.Base_add("指定弹幕重启录制").L(`I: `, uid, msg, "请求重启录制") go func() { - if v, ok := c.C.K_v.LoadV(`仅保存当前直播间流`).(bool); ok && v { - StreamOStop(c.C.Roomid) //停止其他房间录制 - } - StreamOStart(c.C.Roomid) + StreamOCut(c.C.Roomid) time.Sleep(time.Minute) t.reload.Store(false) }() diff --git a/Reply/Reply.go b/Reply/Reply.go index 7d627fa..fe013ed 100644 --- a/Reply/Reply.go +++ b/Reply/Reply.go @@ -503,13 +503,7 @@ func (replyF) room_change(s string) { var sh = []interface{}{"房间改变"} if c.C.Title != title.(string) { - //录制 - go func() { - if v, ok := c.C.K_v.LoadV(`修改标题时重新录制`).(bool); ok && v { - StreamOStop(c.C.Roomid) //停止其他房间录制 - } - StreamOStart(c.C.Roomid) - }() + StreamOCut(c.C.Roomid) } if title != nil { @@ -1098,6 +1092,9 @@ func (replyF) danmu(s string) { msglog := msglog.Log_show_control(false) { //附加功能 弹幕机 封禁 弹幕合并 + //对指定弹幕重新录制 + danmuReLiveTriger.Init(&c.C) + danmuReLiveTriger.Check(item.uid, item.msg) go Danmujif(item.msg) // if Autobanf(item.msg) { // Gui_show(Itos([]interface{}{"风险", item.auth, ":", item.msg})) @@ -1120,9 +1117,6 @@ func (replyF) danmu(s string) { } else { item.msg = _msg } - //对指定弹幕重新录制 - danmuReLiveTriger.Init(&c.C) - danmuReLiveTriger.Check(item.uid, item.msg) } Msg_showdanmu(item) } diff --git a/Reply/stream.go b/Reply/stream.go index 8001292..f3ed408 100644 --- a/Reply/stream.go +++ b/Reply/stream.go @@ -555,8 +555,8 @@ func (t *M4SStream) removeStream() (e error) { return nil } -func (t *M4SStream) saveStream() (e error) { - // 设置保存路径 +// 设置保存路径 +func (t *M4SStream) getSavepath() { t.Current_save_path = t.config.save_path + "/" + time.Now().Format("2006_01_02-15_04_05") + "-" + strconv.Itoa(t.common.Roomid) + "-" + @@ -564,17 +564,19 @@ func (t *M4SStream) saveStream() (e error) { t.common.Qn[t.common.Live_qn] + "-" + pstring.Rand(2, 3) + `/` - - // 清除初始值 - t.last_m4s = nil - t.first_buf = nil - // 显示保存位置 if rel, err := filepath.Rel(t.config.save_path, t.Current_save_path); err == nil { t.log.L(`I: `, "保存到", rel+`/0.`+t.stream_type) } else { t.log.L(`W: `, err) } +} + +func (t *M4SStream) saveStream() (e error) { + // 清除初始值 + t.last_m4s = nil + t.first_buf = nil + if s, ok := t.common.K_v.LoadV("直播Web服务路径").(string); ok && s != "" { t.log.L(`I: `, "Web服务地址:", t.common.Stream_url.String()+s) } @@ -596,37 +598,6 @@ func (t *M4SStream) saveStream() (e error) { // 保存到文件 if t.config.save_to_file { - var ( - contextC context.Context - cancle context.CancelFunc - ) - t.msg.Pull_tag_async(map[string]func(*M4SStream) (disable bool){ - `cut`: func(ms *M4SStream) (disable bool) { - select { - case <-contextC.Done(): - - } - if contextC != nil { - cancle() - } - contextC, cancle = context.WithCancel(context.Background()) - defer cancle() - - l := ms.log.Base_add(`文件`) - startf := func(_ *M4SStream) error { - l.L(`T: `, `start`) - return nil - } - stopf := func(_ *M4SStream) error { - l.L(`T: `, `stop`) - return nil - } - if e := ms.PusherToFile(contextC, ms.Current_save_path+`0.`+ms.stream_type, startf, stopf); e != nil { - l.L(`E: `, e) - } - return false - }, - }) t.msg.Pull_tag_only(`load`, func(ms *M4SStream) (disable bool) { ms.msg.Push_tag(`cut`, ms) return true @@ -912,7 +883,7 @@ func (t *M4SStream) saveStreamM4s() (e error) { } } - go func(link *m4s_link_item, path string) { + go func(link *m4s_link_item) { defer download_limit.UnBlock() link.status = 1 // 设置切片状态为正在下载 @@ -945,7 +916,7 @@ func (t *M4SStream) saveStreamM4s() (e error) { link.data.Append(r.Respon) link.status = 2 // 设置切片状态为下载完成 } - }(v, t.Current_save_path) + }(v) } // 等待队列下载完成 @@ -1156,19 +1127,43 @@ func (t *M4SStream) Start() bool { t.Stream_msg = msgq.NewType[[]byte]() // 设置事件 + if t.config.save_to_file { + var fc funcCtrl.FlashFunc + t.msg.Pull_tag_async(map[string]func(*M4SStream) (disable bool){ + `cut`: func(ms *M4SStream) (disable bool) { + contextC, cancle := context.WithCancel(context.Background()) + fc.FlashWithCallback(cancle) + + l := ms.log.Base_add(`文件`) + startf := func(_ *M4SStream) error { + l.L(`T: `, `start`) + return nil + } + stopf := func(_ *M4SStream) error { + l.L(`T: `, `stop`) + return nil + } + ms.getSavepath() + go StartRecDanmu(contextC, ms.Current_save_path+"0.csv") //保存弹幕 + go Ass_f(contextC, ms.Current_save_path, ms.Current_save_path+"0", time.Now()) //开始ass + if e := ms.PusherToFile(contextC, ms.Current_save_path+`0.`+ms.stream_type, startf, stopf); e != nil { + l.L(`E: `, e) + } + return false + }, + }) + } if t.Callback_stopRec != nil { t.msg.Pull_tag_only("stopRec", func(ms *M4SStream) (disable bool) { ms.Callback_stopRec(ms) return false }) } - if t.Callback_stop != nil { - t.msg.Pull_tag_only("stop", func(ms *M4SStream) (disable bool) { + t.msg.Pull_tag_only("stop", func(ms *M4SStream) (disable bool) { + if ms.Callback_stop != nil { ms.Callback_stop(ms) - return false - }) - } - t.msg.Pull_tag_only("stop", func(_ *M4SStream) (disable bool) { + } + t.msg.ClearAll() return true }) @@ -1250,7 +1245,7 @@ func (t *M4SStream) Stop() { } // 保存到文件 -func (t *M4SStream) PusherToFile(cont context.Context, filepath string, startFunc func(*M4SStream) error, stopFunc func(*M4SStream) error) error { +func (t *M4SStream) PusherToFile(contextC context.Context, filepath string, startFunc func(*M4SStream) error, stopFunc func(*M4SStream) error) error { f := file.New(filepath, 0, false) defer f.Close() f.Delete() @@ -1263,7 +1258,6 @@ func (t *M4SStream) PusherToFile(cont context.Context, filepath string, startFun if len(t.boot_buf) != 0 { f.Write(t.boot_buf, true) } - contextC, cancel := context.WithCancel(cont) t.Stream_msg.Pull_tag(map[string]func([]byte) bool{ `data`: func(b []byte) bool { select { @@ -1272,14 +1266,12 @@ func (t *M4SStream) PusherToFile(cont context.Context, filepath string, startFun default: } if len(b) == 0 { - cancel() return true } f.Write(b, true) return false }, `close`: func(_ []byte) bool { - cancel() return true }, }) diff --git a/go.mod b/go.mod index d6f5d31..7f5fa48 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/gofrs/uuid v4.3.0+incompatible github.com/gotk3/gotk3 v0.6.1 github.com/mdp/qrterminal/v3 v3.0.0 - github.com/qydysky/part v0.24.4 + github.com/qydysky/part v0.24.7 github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 golang.org/x/text v0.8.0 diff --git a/go.sum b/go.sum index ab5857c..580b562 100644 --- a/go.sum +++ b/go.sum @@ -91,6 +91,12 @@ github.com/qydysky/part v0.24.3 h1:bRnN+iYydDyYQHjHX+uVQzdSbglmVwwDUz2QSexO/DY= github.com/qydysky/part v0.24.3/go.mod h1:AQJH+BYeN30eKXjkDqGEtw0vx3wVGplBeOMLSyleEDo= github.com/qydysky/part v0.24.4 h1:JCA1CyggvUNu/B0xKEjnODaUp6rgEi1hjtJwqOTvnaw= github.com/qydysky/part v0.24.4/go.mod h1:AQJH+BYeN30eKXjkDqGEtw0vx3wVGplBeOMLSyleEDo= +github.com/qydysky/part v0.24.5 h1:DNvtDp/HEv59eYmsVnFk+dvEML5liFf/hQlSfPQmnRU= +github.com/qydysky/part v0.24.5/go.mod h1:AQJH+BYeN30eKXjkDqGEtw0vx3wVGplBeOMLSyleEDo= +github.com/qydysky/part v0.24.6 h1:IPNfsQG/er3gdN4beYe7kLzJiC/ds2FtsRgA9W/On/w= +github.com/qydysky/part v0.24.6/go.mod h1:AQJH+BYeN30eKXjkDqGEtw0vx3wVGplBeOMLSyleEDo= +github.com/qydysky/part v0.24.7 h1:wOtm1jvljIb1HZclDAfNMnw3rA/r5oQwifUCBt/d7qY= +github.com/qydysky/part v0.24.7/go.mod h1:AQJH+BYeN30eKXjkDqGEtw0vx3wVGplBeOMLSyleEDo= github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI= github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0= -- 2.39.2