aboutsummaryrefslogtreecommitdiff
path: root/downloader/util.go
blob: f744fad03c14a23c6165622d84e2cf0ed20e2b0e (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
package downloader

import (
	"encoding/json"
	"io/ioutil"
	"net/url"
	"os/exec"
	"time"

	"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
}

type wavBundle struct {
	wav     *wav.Wav
	cleanup func()
}