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

Commit 476df33

Browse files
author
Nyah Check
committed
api: Fixed compilation issues in api
1 parent 1bf99b1 commit 476df33

File tree

2 files changed

+34
-24
lines changed

2 files changed

+34
-24
lines changed

api/apiconv.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
package api
88

99
import (
10+
"bytes"
11+
"encoding/gob"
12+
"errors"
1013
"fmt"
1114
"io"
1215
"net/http"
@@ -19,7 +22,7 @@ import (
1922

2023
//Converts Decoded Video file to mp3 by default with 123 bitrate or to
2124
//flv if otherwise specified and downloads to system
22-
func APIConvertVideo(file string, bitrate int, id string, decVideo []byte) error {
25+
func APIConvertVideo(file string, bitrate int, id string, decVideo []string) error {
2326
cmd := exec.Command("ffmpeg", "-i", "-", "-ab", fmt.Sprintf("%dk", bitrate), file)
2427
stdin, err := cmd.StdinPipe()
2528
if err != nil {
@@ -31,33 +34,37 @@ func APIConvertVideo(file string, bitrate int, id string, decVideo []byte) error
3134

3235
logrus.Infof("Converting video to %q format", filepath.Ext(file))
3336
if filepath.Ext(file) == ".mp3" {
34-
// NOTE: To modify to use Go ffmpeg bindings or cgo
37+
/* NOTE: To modify to use Go ffmpeg bindings or cgo */
38+
39+
buf := &bytes.Buffer{}
40+
gob.NewEncoder(buf).Encode(decVideo)
3541
_, err = exec.LookPath("ffmpeg")
3642
if err != nil {
37-
logrus.Errorf("ffmpeg not found on system")
43+
return errors.New("ffmpeg not found on system")
3844
}
3945

4046
cmd.Start()
4147
logrus.Infof("Downloading mp3 file to disk %s", file)
42-
cmd.Write(decVideo) //download file.
48+
stdin.Write(buf.Bytes()) //download file.
4349

4450
} else {
45-
cmd, err = os.Create(file)
51+
out, err := os.Create(file)
4652
if err != nil {
47-
logrus.Error("Unable to download video file.", err)
53+
logrus.Errorf("Unable to download video file.", err)
54+
return err
4855
}
49-
err = apiDownloadVideo(id, cmd)
56+
err = apiDownloadVideo(id, out)
5057
return err
5158
}
5259

5360
return nil
5461
}
5562

5663
//Downloads decoded video stream.
57-
func apiDownloadVideo(videoUrl, cmd io.Writer) error {
64+
func apiDownloadVideo(url string, out io.Writer) error {
5865
logrus.Infof("Downloading file stream")
5966

60-
resp, err := http.Get(url)
67+
resp, err := http.Get(videoExtractor + url)
6168
if err != nil {
6269
return fmt.Errorf("requesting stream: %s", err)
6370
}

api/apidata.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package api
88

99
import (
10+
"encoding/json"
1011
"errors"
1112
"io/ioutil"
1213
"net/http"
@@ -26,16 +27,16 @@ const (
2627

2728
//Youtube Downloader Data file.
2829
type RawVideoData struct {
29-
Title string `json:"title"`
30-
Author string `json:"author`
31-
Status string `json:"status"`
32-
URLEncodedFmtStreamMap map[string][]string `json:"url_encoded_fmt_stream_map"`
30+
Title string `json:"title"`
31+
Author string `json:"author`
32+
Status string `json:"status"`
33+
URLEncodedFmtStreamMap string `json:"url_encoded_fmt_stream_map"`
3334
}
3435

3536
//gets the Video ID from youtube url
3637
func GetVideoId(url string) (string, error) {
3738
if !strings.Contains(url, "youtube.com") {
38-
return nil, errors.New("Invalid Youtube link")
39+
return "", errors.New("Invalid Youtube link")
3940
}
4041
s := strings.Split(url, "?v=")
4142
s = strings.Split(s[1], "&")
@@ -47,7 +48,7 @@ func GetVideoId(url string) (string, error) {
4748
}
4849

4950
//Gets Video Info, Decode Video Info from a Video ID.
50-
func APIGetVideoStream(id string, video RawVideoData) (videoData []byte, err error) {
51+
func APIGetVideoStream(id string, video *RawVideoData) (videoData []string, err error) {
5152

5253
video = new(RawVideoData) //raw video data
5354
var decodedVideo []string //decoded video data
@@ -65,20 +66,21 @@ func APIGetVideoStream(id string, video RawVideoData) (videoData []byte, err err
6566
logrus.Errorf("Error reading video data: %v", e)
6667
}
6768

68-
output, er := url.ParseQuery(out)
69+
output, er := url.ParseQuery(string(out))
6970
if e != nil {
70-
return nil, errors.New("Error Parsing video byte stream: %v", e)
71+
logrus.Errorf("Error parsing video byte stream: %v", e)
72+
return nil, e
7173
}
7274

7375
//Process Video stream
74-
video.URLEncodedFmtStreamMap = output["url_encoded_fmt_stream_map"]
75-
video.Author = output["author"]
76-
video.Title = output["title"]
77-
video.Status = output["status"]
76+
video.URLEncodedFmtStreamMap = output.Get("url_encoded_fmt_stream_map")
77+
video.Author = output.Get("author")
78+
video.Title = output.Get("title")
79+
video.Status = output.Get("status")
7880

7981
//Decode Video
80-
outputStreams := strings.Split(video.URLEncodedFmtStreamMap[0], ",")
81-
for cur, raw_data := range outputStream {
82+
outputStreams := strings.Split(video.URLEncodedFmtStreamMap, ",")
83+
for cur, raw_data := range outputStreams {
8284
//decoding raw data stream
8385
dec_data, err := url.ParseQuery(raw_data)
8486
if err != nil {
@@ -96,7 +98,8 @@ func APIGetVideoStream(id string, video RawVideoData) (videoData []byte, err err
9698
"format": dec_data["format"][0],
9799
}
98100

99-
decodedVideo = append(decodedVideo, data)
101+
str, _ := json.Marshal(data)
102+
decodedVideo = append(decodedVideo, string(str))
100103
logrus.Infof("\nDecoded %d bytes of %q, in %q format", len(decodedVideo), dec_data["quality"][0], dec_data["format"][0])
101104
}
102105

0 commit comments

Comments
 (0)