aboutsummaryrefslogtreecommitdiff
path: root/downloader/downloader.go
blob: 793cd2292f4ff6b31876b8ea39e0206d93715da9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package downloader

import (
	"net/url"
	"os/exec"
	"strconv"
	"time"

	"io/ioutil"
	"os"

	"encoding/json"

	"github.com/mammothbane/thulani-go/wav"
	"github.com/op/go-logging"
)

var log = logging.MustGetLogger("downloader")

// responsible for decoding from youtube
type videoInfo struct {
	Title       string        `json:"fulltitle"`
	UrlStr      string        `json:"url"`
	DurationSec int           `json:"duration"`
	Url         *url.URL      `json:"-"`
	Duration    time.Duration `json:"-"`
}

func info(inUrl string) (*videoInfo, error) {
	dl := exec.Command("youtube-dl", "-f", "bestaudio", "-x", "-j", inUrl)

	outpipe, err := dl.StdoutPipe()
	if err != nil {
		return nil, err
	}

	errpipe, err := dl.StderrPipe()
	if err != nil {
		return nil, err
	}

	err = dl.Start()
	if err != nil {
		log.Errorf("starting youtube-dl failed")
		return nil, err
	}

	o, ierr := ioutil.ReadAll(outpipe)
	if ierr != nil {
		log.Errorf("unable to read from output pipe")
		return nil, err
	}

	e, ierr := ioutil.ReadAll(errpipe)
	if ierr != nil {
		log.Errorf("unable to read from error pipe")
		return nil, err
	}

	if err := dl.Wait(); err != nil {
		log.Errorf("error:\n%v", string(e))
		return nil, err
	}

	v := videoInfo{}
	if err := json.Unmarshal(o, &v); err != nil {
		return nil, err
	}

	v.Duration = time.Duration(v.DurationSec) * time.Second
	v.Url, err = url.Parse(v.UrlStr)

	//tgt, err := url.Parse(string(o))
	//out := tgt.Scheme + "://" + tgt.Host + tgt.Path + "?" + tgt.Query().Encode()
	return &v, err
}

func Download(inUrl string, startTime time.Duration, duration time.Duration) (<-chan []byte, error) {
	vInfo, err := info(inUrl)
	if err != nil {
		return nil, err
	}

	startSecond := int(startTime.Seconds())
	dur := int(duration.Seconds())

	args := []string{
		"-ss", strconv.Itoa(startSecond),
		"-i", vInfo.Url.String(),
		"-c:a", "pcm_s16le",
		"-f", "wav",
		"-ar", "48000",
		"-ac", "2",
		"-vn", "-y",
	}

	if dur > 0 {
		args = append(args, "-t", strconv.Itoa(dur))
	}

	file, err := ioutil.TempFile("", "thulani_")
	if err != nil {
		return nil, err
	}

	clearTemp := func() {
		if err := file.Close(); err != nil {
			log.Errorf("error closing temp file: %q", err)
		}

		if err := os.Remove(file.Name()); err != nil {
			log.Errorf("unable to remove temp file: %q", err)
		}
	}

	args = append(args, file.Name())

	dl := exec.Command(`ffmpeg`, args...)
	b, err := dl.CombinedOutput()
	if err != nil {
		clearTemp()
		log.Errorf("ffmpeg failed: \n%v", string(b))
		return nil, err
	}

	ch := make(chan []byte, 1024*32)
	done, err := wav.Load(file.Name(), ch)
	if err != nil {
		clearTemp()
		return nil, err
	}

	go func() {
		<-done
		clearTemp()
	}()

	return ch, err
}