78 lines
1.7 KiB
Go
78 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/kkdai/youtube/v2"
|
|
"github.com/lrstanley/go-ytdlp"
|
|
)
|
|
|
|
const OUTPUT_DIR = "C:/Users/Administrator/ytdlpVideos"
|
|
|
|
var dl = ytdlp.New().
|
|
// FormatSort("bestvideo[ext=mp4]+bestaudio[ext=m4a]").
|
|
FormatSort("res,ext:mp4:m4a").
|
|
Output("C:/Users/Administrator/ytdlpVideos/%(uploader)s/%(title)s.%(ext)s").
|
|
LimitRate("50M").
|
|
HTTPChunkSize("20M").
|
|
MarkWatched()
|
|
|
|
func DownloadNative(event PBEvent, status chan error) {
|
|
log.Printf("%s: Downloading %s", event.Record.Id, event.Record.Link)
|
|
videoId := strings.Split(event.Record.Link, "?v=")[1]
|
|
client := youtube.Client{}
|
|
|
|
video, err := client.GetVideo(videoId)
|
|
if err != nil {
|
|
status <- err
|
|
return
|
|
}
|
|
|
|
formats := video.Formats.WithAudioChannels()
|
|
stream, _, err := client.GetStream(video, &formats[0])
|
|
if err != nil {
|
|
status <- err
|
|
return
|
|
}
|
|
defer stream.Close()
|
|
|
|
log.Printf("%s: Video title: %s", event.Record.Id, video.Title)
|
|
outFolder := fmt.Sprintf("%s/%s", OUTPUT_DIR, video.Author)
|
|
out := fmt.Sprintf("%s/%s.mp4", outFolder, video.Title)
|
|
err = os.MkdirAll(outFolder, os.ModePerm)
|
|
if err != nil {
|
|
status <- err
|
|
return
|
|
}
|
|
log.Printf("%s: Video save location: %s", event.Record.Id, out)
|
|
|
|
file, err := os.Create(out)
|
|
if err != nil {
|
|
status <- err
|
|
return
|
|
}
|
|
defer file.Close()
|
|
|
|
_, err = io.Copy(file, stream)
|
|
if err != nil {
|
|
status <- err
|
|
return
|
|
}
|
|
}
|
|
|
|
func Download(event PBEvent, status chan error) {
|
|
_, err := dl.Run(context.TODO(), event.Record.Link)
|
|
if err != nil {
|
|
status <- err
|
|
return
|
|
}
|
|
|
|
log.Printf("Downloaded %s (%s)", event.Record.Id, event.Record.Link)
|
|
SetDownloaded(event)
|
|
}
|