131 lines
3.8 KiB
Go
131 lines
3.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"io"
|
|
"errors"
|
|
"net/http"
|
|
//"strconv"
|
|
|
|
|
|
"github.com/charmbracelet/log"
|
|
"github.com/wader/goutubedl"
|
|
)
|
|
|
|
func checkURL(input string) (error) {
|
|
request, err := http.NewRequest("GET", input, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
client := &http.Client{}
|
|
resp, err := client.Do(request)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
return errors.New(fmt.Sprintf("Video does not exist!"))
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func downloadVideo(url string, path string, group GroupConfig) (err error) {
|
|
goutubedl.Path = "yt-dlp"
|
|
|
|
if err := checkURL(url); err != nil {
|
|
return err
|
|
}
|
|
|
|
log.Debugf("URL \"%s\" exists.", url)
|
|
|
|
video, err := goutubedl.New(context.Background(), url, goutubedl.Options{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if video.Info.IsLive {
|
|
log.Info("Video is a live stream. Skipping.")
|
|
return nil
|
|
}
|
|
|
|
/* VIDEO */
|
|
if group.VideoFormat != "" {
|
|
log.Debugf("Downloading video \"%s\" with format \"%s\"", video.Info.ID, group.VideoFormat)
|
|
|
|
videoDLResult, err := video.Download(context.Background(), group.VideoFormat)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer videoDLResult.Close()
|
|
|
|
file, err := os.OpenFile(MainConfig.TempDir+"/"+video.Info.ID+"-vid", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
|
|
defer file.Close()
|
|
|
|
io.Copy(file, videoDLResult)
|
|
|
|
videoDLResult.Close()
|
|
file.Close()
|
|
}
|
|
|
|
/* AUDIO */
|
|
if group.AudioFormat != "" {
|
|
log.Debugf("Downloading audio \"%s\" with format \"%s\"", video.Info.ID, group.AudioFormat)
|
|
|
|
audioDLResult, err := video.Download(context.Background(), group.AudioFormat)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer audioDLResult.Close()
|
|
|
|
file, err := os.OpenFile(MainConfig.TempDir+"/"+video.Info.ID+"-audio", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
|
|
defer file.Close()
|
|
|
|
io.Copy(file, audioDLResult)
|
|
|
|
audioDLResult.Close()
|
|
file.Close()
|
|
}
|
|
|
|
if group.VideoFormat != "" && group.AudioFormat != "" {
|
|
if err := mergeStreams(MainConfig.TempDir+"/"+video.Info.ID+"-vid",MainConfig.TempDir+"/"+video.Info.ID+"-audio", group.CFormat, path); err != nil {
|
|
log.Errorf("Could not merge files \"%s\" and \"%s\" to \"%s\"", MainConfig.TempDir+"/"+video.Info.ID+"-vid", MainConfig.TempDir+"/"+video.Info.ID+"-audio", path)
|
|
}
|
|
|
|
if err := os.Remove(MainConfig.TempDir+"/"+video.Info.ID+"-vid"); err != nil {
|
|
log.Warnf("Could not remove file \"%s\"", MainConfig.TempDir+"/"+video.Info.ID+"-vid")
|
|
}
|
|
if err := os.Remove(MainConfig.TempDir+"/"+video.Info.ID+"-audio"); err != nil {
|
|
log.Warnf("Could not remove file \"%s\"", MainConfig.TempDir+"/"+video.Info.ID+"-audio")
|
|
}
|
|
} else if group.VideoFormat != "" {
|
|
copyFile(MainConfig.TempDir+"/"+video.Info.ID+"-vid", path)
|
|
|
|
if err := os.Remove(MainConfig.TempDir+"/"+video.Info.ID+"-vid"); err != nil {
|
|
log.Warnf("Could not remove file \"%s\"", MainConfig.TempDir+"/"+video.Info.ID+"-vid")
|
|
}
|
|
} else if group.AudioFormat != "" {
|
|
if err := copyFile(MainConfig.TempDir+"/"+video.Info.ID+"-audio", path); err != nil {
|
|
log.Errorf("Could not copy file \"%s\" to \"%s\"", MainConfig.TempDir+"/"+video.Info.ID+"-audio", path)
|
|
}
|
|
if err := os.Remove(MainConfig.TempDir+"/"+video.Info.ID+"-audio"); err != nil {
|
|
log.Warnf("Could not remove file \"%s\"", MainConfig.TempDir+"/"+video.Info.ID+"-audio")
|
|
}
|
|
}
|
|
|
|
/*
|
|
extension, err := getExtension(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
os.Rename(path, path+"."+extension)
|
|
*/
|
|
|
|
return nil
|
|
}
|