-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathdanmu.go
266 lines (242 loc) · 6.93 KB
/
danmu.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// 弹幕下载相关
package main
import (
"context"
"fmt"
"path/filepath"
"time"
"github.com/orzogc/acfundanmu"
)
// 不同的视频分辨率对应的弹幕字幕设置
var subConfigs = map[int]acfundanmu.SubConfig{
0: {PlayResX: 720, PlayResY: 1280, FontSize: 60}, // 这是手机直播,有一些是 540X960
540: {PlayResX: 960, PlayResY: 540, FontSize: 30},
720: {PlayResX: 1280, PlayResY: 720, FontSize: 40},
1080: {PlayResX: 1920, PlayResY: 1080, FontSize: 60},
}
// 下载直播弹幕
func (s streamer) getDanmu(ctx context.Context, info liveInfo) {
defer func() {
if err := recover(); err != nil {
lPrintErr("Recovering from panic in getDanmu(), the error is:", err)
msg := "下载%s的直播弹幕发生错误,如要重启下载,请运行 startdanmu %d"
lPrintErrf(msg, s.longID(), s.UID)
desktopNotify("下载" + s.Name + "的直播弹幕发生错误")
s.sendMirai(fmt.Sprintf(msg, s.Name, s.UID), false)
}
}()
if s.KeepOnline {
if is_login_acfun() {
lPrintf("开始在%s的直播间挂机", s.longID())
} else {
lPrintErrf("没有登陆AcFun帐号,取消在%s的直播间挂机", s.longID())
if !s.Danmu {
return
}
}
}
if s.Danmu {
lPrintln("开始下载" + s.longID() + "的直播弹幕")
lPrintln("本次下载的 ass 文件保存在" + info.assFile)
if *isListen {
lPrintf("如果想提前结束下载%s的直播弹幕,运行 stopdanmu %d", s.longID(), s.UID)
}
if s.Notify.NotifyDanmu {
if !s.Record {
desktopNotify("开始下载" + s.Name + "的直播弹幕")
s.sendMirai(fmt.Sprintf("开始下载%s的直播弹幕:%s,观看地址:%s", s.Name, s.getTitle(), s.getURL()), false)
}
}
}
var cookies acfundanmu.Cookies
if s.KeepOnline {
cookies = acfun_cookies()
}
ac, err := acfundanmu.NewAcFunLive(acfundanmu.SetLiverUID(int64(s.UID)), acfundanmu.SetCookies(cookies))
checkErr(err)
_ = ac.StartDanmu(ctx, false)
if s.Danmu {
ac.WriteASS(ctx, info.cfg, info.assFile, true)
defer s.moveFile(info.assFile)
} else if s.KeepOnline {
for {
if danmu := ac.GetDanmu(); danmu == nil {
break
}
}
} else {
lPrintErr("s.Danmu 或 s.KeepOnline 必须为 true")
return
}
time.Sleep(5 * time.Second)
Outer:
for {
select {
case <-ctx.Done():
break Outer
default:
// 因意外结束弹幕下载时重启下载
// 应付 AcFun API 可能出现的 bug
if s.isLiveOnByPage() {
if newLiveID := getLiveID(s.UID); newLiveID == info.LiveID {
lPrintWarn("因意外结束下载" + s.longID() + "的直播弹幕,尝试重启下载")
ac, err := acfundanmu.NewAcFunLive(acfundanmu.SetLiverUID(int64(s.UID)), acfundanmu.SetCookies(cookies))
checkErr(err)
_ = ac.StartDanmu(ctx, false)
if s.Danmu {
ac.WriteASS(ctx, info.cfg, info.assFile, false)
} else if s.KeepOnline {
for {
if danmu := ac.GetDanmu(); danmu == nil {
break
}
}
}
time.Sleep(10 * time.Second)
} else {
break Outer
}
} else {
break Outer
}
}
}
if s.KeepOnline {
lPrintf("停止在%s的直播间挂机", s.longID())
}
if s.Danmu {
lPrintln(s.longID() + "的直播弹幕下载已经结束")
if s.Notify.NotifyDanmu {
if !s.Record {
desktopNotify(s.Name + "的直播弹幕下载已经结束")
s.sendMirai(s.Name+"的直播弹幕下载已经结束", false)
}
}
}
}
// 退出直播弹幕下载相关操作
func (s streamer) quitDanmu(liveID string) {
lInfoMap.Lock()
defer lInfoMap.Unlock()
if info, ok := lInfoMap.info[liveID]; ok {
if s.Danmu {
info.isDanmu = false
}
if s.KeepOnline {
info.isKeepOnline = false
}
lInfoMap.info[info.LiveID] = info
}
}
// 初始化弹幕下载
func (s streamer) initDanmu(ctx context.Context, liveID, filename string) {
dctx, dcancel := context.WithCancel(ctx)
defer dcancel()
info, ok := getLiveInfo(liveID)
if ok {
if info.isDanmu && !info.isKeepOnline {
if s.Danmu && !s.KeepOnline {
lPrintWarnf("已经在下载%s的直播弹幕,如要重启下载,请先运行 stopdanmu %d", s.longID(), s.UID)
return
}
s.Danmu = false
} else if !info.isDanmu && info.isKeepOnline {
if !s.Danmu && s.KeepOnline {
lPrintWarn("已经在" + s.longID() + "的直播间挂机")
return
}
s.KeepOnline = false
} else if info.isDanmu && info.isKeepOnline {
lPrintWarnf("已经在下载%s的直播弹幕和在其直播间挂机,如要重启下载,请先运行 stopdanmu %d", s.longID(), s.UID)
return
}
} else {
// 获取直播源和对应的弹幕设置
var err error
info, err = s.getLiveInfo()
if err != nil {
lPrintErr(err)
msg := "无法获取%s的直播源,退出下载直播弹幕,请确定主播正在直播,如要重启下载直播弹幕,请运行 startdanmu %d"
lPrintErrf(msg, s.longID(), s.UID)
if s.Notify.NotifyDanmu {
desktopNotify("无法获取" + s.Name + "的直播源,退出下载直播弹幕")
s.sendMirai(fmt.Sprintf(msg, s.Name, s.UID), false)
}
return
}
}
if s.Danmu {
info.isDanmu = true
info.danmuCancel = dcancel
}
if s.KeepOnline {
info.isKeepOnline = true
info.onlineCancel = dcancel
}
assFile := transFilename(filename)
if assFile == "" {
return
}
info.assFile = assFile + ".ass"
info.cfg.Title = filepath.Base(assFile)
info.cfg.StartTime = time.Now().UnixNano()
setLiveInfo(info)
defer s.quitDanmu(info.LiveID)
s.getDanmu(dctx, info)
}
// 临时下载指定主播的直播弹幕
func startDanmu(uid int) bool {
s, ok := getStreamer(uid)
if !ok {
name := getName(uid)
if name == "" {
lPrintWarnf("不存在 uid 为%d的用户", uid)
return false
}
s = streamer{UID: uid, Name: name}
}
s.Notify.NotifyDanmu = true
s.Danmu = true
liveID := getLiveID(uid)
if liveID == "" {
lPrintErr(s.longID() + "不在直播,取消下载直播弹幕")
return false
}
if isDanmu(liveID) {
lPrintWarnf("已经在下载%s的直播弹幕,如要重启下载,请先运行 stopdanmu %d", s.longID(), s.UID)
return false
}
filename := getTime() + " " + s.Name + " " + s.getTitle()
// 查看程序是否处于监听状态
if *isListen {
// goroutine 是为了快速返回
go s.initDanmu(mainCtx, liveID, filename)
} else {
// 程序只在单独下载一个直播弹幕,不用 goroutine,防止程序提前结束运行
s.initDanmu(mainCtx, liveID, filename)
}
return true
}
// 停止下载指定主播的直播弹幕
func stopDanmu(uid int) bool {
infoList, ok := getLiveInfoByUID(uid)
if !ok {
lPrintWarnf("没有在下载 uid 为%d的主播的直播弹幕", uid)
return true
}
for _, info := range infoList {
if info.isDanmu {
lPrintf("开始停止下载%s的liveID为%s直播弹幕", longID(uid), info.LiveID)
info.danmuCancel()
}
}
return true
}
// 临时下载指定主播的直播视频和弹幕
func startRecDan(uid int) bool {
return startRec(uid, true)
}
// 取消下载指定主播的直播视频和弹幕
func stopRecDan(uid int) bool {
return stopRec(uid) && stopDanmu(uid)
}