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"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"sort"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
|
"github.com/gabriel-vasile/mimetype"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type finishedEncodingVideo struct{}
|
|
|
|
|
type updateProgress struct {
|
|
|
|
|
progress float64
|
|
|
|
|
}
|
2024-09-07 03:56:09 +02:00
|
|
|
type updateEstimate struct {
|
|
|
|
|
estimate int
|
|
|
|
|
}
|
2022-10-07 06:07:44 +03:00
|
|
|
|
|
|
|
|
type initUi struct {
|
|
|
|
|
fileCount int
|
|
|
|
|
files []string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *model) statFile() 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() {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
m.FileCount = 1
|
|
|
|
|
m.Files = append(m.Files, m.Path)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return initUi{
|
|
|
|
|
fileCount: m.FileCount,
|
|
|
|
|
files: m.Files,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type encodeVideoMsg struct{}
|
|
|
|
|
|
|
|
|
|
func encodeVideo() tea.Msg {
|
|
|
|
|
return encodeVideoMsg{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type quitMsg struct{}
|
|
|
|
|
|
2024-01-03 22:24:58 +02:00
|
|
|
func gracefullyQuit() tea.Msg {
|
2022-10-07 06:07:44 +03:00
|
|
|
return quitMsg{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type initCfgMsg struct{}
|
|
|
|
|
|
|
|
|
|
func initCfg() tea.Msg {
|
|
|
|
|
return initCfgMsg{}
|
|
|
|
|
}
|
2022-10-08 02:36:15 +03:00
|
|
|
|
|
|
|
|
type parsedCfgMsg struct {
|
|
|
|
|
parsedConfig ParsedConfig
|
2024-01-03 22:24:58 +02:00
|
|
|
dryRun bool
|
2022-10-08 02:36:15 +03:00
|
|
|
}
|
|
|
|
|
|
2024-01-03 22:24:58 +02: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")
|
|
|
|
|
preset := find(m.Config, "Preset (libx264 & libx265 only)")
|
|
|
|
|
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 {
|
|
|
|
|
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:]
|
|
|
|
|
newFileFullPath := filepath.Join(parentDir, newFileName+fmt.Sprintf(" [%s] [%s]", m.ParsedConfig.VideoEncoder, m.ParsedConfig.AudioEncoder)+extension)
|
|
|
|
|
|
|
|
|
|
os.Remove(newFileFullPath)
|
|
|
|
|
|
|
|
|
|
return tea.Quit()
|
|
|
|
|
}
|