Compare commits

...
2 Commits
5 changed files with 107 additions and 29 deletions
+1 -1
View File
@@ -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.
+58 -10
View File
@@ -20,7 +20,9 @@ var checkmark = lipgloss.NewStyle().Foreground(lipgloss.Color("#22FF33")).Render
var x = lipgloss.NewStyle().Foreground(lipgloss.Color("#FF2233")).Render("✖️")
const (
Cfg Screen = iota
None Screen = iota
Files
Cfg
Main
)
@@ -55,13 +57,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(),
@@ -93,7 +108,7 @@ func (m model) updateConfigFocusedOptions() {
}
func (m model) Init() tea.Cmd {
return tea.Batch(m.Spinner.Tick, initCfg)
return tea.Batch(m.Spinner.Tick, m.statFiles)
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
@@ -104,9 +119,30 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.ErrQuitMessage = msg.msg
m.ErrQuit = true
return m, m.cleanUp
case filesStatMsg:
m.FileCount = msg.fileCount
m.Files = msg.files
if len(m.Files) == 1 {
m.Screen = Cfg
} else if len(m.Files) > 1 {
m.Screen = Files
}
return m, nil
}
switch m.Screen {
case Files:
// TODO: navigate files and select them and stuff
switch msg := msg.(type) {
case tea.KeyMsg:
key := msg.String()
switch key {
case "ctrl+c", "esc":
return m, tea.Quit
}
}
case Cfg:
switch msg := msg.(type) {
case tea.KeyMsg:
@@ -165,7 +201,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case parsedCfgMsg:
m.Screen = Main
m.ParsedConfig = msg.parsedConfig
return m, tea.Batch(m.Spinner.Tick, m.statFile)
return m, tea.Batch(m.Spinner.Tick, startEncoding)
}
case Main:
switch msg := msg.(type) {
@@ -181,10 +217,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
return m, tea.Quit
}
case initUi:
m.FileCount = msg.fileCount
m.Files = msg.files
case encodingStartedMsg:
if m.DryRun {
return m, gracefullyQuit
}
@@ -261,6 +294,19 @@ func parseConfig(cfg []Config) ParsedConfig {
}
}
func FilesScreenView(m model) string {
view := ""
// TODO: Create a nice view where we can select different videos and then select the encoding options and start
// encoding.
for _, file := range m.Files {
view += filepath.Base(file)
view += "\n"
}
return view
}
func CfgScreenView(m model) string {
view := ""
@@ -380,6 +426,8 @@ func (m model) View() string {
}
switch m.Screen {
case Files:
return FilesScreenView(m)
case Cfg:
return CfgScreenView(m)
case Main:
+21 -13
View File
@@ -25,24 +25,24 @@ type ffmpegProcessStart struct {
cmd *exec.Cmd
}
type initUi struct {
type filesStatMsg struct {
fileCount int
files []string
}
func (m *model) statFile() tea.Msg {
func (m *model) statFiles() tea.Msg {
if m.IsDirectory {
entries, _ := os.ReadDir(m.Path)
sort.Sort(DirEntrySlice(entries))
for _, entry := range entries {
fullFilePath := filepath.Join(m.Path, entry.Name())
if entry.IsDir() {
if entry.IsDir() || !entry.Type().IsRegular() {
continue
}
fullFilePath := filepath.Join(m.Path, entry.Name())
mType, err := mimetype.DetectFile(fullFilePath)
if err != nil {
log.Fatal(err)
@@ -55,17 +55,27 @@ func (m *model) statFile() tea.Msg {
m.FileCount++
m.Files = append(m.Files, fullFilePath)
}
if len(m.Files) == 0 {
return errQuitMsg{"Chosen directory has no video files"}
}
} else {
m.FileCount = 1
m.Files = append(m.Files, m.Path)
}
return initUi{
return filesStatMsg{
fileCount: m.FileCount,
files: m.Files,
}
}
type encodingStartedMsg struct{}
func startEncoding() tea.Msg {
return encodingStartedMsg{}
}
type encodeVideoMsg struct{}
func encodeVideo() tea.Msg {
@@ -78,12 +88,6 @@ func gracefullyQuit() tea.Msg {
return quitMsg{}
}
type initCfgMsg struct{}
func initCfg() tea.Msg {
return initCfgMsg{}
}
type parsedCfgMsg struct {
parsedConfig ParsedConfig
dryRun bool
@@ -117,6 +121,10 @@ type errQuitMsg struct {
}
func (m *model) cleanUp() tea.Msg {
if len(m.Files) == 0 {
return tea.Quit()
}
fullFilePath := m.Files[len(m.Files)-1]
fileName := filepath.Base(fullFilePath)
@@ -124,7 +132,7 @@ func (m *model) cleanUp() tea.Msg {
extensionIndex := strings.LastIndex(fileName, ".")
newFileName := fileName[:extensionIndex]
extension := fileName[extensionIndex:]
newFileFullPath := filepath.Join(parentDir, newFileName+fmt.Sprintf(" [%s] [%s]", m.ParsedConfig.VideoEncoder, m.ParsedConfig.AudioEncoder)+extension)
newFileFullPath := filepath.Join(parentDir, newFileName+fmt.Sprintf("_[%s]_[%s]", m.ParsedConfig.VideoEncoder, m.ParsedConfig.AudioEncoder)+extension)
os.Remove(newFileFullPath)
+26 -2
View File
@@ -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)
+1 -3
View File
@@ -92,9 +92,7 @@ func buildFFmpegCmdArgs(fullFilePath string, outFullFilePath string, cfg ParsedC
args = append(args, cfg.AudioEncoder)
}
for _, arg := range additionalArgs {
args = append(args, arg)
}
args = append(args, additionalArgs...)
// Output file
args = append(args, outFullFilePath)