Skip to content

Commit

Permalink
Merge pull request #18 from atzuur/dev
Browse files Browse the repository at this point in the history
fixed segfault from ffmpeg check
  • Loading branch information
vladaad authored Sep 7, 2022
2 parents de5f244 + 5b367ab commit 3844f5a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 24 deletions.
17 changes: 9 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@ package main

import (
"flag"
"github.com/vladaad/discordcompressor/build"
"github.com/vladaad/discordcompressor/encoder/audio"
"github.com/vladaad/discordcompressor/encoder/video"
"github.com/vladaad/discordcompressor/metadata"
"github.com/vladaad/discordcompressor/settings"
"github.com/vladaad/discordcompressor/subtitles"
"github.com/vladaad/discordcompressor/utils"
"io"
"log"
"math"
Expand All @@ -19,6 +12,14 @@ import (
"strings"
"sync"
"time"

"github.com/vladaad/discordcompressor/build"
"github.com/vladaad/discordcompressor/encoder/audio"
vidEnc "github.com/vladaad/discordcompressor/encoder/video"
"github.com/vladaad/discordcompressor/metadata"
"github.com/vladaad/discordcompressor/settings"
"github.com/vladaad/discordcompressor/subtitles"
"github.com/vladaad/discordcompressor/utils"
)

var reEncV bool
Expand Down Expand Up @@ -356,7 +357,7 @@ func checkForFF() {
check := []string{"ffmpeg", "ffprobe"}

for i := range check {
if !utils.CheckIfPresent(check[i]) {
if !utils.CommandExists(check[i]) {
message := check[i] + " not installed or not added to PATH"
if runtime.GOOS == "windows" {
message = message + ", you can download it here: https://github.com/BtbN/FFmpeg-Builds/releases"
Expand Down
11 changes: 6 additions & 5 deletions settings/generator.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package settings

import (
"github.com/vladaad/discordcompressor/utils"
"log"
"math"
"os/exec"
"strconv"
"strings"
"time"

"github.com/vladaad/discordcompressor/utils"
)

func populateSettings() {
Expand All @@ -17,7 +18,7 @@ func populateSettings() {

func generateAudioEncoder() *AudioEncoder {
var encoder *AudioEncoder
if utils.CheckIfPresent("qaac64") {
if utils.CommandExists("qaac64") {
encoder = &AudioEncoder{
Name: "aac",
Type: "qaac",
Expand All @@ -41,7 +42,7 @@ func generateAudioEncoder() *AudioEncoder {
MinBitrate: 96,
BitratePerc: 10,
}
} else if utils.CheckIfPresent("fdkaac") {
} else if utils.CommandExists("fdkaac") {
encoder = &AudioEncoder{
Name: "aac",
Type: "fdkaac",
Expand Down Expand Up @@ -133,9 +134,9 @@ func benchmarkx264() float64 {
)

start := time.Now()
err := cmd.Start()
cmd.Start()

err = cmd.Wait()
err := cmd.Wait()
elapsed := time.Since(start)

if err != nil {
Expand Down
10 changes: 7 additions & 3 deletions settings/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package settings

import (
"encoding/json"
"errors"
"fmt"
"github.com/vladaad/discordcompressor/utils"
"io/fs"
"os"

"github.com/vladaad/discordcompressor/utils"
)

// Stolen from https://github.com/Wieku/danser-go/app/settings
Expand All @@ -30,9 +33,8 @@ func LoadSettings(version string) bool {
fileName += ".json"

file, err := os.Open(fileName)
defer file.Close()

if os.IsNotExist(err) {
if errors.Is(err, fs.ErrNotExist) {
populateSettings()
saveSettings(fileName, fileStorage)
return true
Expand All @@ -42,6 +44,8 @@ func LoadSettings(version string) bool {
load(file, fileStorage)
saveSettings(fileName, fileStorage) //this is done to save additions from the current format
}

defer file.Close()
return false
}

Expand Down
20 changes: 12 additions & 8 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package utils

import (
"fmt"
"github.com/google/uuid"
"github.com/vladaad/discordcompressor/build"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"

"github.com/google/uuid"
"github.com/vladaad/discordcompressor/build"
)

func OpenURL(url string) {
Expand Down Expand Up @@ -80,12 +81,15 @@ func SettingsDir() string {
}
}

func CheckIfPresent(filename string) bool {
_, err := exec.Command(filename).Output()
return !strings.Contains(err.Error(), "executable file not found")
func CommandOutput(cmd string, args []string) string {
out, err := exec.Command(cmd, args...).Output()
if err != nil {
log.Fatal(err)
}
return string(out)
}

func CommandOutput(filename string, args []string) string {
out, _ := exec.Command(filename, args...).Output()
return string(out)
func CommandExists(cmd string) bool {
_, err := exec.LookPath(cmd)
return err == nil
}

0 comments on commit 3844f5a

Please sign in to comment.