Refactored code to resize JPEG snapshot if "h" parameter exists in the URL query

This commit is contained in:
Sergey Krashevich
2023-07-11 10:35:53 +03:00
parent b5d40caffc
commit 490a48cd50
3 changed files with 25 additions and 5 deletions

View File

@@ -60,9 +60,22 @@ func handlerKeyframe(w http.ResponseWriter, r *http.Request) {
case core.CodecH264, core.CodecH265:
ts := time.Now()
var err error
if data, err = ffmpeg.TranscodeToJPEG(data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
// Resize image if "h" parameter exists
if hParam := r.URL.Query().Get("h"); hParam != "" {
h, err := strconv.Atoi(hParam)
if err != nil {
http.Error(w, "Invalid height parameter", http.StatusBadRequest)
return
}
if data, err = ffmpeg.TranscodeToJPEG(data, h); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
} else {
if data, err = ffmpeg.TranscodeToJPEG(data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
log.Debug().Msgf("[mjpeg] transcoding time=%s", time.Since(ts))
}