-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
224 lines (194 loc) · 6.73 KB
/
main.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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/mattn/go-colorable"
"github.com/HoloArchivists/ytarchive-raw-go/download"
"github.com/HoloArchivists/ytarchive-raw-go/log"
"github.com/HoloArchivists/ytarchive-raw-go/merge"
"github.com/HoloArchivists/ytarchive-raw-go/util"
)
func printResult(logger *log.Logger, res *download.DownloadResult) {
if len(res.LostSegments) > 0 {
logger.Warnf("Lost %d segment(s) %v out of %d", len(res.LostSegments), res.LostSegments, res.TotalSegments)
}
if res.Error != nil {
logger.Errorf("Download task failed: %v", res.Error)
} else {
logger.Info("Download succeeded")
}
}
func main() {
colorable.EnableColorsStdout(nil)
disableQuickEditMode()
parseArgs()
increaseOpenFileLimit()
latestVersion, printNewVersion := versionCheck()
if printNewVersion {
log.Warnf("New version available: %s", latestVersion)
}
deleteTempDir := false
if tempDir == "" {
var err error
tempDir, err = ioutil.TempDir("", fmt.Sprintf("ytarchive-%s-", fregData.Metadata.Id))
if err != nil {
log.Fatalf("Unable to create temp dir: %v", err)
}
log.Infof("Storing temporary files in %s", tempDir)
deleteTempDir = !keepFiles
} else {
if err := os.MkdirAll(tempDir, 0755); err != nil {
log.Fatalf("Unable to create temp dir at '%s': %v", tempDir, err)
}
}
defer util.LockFile(filepath.Join(tempDir, fregData.Metadata.Id + ".lock"), func() {
log.Error("This video is already being downloaded by another instance.")
log.Error("Running two instances on the same video with the same temporary directory is not supported.")
})()
muxerOpts := &merge.MuxerOptions {
DeleteSegments: !keepFiles,
DisableResume: disableResume,
FinalFileBase: output,
FregData: &fregData,
// this looks wrong but is correct
IgnoreAudio: onlyVideo,
IgnoreVideo: onlyAudio,
Logger: log.New("muxer"),
Merger: merger,
MergerArguments: mergerArgs,
OverwriteTemp: overwriteTemp,
TempDir: tempDir,
}
if mergeOnlyFile != "" {
log.Infof("Merging video from %s", mergeOnlyFile)
if err := merge.MergeDownloadInfoJson(muxerOpts, mergeOnlyFile); err != nil {
log.Fatalf("Failed to merge: %v", err)
}
log.Info("Success!")
return
}
if keepFiles {
log.Warnf("Temporary files are configured to not be deleted. This will fill up your temporary storage over time.");
}
var ipPool *util.IPPool
if ipPoolFile != "" {
var err error
if ipPool, err = util.ParseIPPool(ipPoolFile); err != nil {
log.Fatalf("Failed to parse IP pool: %v", err)
}
}
client := util.NewClient(&util.HttpClientConfig {
IPPool: ipPool,
Network: network,
UseQuic: useQuic,
})
muxer, err := merge.CreateBestMuxer(muxerOpts)
if err != nil {
log.Fatalf("Unable to create muxer: %v", err)
}
dir := filepath.Dir(muxer.OutputFilePath())
err = os.MkdirAll(dir, 0755)
if err != nil {
log.Fatalf("Unable to create parent directories for output file: %v", err)
}
defer util.LockFile(muxer.OutputFilePath() + ".lock", func() {
log.Error("Another instance is already writing to this output file.")
})()
log.SetWindowName(windowName)
progress := download.NewProgress()
var audioTask, videoTask *download.DownloadTask
if !onlyVideo {
audioTask = &download.DownloadTask {
Client: client,
FailThreshold: failThreshold,
Fsync: fsync,
Logger: log.New("download.audio"),
Merger: muxer.AudioMerger(),
Progress: progress.Audio(),
QueueMode: queueMode,
RequeueDelay: requeueDelay,
RequeueFailed: requeueFailed,
RequeueLast: requeueLast,
RetryThreshold: retryThreshold,
SegmentCount: segmentCount,
SegmentDir: tempDir,
StartSegment: startSegment,
Threads: threads,
Url: fregData.BestAudio(preferredAudio),
}
}
if !onlyAudio {
videoTask = &download.DownloadTask {
Client: client,
FailThreshold: failThreshold,
Fsync: fsync,
Logger: log.New("download.video"),
Merger: muxer.VideoMerger(),
Progress: progress.Video(),
QueueMode: queueMode,
RequeueDelay: requeueDelay,
RequeueFailed: requeueFailed,
RequeueLast: requeueLast,
RetryThreshold: retryThreshold,
SegmentCount: segmentCount,
SegmentDir: tempDir,
StartSegment: startSegment,
Threads: threads,
Url: fregData.BestVideo(preferredVideo),
}
}
if onlyAudio {
videoTask = nil
merge.MergeNothing(muxer.VideoMerger())
} else if onlyVideo {
audioTask = nil
merge.MergeNothing(muxer.AudioMerger())
}
if audioTask != nil {
audioTask.Start()
}
if videoTask != nil {
videoTask.Start()
}
//start muxer early so segments can be deleted if keep-files is disabled
//for the tcp muxer
muxerResult := make(chan error)
go func() {
muxerResult <- muxer.Mux()
}()
var audioRes, videoRes *download.DownloadResult
if audioTask != nil {
audioRes = audioTask.Wait()
}
if videoTask != nil {
videoRes = videoTask.Wait()
}
if audioTask != nil {
printResult(audioTask.Logger, audioRes)
}
if videoTask != nil {
printResult(videoTask.Logger, videoRes)
}
log.Info("Waiting for muxing to finish")
log.Info("This can take a while for long videos, do NOT restart or all muxing progress will be lost")
res := <-muxerResult
//print again once it's done so it doesn't get buried in newer logs
if printNewVersion {
log.Warnf("New version available: %s", latestVersion)
}
if keepFiles {
log.Warnf("Temporary files are configured to not be deleted. This will fill up your temporary storage over time.");
}
if res != nil {
log.Fatalf("Muxing failed: %v", res)
}
if deleteTempDir {
if err = os.RemoveAll(tempDir); err != nil {
log.Warnf("Failed to delete temp dir: %v", err)
}
}
log.Info("Success!")
fmt.Fprintf(os.Stderr, "\n")
}