From 5e74d10409c64de8a06beeb351f821c92244950b Mon Sep 17 00:00:00 2001 From: IllusionMan1212 Date: Fri, 20 Dec 2024 21:51:05 +0200 Subject: [PATCH] fix: query ffmpeg for encoders and only list the ones we recognize and are available --- TODO.md | 2 +- app.go | 19 ++++++++++++++++--- config.go | 28 ++++++++++++++++++++++++++-- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/TODO.md b/TODO.md index 3f2eb5c..f70181a 100644 --- a/TODO.md +++ b/TODO.md @@ -1,4 +1,4 @@ -- [ ] query ffmpeg binary for available encoders and list the ones we know +- [x] query ffmpeg binary for available encoders and list the ones we know - [ ] when passing in a directory, list all video files in that directory and have the user choose which ones they want encoded - This will of course have a select all checkbox for quickly encoding all the files in a dir. - [ ] calculate total progress based on file sizes instead of counts of files for a more accurate progress percentage. diff --git a/app.go b/app.go index df556e1..eba77e6 100644 --- a/app.go +++ b/app.go @@ -55,13 +55,26 @@ func initialModel(fileInfo os.FileInfo, absolutePath string) *model { s.Spinner = spinner.Dot s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205")) - codecs, err := exec.Command("ffmpeg", "-encoders").Output() + codecsStr, err := exec.Command("ffmpeg", "-encoders").Output() if err != nil { log.Println(err) } - // TODO: parse the codecs and get the one we care about. - log.Println(strings.Split(strings.Split(string(codecs), " ------\n")[1], "\n")) + codecs := strings.Split(strings.Split(string(codecsStr), " ------\n")[1], "\n") + + for i, codec := range codecs { + if codec == "" { + continue + } + codecs[i] = strings.Fields(codec)[1] + + // hardcoded indexes are never bad YEP :))) + if contains(SupportedVideoEncoders, codecs[i]) { + Configs[2].Opts = append(Configs[2].Opts, codecs[i]) + } else if contains(SupportedAudioEncoders, codecs[i]) { + Configs[3].Opts = append(Configs[3].Opts, codecs[i]) + } + } return &model{ IsDirectory: fileInfo.IsDir(), diff --git a/config.go b/config.go index 2c17f1f..6751cf3 100644 --- a/config.go +++ b/config.go @@ -2,11 +2,25 @@ package main import "fmt" +var SupportedVideoEncoders = []string{ + "libx264", + "libx265", + "libvpx-vp9", + "librav1e", + "libsvtav1", +} + +var SupportedAudioEncoders = []string{ + "aac", + "libopus", + "libvorbis", +} + var Configs = []Config{ {Name: "Delete old video(s)?", Opts: []string{"No", "Yes"}, FocusedOption: 1}, {Name: "What should we do about encoded videos?", Opts: []string{"Skip", "Delete and encode again"}}, - {Name: "Video Encoder", Opts: []string{"copy", "libx264", "libx265", "libvpx-vp9", "librav1e", "libsvtav1"}}, - {Name: "Audio Encoder", Opts: []string{"None", "copy", "aac", "libopus"}, FocusedOption: 1}, + {Name: "Video Encoder", Opts: []string{"copy"}}, + {Name: "Audio Encoder", Opts: []string{"None", "copy"}, FocusedOption: 1}, {Name: "Preset", Opts: []string{"ultrafast", "superfast", "veryfast", "faster", "fast", "medium", "slow", "slower", "veryslow"}, FocusedOption: 4}, {Name: "Constant Rate Factor (CRF)", Opts: []string{"10", "15", "20", "25", "30", "35", "40", "45", "50"}, FocusedOption: 4}, } @@ -54,6 +68,16 @@ cfgloop: return filtered } +func contains[T comparable](slice []T, elem T) bool { + for _, e := range slice { + if e == elem { + return true + } + } + + return false +} + func getVisibleConfigs(cfgs []Config) []Config { parsed := parseConfig(cfgs)