verifyInstall.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package config
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "strings"
  8. log "github.com/sirupsen/logrus"
  9. "golang.org/x/mod/semver"
  10. )
  11. // VerifyFFMpegPath verifies that the path exists, is a file, and is executable.
  12. func VerifyFFMpegPath(path string) error {
  13. stat, err := os.Stat(path)
  14. if os.IsNotExist(err) {
  15. return errors.New("ffmpeg path does not exist")
  16. }
  17. if err != nil {
  18. return fmt.Errorf("error while verifying the ffmpeg path: %s", err.Error())
  19. }
  20. if stat.IsDir() {
  21. return errors.New("ffmpeg path can not be a folder")
  22. }
  23. mode := stat.Mode()
  24. // source: https://stackoverflow.com/a/60128480
  25. if mode&0o111 == 0 {
  26. return errors.New("ffmpeg path is not executable")
  27. }
  28. cmd := exec.Command(path)
  29. out, _ := cmd.CombinedOutput()
  30. response := string(out)
  31. if response == "" {
  32. return fmt.Errorf("unable to determine the version of your ffmpeg installation at %s you may experience issues with video", path)
  33. }
  34. responseComponents := strings.Split(response, " ")
  35. if len(responseComponents) < 3 {
  36. log.Debugf("unable to determine the version of your ffmpeg installation at %s you may experience issues with video", path)
  37. return nil
  38. }
  39. fullVersionString := responseComponents[2]
  40. versionString := "v" + strings.Split(fullVersionString, "-")[0]
  41. // Some builds of ffmpeg have weird build numbers that we can't parse
  42. if !semver.IsValid(versionString) {
  43. log.Debugf("unable to determine if ffmpeg version %s is recent enough. if you experience issues with video you may want to look into updating", fullVersionString)
  44. return nil
  45. }
  46. if semver.Compare(versionString, FfmpegSuggestedVersion) == -1 {
  47. return fmt.Errorf("your %s version of ffmpeg at %s may be older than the suggested version of %s you may experience issues with video", versionString, path, FfmpegSuggestedVersion)
  48. }
  49. return nil
  50. }