Skip to content

Commit

Permalink
Merge pull request #517 from skrashevich/230711-jpg-resize
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Jul 16, 2023
2 parents ade4c03 + 490a48c commit d62b1e4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,7 @@ API examples:

- MJPEG stream: `http://192.168.1.123:1984/api/stream.mjpeg?src=camera1`
- JPEG snapshots: `http://192.168.1.123:1984/api/frame.jpeg?src=camera1`
- JPEG snapshot resized to 100px: `http://192.168.1.123:1984/api/frame.jpeg?src=camera1&h=100`

### Module: Log

Expand Down
10 changes: 8 additions & 2 deletions internal/ffmpeg/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ package ffmpeg

import (
"bytes"
"fmt"
"os/exec"
)

func TranscodeToJPEG(b []byte) ([]byte, error) {
cmd := exec.Command(defaults["bin"], "-hide_banner", "-i", "-", "-f", "mjpeg", "-")
func TranscodeToJPEG(b []byte, height ...int) ([]byte, error) {
cmdArgs := []string{defaults["bin"], "-hide_banner", "-i", "-", "-f", "mjpeg"}
if len(height) > 0 {
cmdArgs = append(cmdArgs, "-vf", fmt.Sprintf("scale=-1:%d", height[0]))
}
cmdArgs = append(cmdArgs, "-")
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
cmd.Stdin = bytes.NewBuffer(b)
return cmd.Output()
}
19 changes: 16 additions & 3 deletions internal/mjpeg/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,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))
}
Expand Down

0 comments on commit d62b1e4

Please sign in to comment.