2024-12-31 17:53:17 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"io"
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
2024-12-31 18:59:03 -05:00
|
|
|
//"strconv"
|
2025-01-03 04:01:48 -05:00
|
|
|
"path/filepath"
|
2024-12-31 17:53:17 -05:00
|
|
|
|
|
|
|
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2024-12-31 18:59:03 -05:00
|
|
|
func downloadVideo(url string, path string, group GroupConfig) (err error) {
|
2024-12-31 17:53:17 -05:00
|
|
|
goutubedl.Path = "yt-dlp"
|
|
|
|
|
|
|
|
if err := checkURL(url); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("URL \"%s\" exists.", url)
|
|
|
|
|
2025-01-03 20:30:40 -05:00
|
|
|
video, err := goutubedl.New(context.Background(), url, goutubedl.Options{
|
|
|
|
MergeOutputFormat: "",
|
|
|
|
DownloadSubtitles: group.DownloadSubtitles,
|
|
|
|
DownloadThumbnail: group.DownloadThumbnails,
|
|
|
|
})
|
2024-12-31 17:53:17 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2025-01-03 04:01:48 -05:00
|
|
|
tempPath := filepath.Join(MainConfig.TempDir, sanitizeFilename(replaceDelimiters(video.Info.ID)))
|
|
|
|
tempPath = filepath.Clean(tempPath)
|
|
|
|
|
|
|
|
log.Debugf("Using temp path \"%s\".", tempPath)
|
2024-12-31 20:04:03 -05:00
|
|
|
|
2024-12-31 18:59:03 -05:00
|
|
|
/* VIDEO */
|
2024-12-31 19:48:18 -05:00
|
|
|
if group.VideoFormat != "" {
|
2024-12-31 18:59:03 -05:00
|
|
|
log.Debugf("Downloading video \"%s\" with format \"%s\"", video.Info.ID, group.VideoFormat)
|
2024-12-31 17:53:17 -05:00
|
|
|
|
2024-12-31 18:59:03 -05:00
|
|
|
videoDLResult, err := video.Download(context.Background(), group.VideoFormat)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-12-31 17:53:17 -05:00
|
|
|
|
2024-12-31 18:59:03 -05:00
|
|
|
defer videoDLResult.Close()
|
2024-12-31 17:53:17 -05:00
|
|
|
|
2024-12-31 20:04:03 -05:00
|
|
|
file, err := os.OpenFile(tempPath+"-vid", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
|
2025-01-03 04:02:27 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-12-31 18:59:03 -05:00
|
|
|
defer file.Close()
|
2024-12-31 17:53:17 -05:00
|
|
|
|
2024-12-31 18:59:03 -05:00
|
|
|
io.Copy(file, videoDLResult)
|
2024-12-31 17:53:17 -05:00
|
|
|
|
2024-12-31 18:59:03 -05:00
|
|
|
videoDLResult.Close()
|
|
|
|
file.Close()
|
|
|
|
}
|
2024-12-31 17:53:17 -05:00
|
|
|
|
2024-12-31 18:59:03 -05:00
|
|
|
/* AUDIO */
|
2025-01-03 20:29:08 -05:00
|
|
|
if group.ExtractFormat != "" {
|
|
|
|
log.Debugf("Extracting audio from \"%s\" with format \"%s\"", video.Info.ID, group.ExtractFormat)
|
|
|
|
|
|
|
|
audioDLResult, err := video.Download(context.Background(), group.ExtractFormat)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer audioDLResult.Close()
|
|
|
|
|
|
|
|
file, err := os.OpenFile(tempPath+"-extract", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
io.Copy(file, audioDLResult)
|
|
|
|
|
|
|
|
audioDLResult.Close()
|
|
|
|
file.Close()
|
|
|
|
} else if group.AudioFormat != "" {
|
2024-12-31 18:59:03 -05:00
|
|
|
log.Debugf("Downloading audio \"%s\" with format \"%s\"", video.Info.ID, group.AudioFormat)
|
2024-12-31 17:53:17 -05:00
|
|
|
|
2024-12-31 18:59:03 -05:00
|
|
|
audioDLResult, err := video.Download(context.Background(), group.AudioFormat)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-12-31 17:53:17 -05:00
|
|
|
|
2024-12-31 18:59:03 -05:00
|
|
|
defer audioDLResult.Close()
|
2024-12-31 17:53:17 -05:00
|
|
|
|
2024-12-31 20:04:03 -05:00
|
|
|
file, err := os.OpenFile(tempPath+"-audio", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
|
2025-01-03 04:02:27 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-12-31 18:59:03 -05:00
|
|
|
defer file.Close()
|
2024-12-31 17:53:17 -05:00
|
|
|
|
2024-12-31 18:59:03 -05:00
|
|
|
io.Copy(file, audioDLResult)
|
2024-12-31 17:53:17 -05:00
|
|
|
|
2024-12-31 18:59:03 -05:00
|
|
|
audioDLResult.Close()
|
|
|
|
file.Close()
|
|
|
|
}
|
2024-12-31 17:53:17 -05:00
|
|
|
|
2024-12-31 19:48:18 -05:00
|
|
|
if group.VideoFormat != "" && group.AudioFormat != "" {
|
2024-12-31 20:04:03 -05:00
|
|
|
if err := mergeStreams(tempPath+"-vid",tempPath+"-audio", group.CFormat, path); err != nil {
|
|
|
|
log.Errorf("Could not merge files \"%s\" and \"%s\" to \"%s\"", tempPath+"-vid", tempPath+"-audio", path)
|
2024-12-31 19:48:18 -05:00
|
|
|
}
|
2024-12-31 17:53:17 -05:00
|
|
|
|
2024-12-31 20:04:03 -05:00
|
|
|
if err := os.Remove(tempPath+"-vid"); err != nil {
|
|
|
|
log.Warnf("Could not remove file \"%s\"", tempPath+"-vid")
|
2024-12-31 19:48:18 -05:00
|
|
|
}
|
2024-12-31 20:04:03 -05:00
|
|
|
if err := os.Remove(tempPath+"-audio"); err != nil {
|
|
|
|
log.Warnf("Could not remove file \"%s\"", tempPath+"-audio")
|
2024-12-31 19:48:18 -05:00
|
|
|
}
|
2025-01-03 20:29:08 -05:00
|
|
|
} else if group.ExtractFormat != "" {
|
|
|
|
if err := extractAudio(tempPath+"-extract", group.CFormat, path); err != nil {
|
|
|
|
log.Errorf("Could not extract audio from \"%s\" to \"%s\"", tempPath+"-extract", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Remove(tempPath+"-extract"); err != nil {
|
|
|
|
log.Warnf("Could not remove file \"%s\"", tempPath+"-extract")
|
|
|
|
}
|
2024-12-31 19:48:18 -05:00
|
|
|
} else if group.VideoFormat != "" {
|
2024-12-31 20:04:03 -05:00
|
|
|
copyFile(tempPath+"-vid", path)
|
2024-12-31 19:48:18 -05:00
|
|
|
|
2024-12-31 20:04:03 -05:00
|
|
|
if err := os.Remove(tempPath+"-vid"); err != nil {
|
|
|
|
log.Warnf("Could not remove file \"%s\"", tempPath+"-vid")
|
2024-12-31 19:48:18 -05:00
|
|
|
}
|
|
|
|
} else if group.AudioFormat != "" {
|
2024-12-31 20:04:03 -05:00
|
|
|
if err := copyFile(tempPath+"-audio", path); err != nil {
|
|
|
|
log.Errorf("Could not copy file \"%s\" to \"%s\"", tempPath+"-audio", path)
|
2024-12-31 19:48:18 -05:00
|
|
|
}
|
2024-12-31 20:04:03 -05:00
|
|
|
if err := os.Remove(tempPath+"-audio"); err != nil {
|
|
|
|
log.Warnf("Could not remove file \"%s\"", tempPath+"-audio")
|
2024-12-31 19:48:18 -05:00
|
|
|
}
|
2024-12-31 18:59:03 -05:00
|
|
|
}
|
2024-12-31 17:53:17 -05:00
|
|
|
|
2024-12-31 18:59:03 -05:00
|
|
|
/*
|
|
|
|
extension, err := getExtension(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-12-31 17:53:17 -05:00
|
|
|
}
|
|
|
|
|
2024-12-31 18:59:03 -05:00
|
|
|
os.Rename(path, path+"."+extension)
|
|
|
|
*/
|
|
|
|
|
2024-12-31 17:53:17 -05:00
|
|
|
return nil
|
|
|
|
}
|