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

Commit

Permalink
api: Formatting and go routine errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyah Check committed Sep 18, 2017
1 parent 84803c5 commit 822dd39
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 43 deletions.
20 changes: 5 additions & 15 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import (
)

var tables = []struct {
url string // input
id string // expected result
url, id string // input
}{
{"https://www.youtube.com/watch?v=HpNluHOAJFA&list=RDHpNluHOAJFA", "HpNluHOAJFA"},
{"https://www.youtube.com/watch?v=jOWsu8ePrbE&list=RDHpNluHOAJFA&index=8", "jOWsu8ePrbE"},
Expand All @@ -29,23 +28,15 @@ func TestApi(t *testing.T) {

path := "~/Downloads/"
for i, table := range tables {
var rawVideo *RawVideoData
ID, _ := GetVideoId(table.url)
if ID != table.id {
t.Errorf("GetVideoId(%d): expected %q, actual %q", i, table.id, ID)
}

if ID != "" {
video, err := APIGetVideoStream(ID, rawVideo)
if err != nil {
if err := APIGetVideoStream("mp3", ID, path, 123); err != nil {
t.Errorf("APIGetVideoStream(%d): expected %v, actual %v", i, nil, err)
}

file := path + table.id + ".mp3"
err = APIConvertVideo(file, 123, ID, video)
if err != nil {
t.Errorf("APIConvertVideo(%d): expected %v, actual %v", i, nil, err)
}
}
}
}
Expand All @@ -58,15 +49,14 @@ func BenchmarkGetVideoId(b *testing.B) {

func BenchmarkApiGetVideoStream(b *testing.B) {
for n := 0; n < b.N; n++ {
var rawVideo *RawVideoData
vid, _ = APIGetVideoStream(tables[0].id, rawVideo)
APIGetVideoStream("mp3", tables[0].id, "~/Downloads", 123)
}
}

func BenchmarkApiConvertVideo(b *testing.B) {
/*func BenchmarkApiConvertVideo(b *testing.B) {
path := "~/Downloads/"
for n := 0; n < b.N; n++ {
file := path + tables[0].id + ".mp3"
APIConvertVideo(file, 123, tables[0].id, vid)
}
}
}*/
2 changes: 1 addition & 1 deletion api/apiconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,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 uint, id string, decVideo []string) error {
func APIConvertVideo(file, id, format string, bitrate uint, decVideo []string) error {
cmd := exec.Command("ffmpeg", "-i", "-", "-ab", fmt.Sprintf("%dk", bitrate), file)
stdin, err := cmd.StdinPipe()
if err != nil {
Expand Down
25 changes: 20 additions & 5 deletions api/apidata.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ func GetVideoId(url string) (string, error) {
}

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

video = new(RawVideoData) //raw video data
var decodedVideo []string //decoded video data
video := new(RawVideoData) //raw video data
var decodedVideo []string //decoded video data

//Get Video Data stream
videoUrl := videoExtractor + id
Expand All @@ -69,7 +69,7 @@ func APIGetVideoStream(id string, video *RawVideoData) (videoData []string, err
output, er := url.ParseQuery(string(out))
if e != nil {
logrus.Errorf("Error parsing video byte stream: %v", e)
return nil, e
return nil
}

//Process Video stream
Expand Down Expand Up @@ -103,5 +103,20 @@ func APIGetVideoStream(id string, video *RawVideoData) (videoData []string, err
//logrus.Infof("\nDecoded %d bytes of %q, in %q format", len(decodedVideo), dec_data.Get("quality"), dec_data.Get("format"))
}

return decodedVideo, nil
//Convert and Download video data
//create output file name and set path properly.
file := path + video.Title + video.Author
if format == "mp3" {
file = file + ".mp3"

} else { //defaults to flv format for video files.)
file = file + ".flv"
}

err = APIConvertVideo(file, id, format, bitrate, decodedVideo)
if err != nil {
logrus.Errorf("Error downloading video: %v", err)
}

return nil
}
38 changes: 16 additions & 22 deletions ytd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ package main
import (
"flag"
"fmt"
_ "net/http/pprof"
"os"
"runtime"
"runtime/pprof"

"github.com/Ch3ck/ytd/api"
"github.com/Sirupsen/logrus"
Expand All @@ -18,7 +21,7 @@ import (
const (

//BANNER for ytd which prints the help info
BANNER = "ytd -id 'videoId' -format mp3 -bitrate 123 -path ~/Downloads/ videoUrl%s\n"
BANNER = "ytd -id 'videoId' -format mp3 -bitrate 123 -path ~/Downloads/ videoUrl %s\n"
//VERSION which prints the ytd version.
VERSION = "v0.1"
)
Expand All @@ -29,9 +32,10 @@ var (
format string
path string
bitrate uint
file string
)

var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")

func init() {
// parse flags
flag.StringVar(&id, "id", "", "Youtube Video ID")
Expand All @@ -46,7 +50,6 @@ func init() {
}

flag.Parse()

if version {
logrus.Infof("%s", VERSION)
os.Exit(0)
Expand All @@ -55,11 +58,18 @@ func init() {

func main() {
var ID string
var rawVideo *api.RawVideoData
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
logrus.Fatalf("%v", err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
runtime.SetBlockProfileRate(20)
if len(os.Args) == 1 {
usageAndExit(BANNER, -1)
}

//Get Video Id
if id == "" {
url := os.Args[1]
Expand All @@ -69,25 +79,9 @@ func main() {
}

//Extract Video data and decode
video, err := api.APIGetVideoStream(ID, rawVideo)
if err != nil {
if err := api.APIGetVideoStream(format, ID, path, bitrate); err != nil {
logrus.Errorf("Error decoding Video stream: %v", err)
}

//Convert and Download video data
//create output file name and set path properly.
file = path + rawVideo.Title + rawVideo.Author
if format == "mp3" {
file = file + ".mp3"

} else { //defaults to flv format for video files.)
file = file + ".flv"
}

err = api.APIConvertVideo(file, bitrate, ID, video)
if err != nil {
logrus.Errorf("Error downloading video: %v", err)
}
}

func usageAndExit(message string, exitCode int) {
Expand Down

0 comments on commit 822dd39

Please sign in to comment.