Skip to content

Commit

Permalink
🎨 improve mp3 conversion, refactor code, update readme doc
Browse files Browse the repository at this point in the history
  • Loading branch information
noelyahan committed Mar 14, 2018
1 parent 389cc7c commit cb3b4a8
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/
pkg
.idea
.idea
releases
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ gonyvido --url https://www.youtube.com/watch?v=3sCGysVB41k
go get github.com/noelyahan/gonyvido
```

## Quick Use

```
gonyvido -path "./download/mp3" -mp3 -url https://www.youtube.com/watch?v=_P7S2lKif-A
```

## Usage

```go
Expand All @@ -37,15 +43,15 @@ func main() {
// if you need only the audio
api.GetHQVideo(url).Download().ToMP3()
/*
gonyvido.GetHQVideo(url) - > get high quality video
gonyvido.GetMQVideo(url) - > get medium quality video
gonyvido.GetLQVideo(url) - > get low quality video
api.GetHQVideo(url) - > get high quality video
api.GetMQVideo(url) - > get medium quality video
api.GetLQVideo(url) - > get low quality video
*/
}
```
## Getting started

If you want to know what it is like to build applications with gonyvido, check [main.go](main/main.go).
If you want to know what it is like to build applications with gonyvido, check [main.go](main.go).

## Related projects

Expand Down
22 changes: 11 additions & 11 deletions domain/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (v *Video) Download() *Video {
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Fatalln("Please check your internet connection", err)
log.Fatalln("Download failed")
}
v.videoLength = int(resp.ContentLength)

Expand Down Expand Up @@ -155,26 +155,26 @@ func (v *Video) ToMP3() {
<-v.done
mp3 := v.getFileName() + ".mp3"
mp4 := v.getFileName() + v.getExt()
removeMP4 := func() {
err := os.Remove(v.savePath + mp4)
if err != nil {
fmt.Errorf("Could not delete: ", mp4)
}
}
//file := v.savePath + mp4
ffmpeg, err := exec.LookPath("ffmpeg")
if err != nil {
removeMP4()
log.Fatal("ffmpeg not found")
}
fmt.Println(`Converting: ` + v.GetTitle() + ` to mp3`)
cmd := exec.Command(ffmpeg, "-y", "-loglevel", "quiet", "-i", v.getSavePath()+mp4, "-b:a", "320K", "-vn", v.getSavePath()+mp3)
cmd := exec.Command(ffmpeg, "-y", "-loglevel", "quiet", "-i", v.getSavePath() + mp4, "-b:a", "320K", "-vn", v.getSavePath() + mp3)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
removeMP4()
log.Fatal("Failed to convert the mp3: ", err)
}
fmt.Println(`Finished: ` + v.GetTitle() + `.mp3`)
removeMP4()
}


func removeFile(file string) {
err := os.Remove(file)
if err != nil {
fmt.Println("Could not delete: ", file, err)
}
}
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const (
func main() {
url := flag.String("url", defaultUrl, "Video download url")
savePath := flag.String("path", defaultSavePath, "Video download path")
quality := flag.String("quality", defaultQuality, "Video download quality")
toMp3 := flag.Bool("mp3", false, "y/n to download mp3")
quality := flag.String("quality", defaultQuality, "Video download quality <high, medium, low>")
toMp3 := flag.Bool("mp3", false, "Enable this flag for mp3 file")

flag.Parse()

Expand Down
2 changes: 1 addition & 1 deletion youtube/youtube.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

const (
YoutubeVideoInfoApi = "http://youtube.com/get_video_info?video_id="
YoutubeWatchUrl = "//www.youtube.com/watch?v"
YoutubeWatchUrl = "https://www.youtube.com/watch?v"
YoutubeShortUrl = "youtu.be"
)

Expand Down

0 comments on commit cb3b4a8

Please sign in to comment.