Skip to content
This repository has been archived by the owner on Oct 6, 2024. It is now read-only.

Basic pipelines architecture + changed API #19

Merged
merged 1 commit into from
Oct 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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