2022-10-31 16:00:37 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/asticode/go-astisub"
|
|
|
|
ffmpeg "github.com/u2takey/ffmpeg-go"
|
2023-11-22 11:16:53 +00:00
|
|
|
"go.uber.org/multierr"
|
2022-10-31 16:00:37 +00:00
|
|
|
|
|
|
|
"srs.epita.fr/fic-server/admin/sync"
|
|
|
|
)
|
|
|
|
|
2023-11-22 11:16:53 +00:00
|
|
|
func CheckGrammarSubtitleTrack(path string, index uint, lang string, exceptions *sync.CheckExceptions) (errs error) {
|
2022-10-31 16:00:37 +00:00
|
|
|
tmpfile, err := ioutil.TempFile("", "resolution-*.srt")
|
|
|
|
if err != nil {
|
2023-11-22 11:16:53 +00:00
|
|
|
errs = multierr.Append(errs, fmt.Errorf("unable to create a temporary file: %w", err))
|
2022-10-31 16:00:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.Remove(tmpfile.Name())
|
|
|
|
|
|
|
|
// Extract subtitles
|
|
|
|
err = ffmpeg.Input(path).
|
2023-01-18 14:49:41 +00:00
|
|
|
Output(tmpfile.Name(), ffmpeg.KwArgs{"map": fmt.Sprintf("0:%d", index)}).
|
2022-10-31 16:00:37 +00:00
|
|
|
OverWriteOutput().Run()
|
|
|
|
if err != nil {
|
2023-11-22 11:16:53 +00:00
|
|
|
errs = multierr.Append(errs, fmt.Errorf("ffmpeg returns an error when extracting subtitles track: %w", err))
|
2022-10-31 16:00:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
2023-11-22 11:16:53 +00:00
|
|
|
for _, e := range multierr.Errors(hooks.CallCustomHook("CheckGrammar", struct {
|
2023-01-17 17:26:04 +00:00
|
|
|
Str string
|
|
|
|
Language string
|
2023-11-22 11:16:53 +00:00
|
|
|
}{Str: strings.Join(lines, "\n"), Language: lang[:2]}, exceptions)) {
|
|
|
|
errs = multierr.Append(errs, fmt.Errorf("subtitle-track: %w", e))
|
2022-10-31 16:00:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|