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

Commit

Permalink
api: Fixed go routine and seg faults in encoded stream map.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyah Check committed Sep 26, 2017
1 parent c807884 commit 27122f1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
7 changes: 3 additions & 4 deletions api/apiconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ import (
//flv if otherwise specified and downloads to system
func ApiConvertVideo(file, id, format string, bitrate uint, decVideo []string) error {
cmd := exec.Command("ffmpeg", "-i", "-", "-ab", fmt.Sprintf("%dk", bitrate), file)
err = os.MkdirAll(filepath.Dir(file), 666)
if err != nil {
/* if err := os.MkdirAll(filepath.Dir(file), 666); err != nil {
return err
}
out, err := os.Create(file)
if err != nil {
return err
}
} */

stdin, err := cmd.StdinPipe()
if err != nil {
Expand Down Expand Up @@ -62,7 +61,7 @@ func ApiDownloadVideo(file string, url string, video *RawVideoData) error {
video.Vlength = float64(resp.ContentLength)

if resp.StatusCode != 200 {
log.Printf("Reading Output: status code : '%v'", resp.StatusCode, err)
log.Printf("Reading Output: status code : '%v'", resp.StatusCode)
return errors.New("non 200 status code received")
}
err = os.MkdirAll(filepath.Dir(file), 666)
Expand Down
30 changes: 22 additions & 8 deletions api/apidata.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ type RawVideoData struct {
VideoId string
VideoInfo string
Vlength float64
dpercent chan int64
}

func (v *RawVideoData) Write(b []byte) (n int, err error) {
n = len(b)
totalbytes , dlevel := 0.0, 0.0
v.Vlength = totalbytes + float64(n)
curPercent := ((totalbytes / v.Vlength) * 100)
if (dlevel <= curPercent) && (dlevel < 100) {
dlevel++
v.dpercent <- int64(dlevel)
}
return
}

//gets the Video ID from youtube url
Expand Down Expand Up @@ -85,7 +98,7 @@ func APIGetVideoStream(format, id, path string, bitrate uint) (err error) {
return err
}
if status[0] == "fail" {
reason, ok := answer["reason"]
reason, ok := output["reason"]
if ok {
err = fmt.Errorf("'fail' response status found in the server, reason: '%s'", reason[0])
} else {
Expand All @@ -101,14 +114,14 @@ func APIGetVideoStream(format, id, path string, bitrate uint) (err error) {
// read the streams map
video.Author = output["author"][0]
video.Title = output["title"][0]
video.URLEncodedFmtStreamMap, ok = output.Get("url_encoded_fmt_stream_map")
StreamMap, ok := output["url_encoded_fmt_stream_map"]
if !ok {
err = errors.New(fmt.Sprint("no stream map found in the server"))
err = fmt.Errorf("Error reading encoded stream map.")
return err
}

// read and decode streams.
streamsList := strings.Split(video.URLEncodedFmtStreamMap[0], ",")
streamsList := strings.Split(string(StreamMap[0]), ",")
var streams []stream
for streamPos, streamRaw := range streamsList {
streamQry, err := url.ParseQuery(streamRaw)
Expand All @@ -133,13 +146,14 @@ func APIGetVideoStream(format, id, path string, bitrate uint) (err error) {
}

video.URLEncodedFmtStreamMap = streams

//create output file name and set path properly.
file := path + "/" + video.Title + video.Author

//Download Video stream to file
vstream := streams[0]
url := vstream["url"] + "&signature" + vstream["sig"]
logrus.Infof("Downloading file to %s", file)

//create output file name and set path properly.
file := path + video.Title + video.Author
if format == "mp3" {
file = file + ".mp3"
err = ApiConvertVideo(file, id, format, bitrate, decodedVideo)
Expand All @@ -150,7 +164,7 @@ func APIGetVideoStream(format, id, path string, bitrate uint) (err error) {
} else { //defaults to flv format for video files.)
file = file + ".flv"
if err := ApiDownloadVideo(file, url, video); err != nil {
logrus.Errorf("Error downloading video: %v", v)
logrus.Errorf("Error downloading video: %v", err)
}
}

Expand Down

0 comments on commit 27122f1

Please sign in to comment.