check vtt using prefix and suffix instead of regex

This commit is contained in:
Mazen 2021-12-20 21:14:54 +01:00
parent 39abd1da6d
commit 7e942b0c63

View File

@ -14,7 +14,6 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"regexp"
"strings" "strings"
"time" "time"
@ -272,27 +271,18 @@ func (i *FileInfo) detectSubtitles() {
i.Subtitles = []string{} i.Subtitles = []string{}
ext := filepath.Ext(i.Path) ext := filepath.Ext(i.Path)
// detect multiple languages. Base.Lang.vtt // detect multiple languages. Base*.vtt
// TODO: give subtitles descriptive names (lang) and track attributes // TODO: give subtitles descriptive names (lang) and track attributes
afs := &afero.Afero{Fs: i.Fs} afs := &afero.Afero{Fs: i.Fs}
parentDir := strings.TrimRight(i.Path, i.Name) parentDir := strings.TrimRight(i.Path, i.Name)
dir, err := afs.ReadDir(parentDir) dir, err := afs.ReadDir(parentDir)
if err == nil { if err == nil {
base := strings.TrimSuffix(i.Name, ext) base := strings.TrimSuffix(i.Name, ext)
r := regexp.MustCompile("^" + regexp.QuoteMeta(base) + `\.(.*\.)?vtt$`)
for _, f := range dir { for _, f := range dir {
if !f.IsDir() { if !f.IsDir() && strings.HasPrefix(f.Name(), base) && strings.HasSuffix(f.Name(), ".vtt") {
if matches := r.FindStringSubmatch(f.Name()); len(matches) == 2 { i.Subtitles = append(i.Subtitles, path.Join(parentDir, f.Name()))
i.Subtitles = append(i.Subtitles, path.Join(parentDir, matches[0]))
}
} }
} }
return
}
fPath := strings.TrimSuffix(i.Path, ext) + ".vtt"
if _, err := i.Fs.Stat(fPath); err == nil {
i.Subtitles = append(i.Subtitles, fPath)
} }
} }