diff --git a/api/concurrency.go b/api/concurrency.go new file mode 100644 index 0000000..ed873c6 --- /dev/null +++ b/api/concurrency.go @@ -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 +} diff --git a/ytd.go b/ytd.go index 938a315..616b128 100644 --- a/ytd.go +++ b/ytd.go @@ -13,6 +13,7 @@ import ( "os" "runtime" "runtime/pprof" + "strings" "github.com/Ch3ck/youtube-dl/api" "github.com/Sirupsen/logrus" @@ -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") @@ -57,7 +62,6 @@ func init() { } func main() { - var ID string if *cpuprofile != "" { f, err := os.Create(*cpuprofile) if err != nil { @@ -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) + } } }