YTSFlix_Go/youtube/youtube.go

40 lines
783 B
Go

package youtube
import (
"github.com/rylio/ytdl"
"errors"
"os"
)
func Get(url string) (string, error) {
v, err := ytdl.GetVideoInfo(url)
if err != nil {
return "", err
}
if len(v.Formats.Best(ytdl.FormatResolutionKey)) < 1 {
return "", errors.New("not found")
}
u, err := v.GetDownloadURL(v.Formats.Best(ytdl.FormatResolutionKey)[0])
if err != nil {
return "", err
}
return u.String(), nil
}
func Download(url string, file string) error {
v, err := ytdl.GetVideoInfo(url)
if err != nil {
return err
}
if len(v.Formats.Best(ytdl.FormatResolutionKey)) < 1 {
return errors.New("not found")
}
format := v.Formats.Best(ytdl.FormatResolutionKey)[0]
f, err := os.Create(file)
if err != nil {
return err
}
defer f.Close()
return v.Download(format, f)
}