repochecker/videos: Also check grammar in subtitles

This commit is contained in:
nemunaire 2022-10-31 17:00:37 +01:00
commit 5d716106c4
6 changed files with 192 additions and 105 deletions

View file

@ -0,0 +1,47 @@
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"github.com/asticode/go-astisub"
ffmpeg "github.com/u2takey/ffmpeg-go"
"srs.epita.fr/fic-server/admin/sync"
)
func CheckGrammarSubtitleTrack(path string, exceptions *sync.CheckExceptions) (errs []error) {
tmpfile, err := ioutil.TempFile("", "resolution-*.srt")
if err != nil {
errs = append(errs, fmt.Errorf("unable to create a temporary file: %w", err))
return
}
defer os.Remove(tmpfile.Name())
// Extract subtitles
err = ffmpeg.Input(path).
Output(tmpfile.Name(), ffmpeg.KwArgs{"map": "0:s:0"}).
OverWriteOutput().Run()
if err != nil {
errs = append(errs, fmt.Errorf("ffmpeg returns an error when extracting subtitles track: %w", err))
}
subtitles, err := astisub.OpenFile(tmpfile.Name())
if err != nil {
log.Println("Unable to open subtitles file:", err)
return
}
var lines []string
for _, item := range subtitles.Items {
lines = append(lines, item.String())
}
for _, e := range hooks.CallCustomHook("CheckGrammar", strings.Join(lines, "\n"), exceptions) {
errs = append(errs, fmt.Errorf("subtitle-track: %w", e))
}
return
}