diff --git a/cmd/duration/main.go b/cmd/duration/main.go index 878d5e5..580ba08 100644 --- a/cmd/duration/main.go +++ b/cmd/duration/main.go @@ -3,18 +3,35 @@ package main import ( "fmt" "os" + "path/filepath" "github.com/peolic/videohashes/internal" ) func main() { videoPath := "" + ffmpegInstallDir := "./" + + path, err := os.Executable() + if (err == nil) { + ffmpegInstallDir = filepath.Dir(path) + } + path, err = filepath.Abs(ffmpegInstallDir) + if (err == nil) { + ffmpegInstallDir = path + } args := os.Args[1:] if len(args) >= 1 { videoPath = args[0] } + ffmpegPath, ffprobePath := internal.GetFFPaths(ffmpegInstallDir) + if ffmpegPath == "" || ffprobePath == "" { + fmt.Println("acceptable ffmpeg/ffprobe executables not found on path, and could not be installed") + return + } + if videoPath == "" { fmt.Println("missing video path") return @@ -25,12 +42,6 @@ func main() { return } - _, ffprobePath := internal.GetFFPaths() - if ffprobePath == "" { - fmt.Println("ffprobe executable not found") - return - } - duration := internal.GetDuration(ffprobePath, videoPath) out := fmt.Sprintf("Duration: %s (%d)\n", internal.FormatDuration(duration), duration) diff --git a/cmd/videohashes/main.go b/cmd/videohashes/main.go index 9948dc2..773eb80 100644 --- a/cmd/videohashes/main.go +++ b/cmd/videohashes/main.go @@ -4,6 +4,7 @@ import ( "flag" "fmt" "os" + "path/filepath" "github.com/peolic/videohashes/internal" ) @@ -17,13 +18,30 @@ func main() { videoPath := "" calcMD5 := false jsonOut := false + ffmpegInstallDir := "./" + + path, err := os.Executable() + if (err == nil) { + ffmpegInstallDir = filepath.Dir(path) + } + path, err = filepath.Abs(ffmpegInstallDir) + if (err == nil) { + ffmpegInstallDir = path + } flag.StringVar(&videoPath, "video", "", "path to video file") flag.BoolVar(&calcMD5, "md5", false, "calculate md5 checksum as well") flag.BoolVar(&jsonOut, "json", false, "output in json format") + flag.StringVar(&ffmpegInstallDir, "ffmpeg", ffmpegInstallDir, "where to install ffmpeg if needed") flag.Usage = myUsage flag.Parse() + ffmpegPath, ffprobePath := internal.GetFFPaths(ffmpegInstallDir) + if ffmpegPath == "" || ffprobePath == "" { + fmt.Println("acceptable ffmpeg/ffprobe executables not found on path, and could not be installed") + return + } + if videoPath == "" { videoPath = flag.Arg(0) } @@ -39,12 +57,6 @@ func main() { return } - ffmpegPath, ffprobePath := internal.GetFFPaths() - if ffmpegPath == "" || ffprobePath == "" { - fmt.Println("ffmpeg/ffprobe executables not found") - return - } - result := Result{videoPath: videoPath} if err := result.GeneratePHash(ffmpegPath, ffprobePath); err != nil { diff --git a/internal/common.go b/internal/common.go index d11fec6..68c6a10 100644 --- a/internal/common.go +++ b/internal/common.go @@ -1,6 +1,7 @@ package internal import ( + "context" "fmt" "os" @@ -18,17 +19,40 @@ func ValidFile(filePath string) error { return nil } -func GetFFPaths() (string, string) { - var paths []string +func GetFFPaths(targetDir string) (string, string) { + var paths = []string{targetDir} cwd, err := os.Getwd() if err == nil { paths = append(paths, cwd) } + return GetFFMPEG(targetDir, paths) +} + +func GetFFMPEG(targetDir string, paths []string) (string, string) { + if (targetDir != "") { + var ctx = context.Background() + fullpaths := append([]string{targetDir}, paths...) + + ffmpegPath, ffprobePath := ffmpeg.GetPaths(fullpaths) - return ffmpeg.GetPaths(paths) + if ffmpegPath == "" || ffprobePath == "" { + if err := ffmpeg.Download(ctx, targetDir); err != nil { + msg := `Unable to locate / automatically download FFMPEG +Check the readme for download links. +The FFMPEG and FFProbe binaries should be placed in %s +The error was: %s +` + fmt.Printf(msg, targetDir, err) + return "", "" + } + } + return ffmpeg.GetPaths(fullpaths) + } + return "","" } + func GetDuration(ffprobePath string, videoPath string) int { FFProbe := ffmpeg.FFProbe(ffprobePath)