2022-11-18 12:29:39 +00:00
|
|
|
package ytdl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
2022-11-18 21:05:41 +00:00
|
|
|
"path/filepath"
|
2022-11-18 12:29:39 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
)
|
|
|
|
|
|
|
|
type YTdl struct {
|
|
|
|
Title string
|
|
|
|
Url string
|
|
|
|
Channel string
|
|
|
|
Len time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewYTdl(uri string) (*YTdl, error) {
|
|
|
|
ytdl_js, err := exec.Command(
|
|
|
|
"./bin/yt-dlp_linux",
|
|
|
|
uri,
|
|
|
|
"--cookies", "./cookies.txt",
|
|
|
|
"--no-call-home",
|
|
|
|
"--no-cache-dir",
|
|
|
|
"--ignore-errors",
|
|
|
|
"--newline",
|
|
|
|
"--restrict-filenames",
|
|
|
|
"-f", "140",
|
|
|
|
"-j",
|
|
|
|
).Output()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !gjson.ValidBytes(ytdl_js) {
|
|
|
|
return nil, errors.New("invalid json")
|
|
|
|
}
|
|
|
|
|
|
|
|
results := gjson.GetManyBytes(ytdl_js, "title", "url", "duration", "channel")
|
|
|
|
title := results[0].String()
|
|
|
|
geturl := results[1].String()
|
|
|
|
duration, err := time.ParseDuration(fmt.Sprintf("%ds", results[2].Int()))
|
|
|
|
channel := results[3].String()
|
|
|
|
|
|
|
|
return &YTdl{title, geturl, channel, duration}, nil
|
|
|
|
}
|
2022-11-18 21:05:41 +00:00
|
|
|
|
|
|
|
func DownloadAmbiance(uri string, name string) error {
|
|
|
|
err := exec.Command(
|
|
|
|
"./bin/yt-dlp_linux",
|
|
|
|
uri,
|
|
|
|
"-x",
|
|
|
|
"--audio-format", "mp3",
|
|
|
|
"--postprocessor-args", "-ar 48000 -ac 2",
|
|
|
|
"--cookies", "./cookies.txt",
|
|
|
|
"--no-call-home",
|
|
|
|
"--no-cache-dir",
|
|
|
|
"--restrict-filenames",
|
|
|
|
"-f", "140",
|
|
|
|
"-o", filepath.Join("./ambiance/", name+".mp3"),
|
|
|
|
).Run()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|