Skip to content
This repository has been archived by the owner on Nov 21, 2022. It is now read-only.

Commit

Permalink
api: Fixed compilation issues in api
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyah Check committed Sep 16, 2017
1 parent 1bf99b1 commit 476df33
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
25 changes: 16 additions & 9 deletions api/apiconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
package api

import (
"bytes"
"encoding/gob"
"errors"
"fmt"
"io"
"net/http"
Expand All @@ -19,7 +22,7 @@ import (

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

logrus.Infof("Converting video to %q format", filepath.Ext(file))
if filepath.Ext(file) == ".mp3" {
// NOTE: To modify to use Go ffmpeg bindings or cgo
/* NOTE: To modify to use Go ffmpeg bindings or cgo */

buf := &bytes.Buffer{}
gob.NewEncoder(buf).Encode(decVideo)
_, err = exec.LookPath("ffmpeg")
if err != nil {
logrus.Errorf("ffmpeg not found on system")
return errors.New("ffmpeg not found on system")
}

cmd.Start()
logrus.Infof("Downloading mp3 file to disk %s", file)
cmd.Write(decVideo) //download file.
stdin.Write(buf.Bytes()) //download file.

} else {
cmd, err = os.Create(file)
out, err := os.Create(file)
if err != nil {
logrus.Error("Unable to download video file.", err)
logrus.Errorf("Unable to download video file.", err)
return err
}
err = apiDownloadVideo(id, cmd)
err = apiDownloadVideo(id, out)
return err
}

return nil
}

//Downloads decoded video stream.
func apiDownloadVideo(videoUrl, cmd io.Writer) error {
func apiDownloadVideo(url string, out io.Writer) error {
logrus.Infof("Downloading file stream")

resp, err := http.Get(url)
resp, err := http.Get(videoExtractor + url)
if err != nil {
return fmt.Errorf("requesting stream: %s", err)
}
Expand Down
33 changes: 18 additions & 15 deletions api/apidata.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package api

import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
Expand All @@ -26,16 +27,16 @@ const (

//Youtube Downloader Data file.
type RawVideoData struct {
Title string `json:"title"`
Author string `json:"author`
Status string `json:"status"`
URLEncodedFmtStreamMap map[string][]string `json:"url_encoded_fmt_stream_map"`
Title string `json:"title"`
Author string `json:"author`
Status string `json:"status"`
URLEncodedFmtStreamMap string `json:"url_encoded_fmt_stream_map"`
}

//gets the Video ID from youtube url
func GetVideoId(url string) (string, error) {
if !strings.Contains(url, "youtube.com") {
return nil, errors.New("Invalid Youtube link")
return "", errors.New("Invalid Youtube link")
}
s := strings.Split(url, "?v=")
s = strings.Split(s[1], "&")
Expand All @@ -47,7 +48,7 @@ func GetVideoId(url string) (string, error) {
}

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

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

output, er := url.ParseQuery(out)
output, er := url.ParseQuery(string(out))
if e != nil {
return nil, errors.New("Error Parsing video byte stream: %v", e)
logrus.Errorf("Error parsing video byte stream: %v", e)
return nil, e
}

//Process Video stream
video.URLEncodedFmtStreamMap = output["url_encoded_fmt_stream_map"]
video.Author = output["author"]
video.Title = output["title"]
video.Status = output["status"]
video.URLEncodedFmtStreamMap = output.Get("url_encoded_fmt_stream_map")
video.Author = output.Get("author")
video.Title = output.Get("title")
video.Status = output.Get("status")

//Decode Video
outputStreams := strings.Split(video.URLEncodedFmtStreamMap[0], ",")
for cur, raw_data := range outputStream {
outputStreams := strings.Split(video.URLEncodedFmtStreamMap, ",")
for cur, raw_data := range outputStreams {
//decoding raw data stream
dec_data, err := url.ParseQuery(raw_data)
if err != nil {
Expand All @@ -96,7 +98,8 @@ func APIGetVideoStream(id string, video RawVideoData) (videoData []byte, err err
"format": dec_data["format"][0],
}

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

Expand Down

0 comments on commit 476df33

Please sign in to comment.