Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
7b6d3ffe54 |
16
download.go
16
download.go
@@ -6,6 +6,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"sync"
|
||||||
//"path/filepath"
|
//"path/filepath"
|
||||||
|
|
||||||
. "github.com/kkdai/youtube"
|
. "github.com/kkdai/youtube"
|
||||||
@@ -21,7 +22,7 @@ func NewBatch(batchFile, destinationDir string) *Batch {
|
|||||||
|
|
||||||
func (b *Batch) Start() {
|
func (b *Batch) Start() {
|
||||||
//Start downloading all videos
|
//Start downloading all videos
|
||||||
|
log.Println("Start downloading...")
|
||||||
//open the file containing yt links
|
//open the file containing yt links
|
||||||
linkfile, err := os.Open(b.batchfile)
|
linkfile, err := os.Open(b.batchfile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -34,18 +35,27 @@ func (b *Batch) Start() {
|
|||||||
//That should be enough for yt links.
|
//That should be enough for yt links.
|
||||||
scanner := bufio.NewScanner(linkfile)
|
scanner := bufio.NewScanner(linkfile)
|
||||||
|
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
//Call download
|
//Call download
|
||||||
go downloadVideo(b.destDir, scanner.Text())
|
log.Println(scanner.Text())
|
||||||
|
//add go routine to wait for
|
||||||
|
wg.Add(1)
|
||||||
|
go downloadVideo(b.destDir, scanner.Text(), wg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//wait for all go routines to finish
|
||||||
|
wg.Wait()
|
||||||
if err := scanner.Err(); err != nil {
|
if err := scanner.Err(); err != nil {
|
||||||
log.Fatal(err)
|
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
|
// 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 := NewYoutube(false)
|
||||||
y.DecodeURL(url)
|
y.DecodeURL(url)
|
||||||
re := regexp.MustCompile("(mp4|webm|3gpp)")
|
re := regexp.MustCompile("(mp4|webm|3gpp)")
|
||||||
|
Reference in New Issue
Block a user