2020-10-22 07:47:42 +03:00
package config
2020-06-23 04:11:56 +03:00
import (
"errors"
"fmt"
"os"
2021-01-08 09:45:29 +03:00
"os/exec"
"strings"
2021-01-12 05:31:15 +03:00
log "github.com/sirupsen/logrus"
2021-01-08 09:45:29 +03:00
"golang.org/x/mod/semver"
2020-06-23 04:11:56 +03:00
)
2020-11-13 02:14:59 +03:00
// verifyFFMpegPath verifies that the path exists, is a file, and is executable.
2020-10-22 07:47:42 +03:00
func verifyFFMpegPath ( path string ) error {
2020-06-23 04:11:56 +03:00
stat , err := os . Stat ( path )
if os . IsNotExist ( err ) {
return errors . New ( "ffmpeg path does not exist" )
}
if err != nil {
return fmt . Errorf ( "error while verifying the ffmpeg path: %s" , err . Error ( ) )
}
if stat . IsDir ( ) {
return errors . New ( "ffmpeg path can not be a folder" )
}
mode := stat . Mode ( )
//source: https://stackoverflow.com/a/60128480
if mode & 0111 == 0 {
return errors . New ( "ffmpeg path is not executable" )
}
2021-01-08 09:45:29 +03:00
cmd := exec . Command ( path )
2021-01-19 04:11:25 +03:00
out , _ := cmd . CombinedOutput ( )
2021-01-08 09:45:29 +03:00
response := string ( out )
if response == "" {
return fmt . Errorf ( "unable to determine the version of your ffmpeg installation at %s. you may experience issues with video." , path )
}
responseComponents := strings . Split ( response , " " )
2021-01-12 05:31:15 +03:00
if len ( responseComponents ) < 3 {
log . Debugf ( "unable to determine the version of your ffmpeg installation at %s. you may experience issues with video." , path )
return nil
}
2021-01-08 09:45:29 +03:00
fullVersionString := responseComponents [ 2 ]
2021-01-12 05:31:15 +03:00
2021-01-08 09:45:29 +03:00
versionString := "v" + strings . Split ( fullVersionString , "-" ) [ 0 ]
2021-01-12 05:31:15 +03:00
// Some builds of ffmpeg have wierd build numbers that we can't parse
if ! semver . IsValid ( versionString ) {
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 )
return nil
}
if semver . Compare ( versionString , FfmpegSuggestedVersion ) == - 1 {
2021-01-09 03:09:00 +03:00
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 )
2021-01-08 09:45:29 +03:00
}
2020-06-23 04:11:56 +03:00
return nil
}