2022-10-31 16:00:37 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
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"
|
|
|
|
"srs.epita.fr/fic-server/libfic"
|
|
|
|
)
|
|
|
|
|
|
|
|
type VideoInfo struct {
|
|
|
|
Streams []struct {
|
2023-01-18 14:49:41 +00:00
|
|
|
Index uint `json:"index"`
|
2022-10-31 16:00:37 +00:00
|
|
|
CodecType string `json:"codec_type"`
|
|
|
|
CodecName string `json:"codec_name"`
|
|
|
|
CodecLongName string `json:"codec_long_name"`
|
|
|
|
Duration string
|
|
|
|
NbFrames string `json:"nb_frames"`
|
|
|
|
Width int
|
|
|
|
Height int
|
2023-01-18 14:49:41 +00:00
|
|
|
Tags map[string]string `json:"tags"`
|
2022-10-31 16:00:37 +00:00
|
|
|
} `json:"streams"`
|
|
|
|
}
|
|
|
|
|
2022-10-31 17:12:11 +00:00
|
|
|
func gcd(a, b int) int {
|
|
|
|
var bgcd func(a, b, res int) int
|
|
|
|
|
|
|
|
bgcd = func(a, b, res int) int {
|
|
|
|
switch {
|
|
|
|
case a == b:
|
|
|
|
return res * a
|
|
|
|
case a%2 == 0 && b%2 == 0:
|
|
|
|
return bgcd(a/2, b/2, 2*res)
|
|
|
|
case a%2 == 0:
|
|
|
|
return bgcd(a/2, b, res)
|
|
|
|
case b%2 == 0:
|
|
|
|
return bgcd(a, b/2, res)
|
|
|
|
case a > b:
|
|
|
|
return bgcd(a-b, b, res)
|
|
|
|
default:
|
|
|
|
return bgcd(a, b-a, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bgcd(a, b, 1)
|
|
|
|
}
|
|
|
|
|
2023-11-22 11:16:53 +00:00
|
|
|
func checkResolutionVideo(e *fic.Exercice, exceptions *sync.CheckExceptions) (errs error) {
|
2022-10-31 16:00:37 +00:00
|
|
|
i, ok := sync.GlobalImporter.(sync.LocalImporter)
|
|
|
|
if !ok {
|
|
|
|
log.Printf("Unable to load `videos-rules.so` as the current Importer is not a LocalImporter (%T).", sync.GlobalImporter)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(e.VideoURI) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter exceptions to only keep related to resolution.mp4
|
|
|
|
exceptions = exceptions.GetFileExceptions("resolution.mp4")
|
|
|
|
|
|
|
|
path, err := url.PathUnescape(e.VideoURI[9:])
|
|
|
|
if err != nil {
|
|
|
|
path = e.VideoURI
|
|
|
|
}
|
|
|
|
path = i.GetLocalPath(path)
|
|
|
|
|
|
|
|
data, err := ffmpeg.Probe(path)
|
|
|
|
if err != nil {
|
2023-11-22 11:16:53 +00:00
|
|
|
errs = multierr.Append(errs, fmt.Errorf("unable to open %q: %w", path, err))
|
2022-10-31 16:00:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vInfo := &VideoInfo{}
|
|
|
|
err = json.Unmarshal([]byte(data), vInfo)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-01-18 14:49:41 +00:00
|
|
|
video_seen := []int{}
|
|
|
|
subtitles_seen := []int{}
|
|
|
|
for idx, s := range vInfo.Streams {
|
2022-10-31 16:00:37 +00:00
|
|
|
if s.CodecType == "video" {
|
2023-01-18 14:49:41 +00:00
|
|
|
video_seen = append(video_seen, idx)
|
2022-10-31 16:00:37 +00:00
|
|
|
if (s.Width > 1920 || s.Height > 1080) && !exceptions.HasException(":size:above_maximum") {
|
2023-11-22 11:16:53 +00:00
|
|
|
errs = multierr.Append(errs, fmt.Errorf("video track is too wide: %dx%d (maximum allowed: 1920x1080)", s.Width, s.Height))
|
2022-10-31 16:00:37 +00:00
|
|
|
}
|
|
|
|
|
2022-10-31 17:12:11 +00:00
|
|
|
ratio := s.Width * 10 / s.Height
|
|
|
|
if ratio < 13 || ratio > 19 && !exceptions.HasException(":size:strange_ratio") {
|
|
|
|
m := gcd(s.Width, s.Height)
|
2023-11-22 11:16:53 +00:00
|
|
|
errs = multierr.Append(errs, fmt.Errorf("video track has a strange ratio: %d:%d. Is this really expected?", s.Width/m, s.Height/m))
|
2022-10-31 17:12:11 +00:00
|
|
|
}
|
|
|
|
|
2022-10-31 16:00:37 +00:00
|
|
|
if s.CodecName != "h264" {
|
2023-11-22 11:16:53 +00:00
|
|
|
errs = multierr.Append(errs, fmt.Errorf("video codec has to be H264 (currently: %s)", s.CodecLongName))
|
2022-10-31 16:00:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
duration, err := strconv.ParseFloat(s.Duration, 64)
|
|
|
|
if err == nil {
|
|
|
|
if duration < 45 && !exceptions.HasException(":duration:too_short") {
|
2023-11-22 11:16:53 +00:00
|
|
|
errs = multierr.Append(errs, fmt.Errorf("video is too short"))
|
2022-10-31 16:00:37 +00:00
|
|
|
}
|
|
|
|
if duration > 450 && !exceptions.HasException(":duration:too_long") {
|
2023-11-22 11:16:53 +00:00
|
|
|
errs = multierr.Append(errs, fmt.Errorf("video is too long"))
|
2022-10-31 16:00:37 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-11-22 11:16:53 +00:00
|
|
|
errs = multierr.Append(errs, fmt.Errorf("invalid track duration: %q", s.Duration))
|
2022-10-31 16:00:37 +00:00
|
|
|
}
|
|
|
|
} else if s.CodecType == "subtitle" {
|
2023-01-18 14:49:41 +00:00
|
|
|
subtitles_seen = append(subtitles_seen, idx)
|
2022-10-31 16:00:37 +00:00
|
|
|
|
|
|
|
if s.CodecName != "mov_text" {
|
2023-11-22 11:16:53 +00:00
|
|
|
errs = multierr.Append(errs, fmt.Errorf("subtitle format has to be MOV text/3GPP Timed Text (currently: %s)", s.CodecLongName))
|
2022-10-31 16:00:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nbframes, err := strconv.ParseInt(s.NbFrames, 10, 64)
|
|
|
|
if err == nil {
|
|
|
|
if nbframes < 5 && !exceptions.HasException(":subtitle:tiny") {
|
2023-11-22 11:16:53 +00:00
|
|
|
errs = multierr.Append(errs, fmt.Errorf("too few subtitles"))
|
2022-10-31 16:00:37 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-11-22 11:16:53 +00:00
|
|
|
errs = multierr.Append(errs, fmt.Errorf("invalid number of frame: %q", s.NbFrames))
|
2022-10-31 16:00:37 +00:00
|
|
|
}
|
|
|
|
} else if s.CodecType == "audio" {
|
|
|
|
if !exceptions.HasException(":audio:allowed") {
|
2023-11-22 11:16:53 +00:00
|
|
|
errs = multierr.Append(errs, fmt.Errorf("an audio track is present, use subtitle for explainations"))
|
2022-10-31 16:00:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if s.CodecName != "aac" {
|
2023-11-22 11:16:53 +00:00
|
|
|
errs = multierr.Append(errs, fmt.Errorf("audio codec has to be AAC (Advanced Audio Coding) (currently: %s)", s.CodecLongName))
|
2022-10-31 16:00:37 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-11-22 11:16:53 +00:00
|
|
|
errs = multierr.Append(errs, fmt.Errorf("unknown track found of type %q", s.CodecType))
|
2022-10-31 16:00:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-18 14:49:41 +00:00
|
|
|
if len(video_seen) == 0 {
|
2023-11-22 11:16:53 +00:00
|
|
|
errs = multierr.Append(errs, fmt.Errorf("no video track found"))
|
2023-01-18 14:49:41 +00:00
|
|
|
} else if len(video_seen) > 1 {
|
2023-11-22 11:16:53 +00:00
|
|
|
errs = multierr.Append(errs, fmt.Errorf("%d video tracks found, is it expected?", len(video_seen)))
|
2022-10-31 16:00:37 +00:00
|
|
|
}
|
2023-01-18 14:49:41 +00:00
|
|
|
if len(subtitles_seen) == 0 && !exceptions.HasException(":subtitle:no_track") {
|
2023-11-22 11:16:53 +00:00
|
|
|
errs = multierr.Append(errs, fmt.Errorf("no subtitles track found"))
|
2023-01-18 14:49:41 +00:00
|
|
|
} else if len(subtitles_seen) > 0 {
|
|
|
|
for _, idx := range subtitles_seen {
|
2023-01-18 18:25:27 +00:00
|
|
|
language := e.Language
|
2023-01-18 14:49:41 +00:00
|
|
|
if lang, ok := vInfo.Streams[idx].Tags["language"]; e.Language != "" && (!ok || lang == "" || lang == "und") {
|
2023-11-22 11:16:53 +00:00
|
|
|
errs = multierr.Append(errs, fmt.Errorf("subtitles track %d with no language defined", vInfo.Streams[idx].Index))
|
2023-01-18 18:25:27 +00:00
|
|
|
} else {
|
|
|
|
language = lang
|
2023-01-18 14:49:41 +00:00
|
|
|
}
|
|
|
|
|
2023-11-22 11:16:53 +00:00
|
|
|
errs = multierr.Append(errs, CheckGrammarSubtitleTrack(path, vInfo.Streams[idx].Index, language, exceptions))
|
2023-01-18 14:49:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if e.Language != "" && len(subtitles_seen) < 2 {
|
2023-11-22 11:16:53 +00:00
|
|
|
errs = multierr.Append(errs, fmt.Errorf("subtitle tracks must exist in original language and translated, only one subtitle track found"))
|
2023-01-18 14:49:41 +00:00
|
|
|
}
|
|
|
|
|
2022-10-31 16:00:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|