Change project structure
This commit is contained in:
60
src/subtitles.go
Normal file
60
src/subtitles.go
Normal file
@ -0,0 +1,60 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/asticode/go-astisub"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user