Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
400aab0964 | |||
fc046da8a6 | |||
e37ec06287 | |||
7b6d3ffe54 |
38
download.go
38
download.go
@@ -6,6 +6,8 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
//"path/filepath"
|
||||
|
||||
. "github.com/kkdai/youtube"
|
||||
@@ -21,7 +23,7 @@ func NewBatch(batchFile, destinationDir string) *Batch {
|
||||
|
||||
func (b *Batch) Start() {
|
||||
//Start downloading all videos
|
||||
|
||||
log.Println("Start downloading...")
|
||||
//open the file containing yt links
|
||||
linkfile, err := os.Open(b.batchfile)
|
||||
if err != nil {
|
||||
@@ -34,18 +36,32 @@ func (b *Batch) Start() {
|
||||
//That should be enough for yt links.
|
||||
scanner := bufio.NewScanner(linkfile)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
|
||||
for scanner.Scan() {
|
||||
//Call download
|
||||
go downloadVideo(b.destDir, scanner.Text())
|
||||
//add go routine to wait for
|
||||
if strings.TrimSpace(scanner.Text()) != "" {
|
||||
wg.Add(1)
|
||||
log.Println(scanner.Text())
|
||||
go downloadVideo(b.destDir, strings.TrimSpace(scanner.Text()), &wg)
|
||||
} else {
|
||||
log.Println("Ignoring blank line")
|
||||
}
|
||||
}
|
||||
|
||||
//wait for all go routines to finish
|
||||
wg.Wait()
|
||||
log.Println("All downloads finished")
|
||||
if err := scanner.Err(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func downloadVideo(destDir, url string) {
|
||||
func downloadVideo(destDir, url string, wg *sync.WaitGroup) {
|
||||
// NewYoutube(debug) if debug parameter will set true we can log of messages
|
||||
//defer Done() so that WaitGroup knows when the routine finishes
|
||||
defer wg.Done()
|
||||
y := NewYoutube(false)
|
||||
y.DecodeURL(url)
|
||||
re := regexp.MustCompile("(mp4|webm|3gpp)")
|
||||
@@ -53,6 +69,20 @@ func downloadVideo(destDir, url string) {
|
||||
filename := fmt.Sprintf("%s/%s.%s", destDir, y.StreamList[0]["title"], fileext)
|
||||
|
||||
log.Println("Start downloading:", filename)
|
||||
y.StartDownload(filename)
|
||||
go func() {
|
||||
var i int64 = 0
|
||||
for i < 100 {
|
||||
i = <-y.DownloadPercent
|
||||
if i%10 == 0 {
|
||||
log.Println(i, "%", y.StreamList[0]["title"])
|
||||
}
|
||||
}
|
||||
}()
|
||||
err := y.StartDownload(filename)
|
||||
if err != nil {
|
||||
log.Println("Failed to download", url)
|
||||
log.Println("Message:", err.Error())
|
||||
} else {
|
||||
log.Println("Finished downloading:", filename)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user