From d0c3dea7b5bd8f5895c24948b177a13ebb871820 Mon Sep 17 00:00:00 2001 From: qydysky Date: Mon, 7 Aug 2023 16:24:57 +0800 Subject: [PATCH] =?utf8?q?Add=20=E5=88=87=E7=89=87=E5=90=8E=E5=B0=86csv?= =?utf8?q?=E5=BC=B9=E5=B9=95=E8=BD=AC=E4=B8=BAxml=E5=BC=B9=E5=B9=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- Reply/F.go | 9 ++- Reply/F/comp.go | 1 + Reply/F/danmuXml/danmuXml.go | 103 +++++++++++++++++++++++++++++++++++ Reply/stream.go | 2 +- 4 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 Reply/F/danmuXml/danmuXml.go diff --git a/Reply/F.go b/Reply/F.go index d459d8c..93a01df 100644 --- a/Reply/F.go +++ b/Reply/F.go @@ -35,6 +35,7 @@ import ( send "github.com/qydysky/bili_danmu/Send" p "github.com/qydysky/part" + comp "github.com/qydysky/part/component" file "github.com/qydysky/part/file" limit "github.com/qydysky/part/limit" msgq "github.com/qydysky/part/msgq" @@ -1603,13 +1604,19 @@ func StartRecDanmu(c context.Context, filePath string) { var Recoder = websocket.Recorder{ Server: StreamWs, } - if e := Recoder.Start(filePath); e == nil { + if e := Recoder.Start(filePath + "0.csv"); e == nil { f.L(`I: `, `开始`) } else { f.L(`E: `, e) } <-c.Done() f.L(`I: `, `结束`) + + // 弹幕录制结束 + if e := comp.Run[string](`bili_danmu.Reply.StartRecDanmu.Stop`, context.Background(), &filePath); e != nil { + f.L(`E: `, e) + } + Recoder.Stop() } diff --git a/Reply/F/comp.go b/Reply/F/comp.go index 2d71306..3933bfd 100644 --- a/Reply/F/comp.go +++ b/Reply/F/comp.go @@ -1,5 +1,6 @@ package f import ( + _ "github.com/qydysky/bili_danmu/Reply/F/danmuXml" _ "github.com/qydysky/bili_danmu/Reply/F/liveOver" ) diff --git a/Reply/F/danmuXml/danmuXml.go b/Reply/F/danmuXml/danmuXml.go new file mode 100644 index 0000000..4981fad --- /dev/null +++ b/Reply/F/danmuXml/danmuXml.go @@ -0,0 +1,103 @@ +package danmuxml + +import ( + "bytes" + "context" + "encoding/json" + "encoding/xml" + "fmt" + "strconv" + + "github.com/dustin/go-humanize" + comp "github.com/qydysky/part/component" + file "github.com/qydysky/part/file" +) + +func init() { + if e := comp.Put[string](`bili_danmu.Reply.StartRecDanmu.Stop.danmuxml.toXml`, toXml); e != nil { + panic(e) + } +} + +type danmu struct { + XMLName xml.Name `xml:"i"` + Chatserver string `xml:"chatserver"` + Chatid int `xml:"chatid"` + Mission int `xml:"mission"` + Maxlimit int `xml:"maxlimit"` + State int `xml:"state"` + RealName int `xml:"real_name"` + Source string `xml:"source"` + D []danmuD `xml:"d"` +} + +type danmuD struct { + P string `xml:"p,attr"` + Data string `xml:",chardata"` +} + +type DataStyle struct { + Color string `json:"color"` + Border bool `json:"border"` + Mode int `json:"mode"` +} + +type Data struct { + Text string `json:"text"` + Style DataStyle `json:"style"` + Time float64 `json:"time"` +} + +func toXml(ctx context.Context, path *string) error { + d := danmu{ + Chatserver: "chat.bilibili.com", + Chatid: 0, + Mission: 0, + Maxlimit: 1500, + State: 0, + RealName: 0, + Source: "e-r", + } + + csvf := file.New((*path)+"0.csv", 0, false) + var data = Data{} + for i := 0; true; i += 1 { + if line, e := csvf.ReadUntil('\n', humanize.KByte, humanize.MByte); len(line) != 0 { + lined := bytes.SplitN(line, []byte{','}, 3) + if len(lined) == 3 { + if e := json.Unmarshal(lined[2], &data); e == nil { + if data.Style.Color == "" { + data.Style.Color = "#FFFFFF" + } + if color, e := strconv.ParseInt(data.Style.Color[1:], 16, 0); e == nil { + d.D = append(d.D, danmuD{ + P: fmt.Sprintf("%s,1,25,%d,0,0,0,%d", lined[0], color, i), + Data: data.Text, + }) + } + } + } + } else if e != nil { + break + } + } + csvf.Close() + + output, err := xml.MarshalIndent(d, "", " ") + if err != nil { + return err + } + + f := file.New((*path)+"0.xml", 0, false) + defer f.Close() + + if _, err := f.Write([]byte(xml.Header), true); err != nil { + return err + } + + if _, err := f.Write(output, true); err != nil { + return err + } + + return nil +} diff --git a/Reply/stream.go b/Reply/stream.go index ffbc994..9f27f76 100644 --- a/Reply/stream.go +++ b/Reply/stream.go @@ -1274,7 +1274,7 @@ func (t *M4SStream) Start() bool { } fj.Close() } - go StartRecDanmu(contextC, cp+"0.csv") //保存弹幕 + go StartRecDanmu(contextC, cp) //保存弹幕 go Ass_f(contextC, cp, cp+"0", time.Now()) //开始ass startT := time.Now() if e := ms.PusherToFile(contextC, cp+`0.`+st, startf, stopf); e != nil { -- 2.39.2