Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow downloading of ffmpeg/ffprobe if no acceptable executables on path #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions cmd/duration/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
24 changes: 18 additions & 6 deletions cmd/videohashes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"os"
"path/filepath"

"github.com/peolic/videohashes/internal"
)
Expand All @@ -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)
}
Expand All @@ -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 {
Expand Down
30 changes: 27 additions & 3 deletions internal/common.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"context"
"fmt"
"os"

Expand All @@ -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)

Expand Down