-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathjob.go
More file actions
138 lines (114 loc) · 3.19 KB
/
Copy pathjob.go
File metadata and controls
138 lines (114 loc) · 3.19 KB
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
package ytdlpjob
import (
"bufio"
"context"
"encoding/json"
"os/exec"
"strconv"
"strings"
"github.com/tanq16/danzo/internal/highway"
"github.com/tanq16/danzo/utils"
)
type YTDLPProgress struct {
DownloadedBytes string `json:"downloaded_bytes"`
TotalBytes string `json:"total_bytes"`
TotalBytesEst string `json:"total_bytes_estimate"`
Status string `json:"status"`
}
type YTDLPJob struct {
URL string
OutputPath string
}
func New(url, outputPath string) *YTDLPJob {
return &YTDLPJob{
URL: url,
OutputPath: outputPath,
}
}
func (j *YTDLPJob) ID() string {
if j.OutputPath != "" {
return j.OutputPath
}
return j.URL
}
func (j *YTDLPJob) Type() string { return "ytdlp" }
func (j *YTDLPJob) Run(ctx context.Context, prog chan<- highway.Progress) error {
args := []string{j.URL, "--newline", "--progress-template", `JSON_PROGRESS: {"downloaded_bytes": "%(progress.downloaded_bytes)s", "total_bytes": "%(progress.total_bytes)s", "total_bytes_estimate": "%(progress.total_bytes_estimate)s", "status": "%(progress.status)s"}`}
if j.OutputPath != "" {
args = append(args, "-o", j.OutputPath)
}
cmd := exec.CommandContext(ctx, "yt-dlp", args...)
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
if err := cmd.Start(); err != nil {
return err
}
scanner := bufio.NewScanner(stdout)
currentPhase := "Downloading"
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "[Merger]") || strings.Contains(line, "Merging formats") {
currentPhase = "Merging"
prog <- highway.Progress{
JobID: j.ID(),
Type: highway.ProgressTypeSubStatus,
Message: currentPhase,
SubStatus: "ffmpeg consolidating streams...",
}
continue
}
if strings.HasPrefix(line, "JSON_PROGRESS: ") {
jsonStr := strings.TrimPrefix(line, "JSON_PROGRESS: ")
var p YTDLPProgress
if err := json.Unmarshal([]byte(jsonStr), &p); err != nil {
continue
}
if currentPhase == "Merging" {
continue
}
downBytes, _ := strconv.ParseInt(p.DownloadedBytes, 10, 64)
var totalBytes int64
if p.TotalBytes != "NA" {
totalBytes, _ = strconv.ParseInt(p.TotalBytes, 10, 64)
} else if p.TotalBytesEst != "NA" {
totalBytes, _ = strconv.ParseInt(p.TotalBytesEst, 10, 64)
}
if totalBytes > 0 {
prog <- highway.Progress{
JobID: j.ID(),
Type: highway.ProgressTypeProgress,
Message: currentPhase,
Current: downBytes,
Total: totalBytes,
Extra: utils.FormatBytes(uint64(downBytes)) + "/" + utils.FormatBytes(uint64(totalBytes)),
}
}
}
}
err = cmd.Wait()
if err != nil {
prog <- highway.Progress{JobID: j.ID(), Done: true, Error: err, ErrMsg: err.Error()}
return err
}
prog <- highway.Progress{JobID: j.ID(), Done: true}
return nil
}
type ytdlpJobState struct {
URL string `json:"url"`
OutputPath string `json:"outputPath"`
}
func (j *YTDLPJob) Marshal() ([]byte, error) {
return json.Marshal(ytdlpJobState{
URL: j.URL,
OutputPath: j.OutputPath,
})
}
func Unmarshal(data []byte) (highway.Job, error) {
var state ytdlpJobState
if err := json.Unmarshal(data, &state); err != nil {
return nil, err
}
return New(state.URL, state.OutputPath), nil
}