package main import ( "encoding/json" "fmt" "log" "net/url" "strconv" ffmpeg "github.com/u2takey/ffmpeg-go" "srs.epita.fr/fic-server/admin/sync" "srs.epita.fr/fic-server/libfic" ) type VideoInfo struct { Streams []struct { 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 } `json:"streams"` } func CheckResolutionVideo(e *fic.Exercice, exceptions *sync.CheckExceptions) (errs []error) { 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 } path, err := url.PathUnescape(e.VideoURI[9:]) if err != nil { path = e.VideoURI } data, err := ffmpeg.Probe(i.GetLocalPath(path)) if err != nil { errs = append(errs, fmt.Errorf("unable to open %q: %w", path, err)) return } vInfo := &VideoInfo{} err = json.Unmarshal([]byte(data), vInfo) if err != nil { panic(err) } video_seen := false subtitles_seen := false for _, s := range vInfo.Streams { if s.CodecType == "video" { video_seen = true if (s.Width > 1920 || s.Height > 1080) && !exceptions.HasException(":size:above_maximum") { errs = append(errs, fmt.Errorf("video track is too wide: %dx%d (maximum allowed: 1920x1080)", s.Width, s.Height)) } if s.CodecName != "h264" { errs = append(errs, fmt.Errorf("video codec has to be H264 (currently: %s)", s.CodecLongName)) } duration, err := strconv.ParseFloat(s.Duration, 64) if err == nil { if duration < 45 && !exceptions.HasException(":duration:too_short") { errs = append(errs, fmt.Errorf("video is too short")) } if duration > 450 && !exceptions.HasException(":duration:too_long") { errs = append(errs, fmt.Errorf("video is too long")) } } else { errs = append(errs, fmt.Errorf("invalid track duration: %q", s.Duration)) } } else if s.CodecType == "subtitle" { subtitles_seen = true if s.CodecName != "mov_text" { errs = append(errs, fmt.Errorf("subtitle format has to be MOV text/3GPP Timed Text (currently: %s)", s.CodecLongName)) } nbframes, err := strconv.ParseInt(s.NbFrames, 10, 64) if err == nil { if nbframes < 5 && !exceptions.HasException(":subtitle:tiny") { errs = append(errs, fmt.Errorf("too few subtitles")) } } else { errs = append(errs, fmt.Errorf("invalid number of frame: %q", s.NbFrames)) } } else if s.CodecType == "audio" { if !exceptions.HasException(":audio:allowed") { errs = append(errs, fmt.Errorf("an audio track is present, use subtitle for explainations")) } if s.CodecName != "aac" { errs = append(errs, fmt.Errorf("audio codec has to be AAC (Advanced Audio Coding) (currently: %s)", s.CodecLongName)) } } else { errs = append(errs, fmt.Errorf("unknown track found of type %q", s.CodecType)) } } if !video_seen { errs = append(errs, fmt.Errorf("no video track found")) } if !subtitles_seen && !exceptions.HasException(":subtitle:no_track") { errs = append(errs, fmt.Errorf("no subtitles track found")) } return } func RegisterChecksHooks(h *sync.CheckHooks) { h.RegisterExerciceHook(CheckResolutionVideo) }