59 lines
1.1 KiB
Go
Executable File
59 lines
1.1 KiB
Go
Executable File
package youtubedl
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os/exec"
|
|
"path"
|
|
)
|
|
|
|
type Client struct {
|
|
}
|
|
|
|
func New() (*Client, error) {
|
|
return &Client{}, install()
|
|
}
|
|
|
|
func (c *Client) Download(video, local string) error {
|
|
errs := []error{}
|
|
for _, extras := range [][]string{
|
|
[]string{"--add-metadata", "--metadata-from-title", ".*[0-9]/[0-9][0-9] (?P<title>.+)"},
|
|
[]string{},
|
|
} {
|
|
args := append([]string{
|
|
"youtube-dl",
|
|
"-f",
|
|
"22",
|
|
"-o",
|
|
local,
|
|
"--write-sub",
|
|
"--write-auto-sub",
|
|
"--sub-format",
|
|
"srt",
|
|
}, 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("python3", "-m", "vtt_to_srt", path.Dir(local))
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
err = fmt.Errorf("%v: %s", err, out)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|