Skip to content

Commit

Permalink
Add ffmpeg version checker
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed May 23, 2024
1 parent 02af2e2 commit c726651
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
5 changes: 4 additions & 1 deletion internal/ffmpeg/ffmpeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ func Init() {
}

streams.RedirectFunc("ffmpeg", func(url string) (string, error) {
if _, err := Version(); err != nil {
return "", err
}
args := parseArgs(url[7:])
if slices.Contains(args.Codecs, "auto") {
return "", nil // force call streams.HandleFunc("ffmpeg")
Expand All @@ -46,7 +49,7 @@ var defaults = map[string]string{
"global": "-hide_banner",

// inputs
"file": "-re -i {input}",
"file": "-re -readrate_initial_burst 0.001 -i {input}",
"http": "-fflags nobuffer -flags low_delay -i {input}",
"rtsp": "-fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i {input}",

Expand Down
68 changes: 68 additions & 0 deletions internal/ffmpeg/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package ffmpeg

import (
"bytes"
"errors"
"os/exec"
"strings"
"sync"

"github.com/rs/zerolog/log"
)

var checkMu sync.Mutex
var checkErr error
var checkVer string

const (
FFmpeg50 = "59. 16"
FFmpeg51 = "59. 27"
FFmpeg60 = "60. 3"
FFmpeg61 = "60. 16"
FFmpeg70 = "61. 1"
)

func Version() (string, error) {
checkMu.Lock()
defer checkMu.Unlock()

if checkVer != "" {
return checkVer, checkErr
}

cmd := exec.Command(defaults["bin"], "-version")
b, err := cmd.Output()
if err != nil {
checkVer = "-"
checkErr = err
return checkVer, checkErr
}

if len(b) < 100 {
checkVer = "?"
return checkVer, nil
}

// ffmpeg version n7.0-30-g8b0fe91754-20240520 Copyright (c) 2000-2024 the FFmpeg developers
b = b[15:]
if i := bytes.IndexByte(b, ' '); i > 0 {
checkVer = string(b[:i])
}

// libavformat 60. 16.100 / 60. 16.100
if i := strings.Index(string(b), "libavformat"); i > 0 {
// better to compare libavformat, because nightly/master builds
libav := string(b[i+15 : i+25])
if libav < FFmpeg50 {
checkErr = errors.New("ffmpeg: unsupported version: " + checkVer)
return checkVer, checkErr
}
if libav < FFmpeg61 && strings.Contains(defaults["file"], "readrate_initial_burst") {
defaults["file"] = "-re -i {input}"
}
}

log.Debug().Str("version", checkVer).Msgf("[ffmpeg] bin")

return checkVer, nil
}

0 comments on commit c726651

Please sign in to comment.