Files
ffui/cmds.go
T

141 lines
2.8 KiB
Go
Raw Normal View History

2022-10-07 06:07:44 +03:00
package main
import (
2022-10-08 02:36:15 +03:00
"fmt"
2022-10-07 06:07:44 +03:00
"log"
"os"
"os/exec"
2022-10-07 06:07:44 +03:00
"path/filepath"
"sort"
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/gabriel-vasile/mimetype"
)
type finishedEncodingVideo struct{}
type updateProgress struct {
progress float64
}
type updateEstimate struct {
estimate int
}
2022-10-07 06:07:44 +03:00
type ffmpegProcessStart struct {
cmd *exec.Cmd
}
2024-12-20 23:25:13 +02:00
type filesStatMsg struct {
2022-10-07 06:07:44 +03:00
fileCount int
files []string
}
2024-12-20 23:25:13 +02:00
func (m *model) statFiles() tea.Msg {
2022-10-07 06:07:44 +03:00
if m.IsDirectory {
entries, _ := os.ReadDir(m.Path)
sort.Sort(DirEntrySlice(entries))
for _, entry := range entries {
2024-12-20 23:25:13 +02:00
if entry.IsDir() || !entry.Type().IsRegular() {
2022-10-07 06:07:44 +03:00
continue
}
2024-12-20 23:25:13 +02:00
fullFilePath := filepath.Join(m.Path, entry.Name())
2022-10-07 06:07:44 +03:00
mType, err := mimetype.DetectFile(fullFilePath)
if err != nil {
log.Fatal(err)
}
if !strings.HasPrefix(mType.String(), "video/") {
continue
}
m.FileCount++
m.Files = append(m.Files, fullFilePath)
}
2024-12-20 23:25:13 +02:00
if len(m.Files) == 0 {
return errQuitMsg{"Chosen directory has no video files"}
}
2022-10-07 06:07:44 +03:00
} else {
m.FileCount = 1
m.Files = append(m.Files, m.Path)
}
2024-12-20 23:25:13 +02:00
return filesStatMsg{
2022-10-07 06:07:44 +03:00
fileCount: m.FileCount,
files: m.Files,
}
}
2024-12-20 23:25:13 +02:00
type encodingStartedMsg struct{}
func startEncoding() tea.Msg {
return encodingStartedMsg{}
}
2022-10-07 06:07:44 +03:00
type encodeVideoMsg struct{}
func encodeVideo() tea.Msg {
return encodeVideoMsg{}
}
type quitMsg struct{}
func gracefullyQuit() tea.Msg {
2022-10-07 06:07:44 +03:00
return quitMsg{}
}
2022-10-08 02:36:15 +03:00
type parsedCfgMsg struct {
parsedConfig ParsedConfig
dryRun bool
2022-10-08 02:36:15 +03:00
}
func (m *model) parseConfig(dryRun bool) tea.Cmd {
m.DryRun = dryRun
return func() tea.Msg {
vEncoder := find(m.Config, "Video Encoder")
aEncoder := find(m.Config, "Audio Encoder")
2024-12-19 10:10:51 +02:00
preset := find(m.Config, "Preset")
crf := find(m.Config, "Constant Rate Factor (CRF)")
return parsedCfgMsg{
parsedConfig: ParsedConfig{
DeleteOldVideo: find(m.Config, "Delete old video(s)?").FocusedOption != 0,
SkipEncodedVid: find(m.Config, "What should we do about encoded videos?").FocusedOption == 0,
VideoEncoder: vEncoder.Opts[vEncoder.FocusedOption],
AudioEncoder: aEncoder.Opts[aEncoder.FocusedOption],
Preset: preset.Opts[preset.FocusedOption],
CRF: crf.Opts[crf.FocusedOption],
},
dryRun: dryRun,
}
2022-10-08 02:36:15 +03:00
}
}
type errQuitMsg struct {
msg string
}
func (m *model) cleanUp() tea.Msg {
2024-12-20 23:25:13 +02:00
if len(m.Files) == 0 {
return tea.Quit()
}
2022-10-08 02:36:15 +03:00
fullFilePath := m.Files[len(m.Files)-1]
fileName := filepath.Base(fullFilePath)
parentDir := filepath.Dir(fullFilePath)
extensionIndex := strings.LastIndex(fileName, ".")
newFileName := fileName[:extensionIndex]
extension := fileName[extensionIndex:]
2024-12-20 23:25:13 +02:00
newFileFullPath := filepath.Join(parentDir, newFileName+fmt.Sprintf("_[%s]_[%s]", m.ParsedConfig.VideoEncoder, m.ParsedConfig.AudioEncoder)+extension)
2022-10-08 02:36:15 +03:00
os.Remove(newFileFullPath)
return tea.Quit()
}