Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
752dce9751 | |||
8d11b58cec | |||
35694eaed2 | |||
400aab0964 | |||
fc046da8a6 | |||
e37ec06287 |
10
README.md
10
README.md
@@ -1,3 +1,11 @@
|
|||||||
# ytbatchdownloader
|
# ytbatchdownloader
|
||||||
|
|
||||||
A simple Youtube downloader to load multiple videos.
|
A simple Youtube downloader to load multiple videos.
|
||||||
|
|
||||||
|
## Credits
|
||||||
|
|
||||||
|
Used libraries:
|
||||||
|
|
||||||
|
[youtube downloader](https://github.com/kkdai/youtube)
|
||||||
|
|
||||||
|
[uiprogress](https://github.com/gosuri/uiprogress)
|
||||||
|
46
download.go
46
download.go
@@ -6,9 +6,11 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
//"path/filepath"
|
//"path/filepath"
|
||||||
|
|
||||||
|
"github.com/gosuri/uiprogress"
|
||||||
. "github.com/kkdai/youtube"
|
. "github.com/kkdai/youtube"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -35,24 +37,34 @@ 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)
|
||||||
|
|
||||||
|
//Start rendering for progressbar
|
||||||
|
//This is the initialization the actual bars
|
||||||
|
//will be instantiated in the go routines
|
||||||
|
uiprogress.Start()
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
//Call download
|
//Call download
|
||||||
log.Println(scanner.Text())
|
|
||||||
//add go routine to wait for
|
//add go routine to wait for
|
||||||
wg.Add(1)
|
if strings.TrimSpace(scanner.Text()) != "" {
|
||||||
go downloadVideo(b.destDir, scanner.Text(), wg)
|
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
|
//wait for all go routines to finish
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
log.Println("All downloads finished")
|
||||||
if err := scanner.Err(); err != nil {
|
if err := scanner.Err(); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func downloadVideo(destDir, url string, wg sync.WaitGroup) {
|
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 Done() so that WaitGroup knows when the routine finishes
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
@@ -61,8 +73,28 @@ func downloadVideo(destDir, url string, wg sync.WaitGroup) {
|
|||||||
re := regexp.MustCompile("(mp4|webm|3gpp)")
|
re := regexp.MustCompile("(mp4|webm|3gpp)")
|
||||||
fileext := re.FindString(y.StreamList[0]["type"])
|
fileext := re.FindString(y.StreamList[0]["type"])
|
||||||
filename := fmt.Sprintf("%s/%s.%s", destDir, y.StreamList[0]["title"], fileext)
|
filename := fmt.Sprintf("%s/%s.%s", destDir, y.StreamList[0]["title"], fileext)
|
||||||
|
filenameRune := []rune(y.StreamList[0]["title"])
|
||||||
|
shortFilename := string(filenameRune[0:20]) + "..."
|
||||||
|
|
||||||
log.Println("Start downloading:", filename)
|
go func() {
|
||||||
y.StartDownload(filename)
|
bar := uiprogress.AddBar(100).AppendCompleted()
|
||||||
log.Println("Finished downloading:", filename)
|
|
||||||
|
//Custom decorator (the short name in front of the bar)
|
||||||
|
bar.PrependFunc(func(b *uiprogress.Bar) string {
|
||||||
|
return shortFilename
|
||||||
|
})
|
||||||
|
|
||||||
|
var i int64 = 0
|
||||||
|
for i < 100 {
|
||||||
|
i = <-y.DownloadPercent
|
||||||
|
bar.Set(int(i))
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
//start the actual download
|
||||||
|
err := y.StartDownload(filename)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Failed to download", url)
|
||||||
|
log.Println("Message:", err.Error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user