Add ytdlp binding lib

This commit is contained in:
2024-06-17 15:50:22 +02:00
parent 3d41e4c75e
commit 8286e578f2
4 changed files with 56 additions and 62 deletions

View File

@@ -1,19 +1,27 @@
package main
import (
"bufio"
"context"
"fmt"
"io"
"log"
"os"
"os/exec"
"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]
@@ -58,64 +66,12 @@ func DownloadNative(event PBEvent, status chan error) {
}
func Download(event PBEvent, status chan error) {
cmd := exec.Command("yt-dlp",
"--mark-watched",
"--color", "never",
"-r", "50M",
"--http-chunk-size", "20M",
"-N", "4",
"-o", "C:/Users/Administrator/ytdlpVideos/%(uploader)s/%(title)s.%(ext)s",
"-f", "bestvideo[ext=mp4]+bestaudio[ext=m4a]",
event.Record.Link)
// Get the pipes for stdout and stderr
stdout, err := cmd.StdoutPipe()
_, err := dl.Run(context.TODO(), event.Record.Link)
if err != nil {
log.Printf("Error creating StdoutPipe: %++v for cmd %++v", err, cmd)
status <- err
return
}
stderr, err := cmd.StderrPipe()
if err != nil {
log.Printf("Error creating StderrPipe: %++v for cmd %++v", err, cmd)
status <- err
return
}
err = cmd.Start()
if err != nil {
log.Printf("Error starting command: %++v with error %++v", cmd, err)
status <- err
return
}
stdoutScanner := bufio.NewScanner(stdout)
go func() {
for stdoutScanner.Scan() {
log.Printf("%s: %s", event.Record.Id, stdoutScanner.Text())
}
}()
stderrScanner := bufio.NewScanner(stderr)
go func() {
for stderrScanner.Scan() {
log.Printf("%s: %s", event.Record.Id, stderrScanner.Text())
}
}()
err = cmd.Wait()
if err != nil {
exitError, ok := err.(*exec.ExitError)
if ok {
exitCode := exitError.ExitCode()
log.Printf("Command exited with code %d", exitCode)
} else {
log.Printf("Error running yt-dlp: %++v; %++v", cmd, err)
return
}
} else {
log.Printf("Downloaded %s (%s)", event.Record.Id, event.Record.Link)
SetDownloaded(event)
}
log.Printf("Downloaded %s (%s)", event.Record.Id, event.Record.Link)
SetDownloaded(event)
}