Skip to content
This repository has been archived by the owner on Nov 21, 2022. It is now read-only.

Commit

Permalink
Basic pipelines architecture + changed API
Browse files Browse the repository at this point in the history
See https://blog.golang.org/pipelines for the concurrency usage.
I changed also the `id` parameter to allow multiple entry.

Example: `youtube-dl -ids
https://www.youtube.com/watch\?v\=lWEbEtr_Vng,https://www.youtube.com/watch\?v\=HpNluHOAJFA\&list\=RDHpNluHOAJFA`

Should fix #17.
  • Loading branch information
yageek committed Oct 2, 2017
1 parent f203050 commit 07a8eef
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
30 changes: 30 additions & 0 deletions api/concurrency.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package api

import "sync"

//DownloadStreams download a batch of elements asynchronouslly
func DownloadStreams(maxOperations int, format, outputPath string, bitrate uint, urls []string) <-chan error {

var wg sync.WaitGroup
wg.Add(len(urls))

ch := make(chan error, maxOperations)
for _, url := range urls {
go func(url string) {
defer wg.Done()

if ID, err := GetVideoId(url); err != nil {
ch <- err
} else {
ch <- APIGetVideoStream(format, ID, outputPath, bitrate)
}
}(url)
}

go func() {
wg.Wait()
close(ch)
}()

return ch
}
30 changes: 20 additions & 10 deletions ytd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"os"
"runtime"
"runtime/pprof"
"strings"

"github.com/Ch3ck/youtube-dl/api"
"github.com/Sirupsen/logrus"
Expand All @@ -21,24 +22,28 @@ import (
const (

//BANNER for ytd which prints the help info
BANNER = "ytd -id 'videoId' -format mp3 -bitrate 123 -path ~/Downloads/ videoUrl %s\n"
BANNER = "ytd -ids 'videoId,videoId2' -format mp3 -bitrate 123 -path ~/Downloads/ videoUrl %s\n"
//VERSION which prints the ytd version.
VERSION = "v0.1"
)

var (
id string
ids string
version bool
format string
path string
bitrate uint
)

const (
defaultMaxDownloads = 5
)

var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")

func init() {
// parse flags
flag.StringVar(&id, "id", "", "Youtube Video ID")
flag.StringVar(&ids, "ids", "", "Youtube Video IDs. Separated then by using a comma.")
flag.StringVar(&format, "format", "", "File Format(mp3, webm, flv)")
flag.StringVar(&path, "path", ".", "Output Path")
flag.BoolVar(&version, "version", false, "print version and exit")
Expand All @@ -57,7 +62,6 @@ func init() {
}

func main() {
var ID string
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
Expand All @@ -74,17 +78,23 @@ func main() {
if len(os.Args) == 1 {
usageAndExit(BANNER, -1)
}

//Get Video Id
if id == "" {
if ids == "" {
url := os.Args[1]
ID, _ = api.GetVideoId(url)
startProcessing([]string{url})
} else {
ID, _ = api.GetVideoId(id)
startProcessing(strings.Split(ids, ","))
}
}

//Extract Video data and decode
if err := api.APIGetVideoStream(format, ID, path, bitrate); err != nil {
logrus.Errorf("Error decoding Video stream: %v", err)
func startProcessing(urls []string) {
ch := api.DownloadStreams(defaultMaxDownloads, format, path, bitrate, urls)
for err := range ch {
//Extract Video data and decode
if err != nil {
logrus.Errorf("Error decoding Video stream: %v", err)
}
}
}

Expand Down

0 comments on commit 07a8eef

Please sign in to comment.