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"
"path"
"path/filepath"
"regexp"
"strings"
"time"
@ -272,27 +271,18 @@ func (i *FileInfo) detectSubtitles() {
i.Subtitles = []string{}
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
afs := &afero.Afero{Fs: i.Fs}
parentDir := strings.TrimRight(i.Path, i.Name)
dir, err := afs.ReadDir(parentDir)
if err == nil {
base := strings.TrimSuffix(i.Name, ext)
r := regexp.MustCompile("^" + regexp.QuoteMeta(base) + `\.(.*\.)?vtt$`)
for _, f := range dir {
if !f.IsDir() {
if matches := r.FindStringSubmatch(f.Name()); len(matches) == 2 {
i.Subtitles = append(i.Subtitles, path.Join(parentDir, matches[0]))
}
if !f.IsDir() && strings.HasPrefix(f.Name(), base) && strings.HasSuffix(f.Name(), ".vtt") {
i.Subtitles = append(i.Subtitles, path.Join(parentDir, f.Name()))
}
}
return
}
fPath := strings.TrimSuffix(i.Path, ext) + ".vtt"
if _, err := i.Fs.Stat(fPath); err == nil {
i.Subtitles = append(i.Subtitles, fPath)
}
}