74 lines
1.4 KiB
Go
Executable File
74 lines
1.4 KiB
Go
Executable File
package youtubedl
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"os/exec"
|
|
"path"
|
|
)
|
|
|
|
type Client struct {
|
|
}
|
|
|
|
func New() (*Client, error) {
|
|
return &Client{}, install()
|
|
}
|
|
|
|
func (c *Client) Download(video, local string) error {
|
|
if _, ok := os.LookupEnv("TEST"); ok {
|
|
log.Println("youtube-dl download", video, local)
|
|
return nil
|
|
}
|
|
errs := []error{}
|
|
for _, extras := range [][]string{
|
|
[]string{"--add-metadata", "--metadata-from-title", ".*[0-9]/[0-9][0-9] (?P<title>.+)"},
|
|
[]string{},
|
|
} {
|
|
args := append([]string{
|
|
"/tmp/yt-dlp",
|
|
"-f",
|
|
"22",
|
|
"-o",
|
|
local,
|
|
"--write-sub",
|
|
"--write-auto-sub",
|
|
"--sub-format",
|
|
"srt",
|
|
"--no-check-certificate",
|
|
}, extras...)
|
|
args = append(args, video)
|
|
cmd := exec.Command(args[0], args[1:]...)
|
|
log.Println(cmd.Path, cmd.Args)
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
log.Printf("failed with %v: %s", err, out)
|
|
errs = append(errs, fmt.Errorf("%v: %s", err, out))
|
|
} else {
|
|
log.Printf("passed with %s", out)
|
|
errs = nil
|
|
break
|
|
}
|
|
}
|
|
if len(errs) > 0 {
|
|
err := fmt.Errorf("%v", errs)
|
|
return err
|
|
}
|
|
cmd := exec.Command(
|
|
"/bin/sh",
|
|
"-c",
|
|
fmt.Sprintf(
|
|
"true; python3 -m vtt_to_srt %q || python3 /usr/bin/vtt_to_srt.py %q || vtt_to_srt %q",
|
|
path.Dir(local),
|
|
path.Dir(local),
|
|
path.Dir(local),
|
|
),
|
|
)
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
err = fmt.Errorf("%v: %s", err, out)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|