-
Notifications
You must be signed in to change notification settings - Fork 46
/
ytdlp.go
82 lines (79 loc) · 2.71 KB
/
ytdlp.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
// Package ytdlp YouTube 视频下载
package ytdlp
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path"
"time"
ctrl "github.com/FloatTech/zbpctrl"
"github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext"
zero "github.com/wdvxdr1123/ZeroBot"
)
func init() {
en := control.Register("ytdlp", &ctrl.Options[*zero.Ctx]{
DisableOnDefault: false,
Brief: "ytdlp 下载器",
Help: "/yt-dlp [youtube-video-link]",
PrivateDataFolder: "ytdlp",
}).ApplySingle(ctxext.DefaultSingle)
// 初始化临时文件夹
tempFileDir := path.Join(en.DataFolder(), "temp")
err := os.MkdirAll(tempFileDir, 0750)
if err != nil {
panic(err)
}
// 注册指令
en.OnPrefix("/yt-dlp", zero.OnlyGroup, zero.SuperUserPermission).
SetBlock(true).
Handle(func(ctx *zero.Ctx) {
wd, err := os.Getwd()
if err != nil {
ctx.Send("系统错误,无法获取当前程序目录。")
}
_, err = exec.LookPath("yt-dlp")
if err != nil {
ctx.Send("目标服务器未安装 yt-dlp。")
return
}
url := ctx.State["args"].(string)
cmdVideoFileSize := exec.Command("yt-dlp", "--print", "%(filesize,filesize_approx)s", url)
videoFileSizeInByte, err := cmdVideoFileSize.Output()
if err != nil {
ctx.Send("获取视频文件大小失败,可能是服务器无法访问 YouTube。")
return
}
videoFileSize := string(videoFileSizeInByte)
cmdVideoTitle := exec.Command("yt-dlp", "--print", "%(title)s", url)
videoTitleInByte, err := cmdVideoTitle.Output()
if err != nil {
ctx.Send("获取视频标题失败,可能是服务器无法访问 YouTube。")
return
}
videoTitle := string(videoTitleInByte)
ctx.Send(fmt.Sprintf("视频标题:%s视频大小:%s即将开始下载视频,请稍候。", videoTitle, videoFileSize))
fileName := fmt.Sprintf("%d_%d.mp4", ctx.Event.Sender.ID, time.Now().Unix())
videoFilePath := path.Join(tempFileDir, fileName)
cmdDownload := exec.Command("yt-dlp", url, "-f", "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best", "-o", videoFilePath)
downloadLogInByte, err := cmdDownload.Output()
downloadLog := string(downloadLogInByte)
if err != nil {
ctx.Send(fmt.Sprintf("视频文件下载失败,可能存在网络波动。\n%s", downloadLog))
return
}
ctx.Send("文件下载成功,正在上传,请稍候。")
fullVideoFilePath := path.Join(wd, videoFilePath)
resp := ctx.UploadThisGroupFile(fullVideoFilePath, fileName, "")
if resp.Status != "ok" {
respData, err := json.Marshal(resp)
errMsg := string(respData)
if err != nil {
errMsg = err.Error()
}
ctx.Send(fmt.Sprintln("文件上传失败。", errMsg))
}
os.Remove(videoFilePath)
})
}