Download subtitles from drmd file

This commit is contained in:
Joren 2024-09-06 11:29:19 +02:00
parent 6b192aadc9
commit f014b06f88
Signed by: Joren
GPG Key ID: 280E33DFBC0F1B55
4 changed files with 96 additions and 2 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
config.toml

8
go.mod
View File

@ -3,3 +3,11 @@ module DRMDTool
go 1.23.0
require github.com/BurntSushi/toml v1.4.0
require (
github.com/asticode/go-astikit v0.20.0 // indirect
github.com/asticode/go-astisub v0.26.2 // indirect
github.com/asticode/go-astits v1.8.0 // indirect
golang.org/x/net v0.0.0-20200904194848-62affa334b73 // indirect
golang.org/x/text v0.3.2 // indirect
)

25
go.sum
View File

@ -1,2 +1,27 @@
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/asticode/go-astikit v0.20.0 h1:+7N+J4E4lWx2QOkRdOf6DafWJMv6O4RRfgClwQokrH8=
github.com/asticode/go-astikit v0.20.0/go.mod h1:h4ly7idim1tNhaVkdVBeXQZEE3L0xblP7fCWbgwipF0=
github.com/asticode/go-astisub v0.26.2 h1:cdEXcm+SUSmYCEPTQYbbfCECnmQoIFfH6pF8wDJhfVo=
github.com/asticode/go-astisub v0.26.2/go.mod h1:WTkuSzFB+Bp7wezuSf2Oxulj5A8zu2zLRVFf6bIFQK8=
github.com/asticode/go-astits v1.8.0 h1:rf6aiiGn/QhlFjNON1n5plqF3Fs025XLUwiQ0NB6oZg=
github.com/asticode/go-astits v1.8.0/go.mod h1:DkOWmBNQpnr9mv24KfZjq4JawCFX1FCqjLVGvO0DygQ=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/profile v1.4.0/go.mod h1:NWz/XGvpEW1FyYQ7fCx4dqYBLlfTcE+A9FLAkNKqjFE=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20200904194848-62affa334b73 h1:MXfv8rhZWmFeqX3GNZRsd6vOLoaCHjYEX3qkRo3YBUA=
golang.org/x/net v0.0.0-20200904194848-62affa334b73/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

64
main.go
View File

@ -19,6 +19,7 @@ import (
"embed"
"github.com/BurntSushi/toml"
"github.com/asticode/go-astisub"
)
type Item struct {
@ -62,7 +63,7 @@ type ProgressInfo struct {
var templates *template.Template
//go:embed templates/*
//go:embed templates
var templateFS embed.FS
func init() {
@ -320,12 +321,22 @@ func downloadFile(item Item) error {
command := getDownloadCommand(item, mpdPath)
subtitlePaths, err := downloadAndConvertSubtitles(item.Subtitles)
if err != nil {
fmt.Printf("Error processing subtitles: %v\n", err)
}
for _, path := range subtitlePaths {
fmt.Println("Adding subtitle:", path)
command += fmt.Sprintf(" --mux-import \"path=%s:lang=nl:name=Nederlands\"", path)
}
cmd := exec.Command("bash", "-c", command)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
err = cmd.Run()
if err != nil {
return fmt.Errorf("error executing download command: %v", err)
}
@ -334,6 +345,55 @@ func downloadFile(item Item) error {
return nil
}
func downloadAndConvertSubtitles(subtitlesURLs string) ([]string, error) {
var subtitlePaths []string
urls := strings.Split(subtitlesURLs, ",")
for _, url := range urls {
vttPath, err := downloadSubtitle(url)
if err != nil {
return nil, fmt.Errorf("error downloading subtitle: %v", err)
}
srtPath, err := convertVTTtoSRT(vttPath)
if err != nil {
return nil, fmt.Errorf("error converting subtitle: %v", err)
}
subtitlePaths = append(subtitlePaths, srtPath)
}
return subtitlePaths, nil
}
func downloadSubtitle(url string) (string, error) {
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
tempFile, err := os.CreateTemp("", "subtitle_*.vtt")
if err != nil {
return "", err
}
defer tempFile.Close()
_, err = io.Copy(tempFile, resp.Body)
if err != nil {
return "", err
}
return tempFile.Name(), nil
}
func convertVTTtoSRT(vttPath string) (string, error) {
srtPath := strings.TrimSuffix(vttPath, ".vtt") + ".srt"
s1, _ := astisub.OpenFile(vttPath)
s1.Write(srtPath)
return srtPath, nil
}
func isValidURL(toTest string) bool {
_, err := url.ParseRequestURI(toTest)
return err == nil