forked from kkdai/youtube
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdownloader.go
More file actions
167 lines (141 loc) · 3.84 KB
/
downloader.go
File metadata and controls
167 lines (141 loc) · 3.84 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
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
package downloader
import (
"context"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"github.com/kkdai/youtube/v2"
"github.com/vbauerster/mpb/v5"
"github.com/vbauerster/mpb/v5/decor"
)
// Downloader offers high level functions to download videos into files
type Downloader struct {
youtube.Client
OutputDir string // optional directory to store the files
}
func (dl *Downloader) getOutputFile(v *youtube.Video, format *youtube.Format, outputFile string) (string, error) {
if outputFile == "" {
outputFile = SanitizeFilename(v.Title)
outputFile += pickIdealFileExtension(format.MimeType)
}
if dl.OutputDir != "" {
if err := os.MkdirAll(dl.OutputDir, 0o755); err != nil {
return "", err
}
outputFile = filepath.Join(dl.OutputDir, outputFile)
}
return outputFile, nil
}
// Download : Starting download video by arguments.
func (dl *Downloader) Download(ctx context.Context, v *youtube.Video, format *youtube.Format, outputFile string) error {
destFile, err := dl.getOutputFile(v, format, outputFile)
if err != nil {
return err
}
// Create output file
out, err := os.Create(destFile)
if err != nil {
return err
}
defer out.Close()
dl.logf("Download to file=%s", destFile)
return dl.videoDLWorker(ctx, out, v, format)
}
// DownloadWithHighQuality : Starting downloading video with high quality (>720p).
func (dl *Downloader) DownloadWithHighQuality(ctx context.Context, outputFile string, v *youtube.Video, quality string) error {
var videoFormat, audioFormat *youtube.Format
switch quality {
case "hd1080":
videoFormat = v.Formats.FindByItag(137)
audioFormat = v.Formats.FindByItag(140)
default:
return fmt.Errorf("unknown quality: %s", quality)
}
if videoFormat == nil {
return fmt.Errorf("no format video/mp4 for %s found", quality)
}
if audioFormat == nil {
return fmt.Errorf("no format audio/mp4 for %s found", quality)
}
destFile, err := dl.getOutputFile(v, videoFormat, outputFile)
if err != nil {
return err
}
outputDir := filepath.Dir(destFile)
// Create temporary video file
videoFile, err := ioutil.TempFile(outputDir, "youtube_*.m4v")
if err != nil {
return err
}
defer os.Remove(videoFile.Name())
// Create temporary audio file
audioFile, err := ioutil.TempFile(outputDir, "youtube_*.m4a")
if err != nil {
return err
}
defer os.Remove(audioFile.Name())
dl.logf("Downloading video file...")
err = dl.videoDLWorker(ctx, videoFile, v, videoFormat)
if err != nil {
return err
}
dl.logf("Downloading audio file...")
err = dl.videoDLWorker(ctx, audioFile, v, audioFormat)
if err != nil {
return err
}
ffmpegVersionCmd := exec.Command("ffmpeg", "-y",
"-i", videoFile.Name(),
"-i", audioFile.Name(),
"-strict",
"-2",
"-shortest",
destFile,
"-loglevel", "warning",
)
ffmpegVersionCmd.Stderr = os.Stderr
ffmpegVersionCmd.Stdout = os.Stdout
dl.logf("merging video and audio to %s", destFile)
return ffmpegVersionCmd.Run()
}
func (dl *Downloader) videoDLWorker(ctx context.Context, out *os.File, video *youtube.Video, format *youtube.Format) error {
resp, err := dl.GetStreamContext(ctx, video, format)
if err != nil {
return err
}
defer resp.Body.Close()
prog := &progress{
contentLength: float64(resp.ContentLength),
}
// create progress bar
progress := mpb.New(mpb.WithWidth(64))
bar := progress.AddBar(
int64(prog.contentLength),
mpb.PrependDecorators(
decor.CountersKibiByte("% .2f / % .2f"),
decor.Percentage(decor.WCSyncSpace),
),
mpb.AppendDecorators(
decor.EwmaETA(decor.ET_STYLE_GO, 90),
decor.Name(" ] "),
decor.EwmaSpeed(decor.UnitKiB, "% .2f", 60),
),
)
reader := bar.ProxyReader(resp.Body)
mw := io.MultiWriter(out, prog)
_, err = io.Copy(mw, reader)
if err != nil {
return err
}
progress.Wait()
return nil
}
func (dl *Downloader) logf(format string, v ...interface{}) {
if dl.Debug {
log.Printf(format, v...)
}
}