]> 127.0.0.1 Git - bili_danmu/.git/commitdiff
Add 切片后将csv弹幕转为xml弹幕
authorqydysky <qydysky@foxmail.com>
Mon, 7 Aug 2023 08:24:57 +0000 (16:24 +0800)
committerqydysky <qydysky@foxmail.com>
Mon, 7 Aug 2023 08:24:57 +0000 (16:24 +0800)
Reply/F.go
Reply/F/comp.go
Reply/F/danmuXml/danmuXml.go [new file with mode: 0644]
Reply/stream.go

index d459d8c7a243bbbd985afcf877f68ab039b51255..93a01df5e3c75fb1ab1f2f1fedb5442879bf97a9 100644 (file)
@@ -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()
 }
 
index 2d71306dcbeef56e9c7a43851675576cf564f60b..3933bfd41e2b55571abad69850ac9c4c6a13f8e2 100644 (file)
@@ -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 (file)
index 0000000..4981fad
--- /dev/null
@@ -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
+}
index ffbc9941c7cfaaa5bc03d3afaec4a48910ee8c16..9f27f76317f353359fd0afb87ddb6c1dcdb1a40e 100644 (file)
@@ -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 {