package main
import (
"bufio"
"context"
"encoding/json"
"flag"
"fmt"
"image"
"image/color"
"image/png"
"io"
"log"
"math"
"os"
"os/exec"
"path/filepath"
"slices"
"strconv"
"strings"
"sync"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/storage"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"git.leaktechnologies.dev/stu/VideoTools/internal/player"
"github.com/hajimehoshi/oto"
)
// Module describes a high level tool surface that gets a tile on the menu.
type Module struct {
ID string
Label string
Color color.Color
Handle func(files []string)
}
var (
debugFlag = flag.Bool("debug", false, "enable verbose logging (env: VIDEOTOOLS_DEBUG=1)")
backgroundColor = mustHex("#0B0F1A")
gridColor = mustHex("#171C2A")
textColor = mustHex("#E1EEFF")
queueColor = mustHex("#5961FF")
modules = []Module{
{"convert", "Convert", mustHex("#5E2AE2"), handleConvert},
{"merge", "Merge", mustHex("#3852F3"), handleMerge},
{"trim", "Trim", mustHex("#1B87F4"), handleTrim},
{"filters", "Filters", mustHex("#1FC4D0"), handleFilters},
{"upscale", "Upscale", mustHex("#3FD777"), handleUpscale},
{"audio", "Audio", mustHex("#9CE33D"), handleAudio},
{"thumb", "Thumb", mustHex("#F0C33E"), handleThumb},
{"inspect", "Inspect", mustHex("#F69A3F"), handleInspect},
}
)
var (
logFilePath string
logFile *os.File
logHistory []string
debugEnabled bool
debugLogger = log.New(os.Stderr, "[videotools] ", log.LstdFlags|log.Lmicroseconds)
)
const logHistoryMax = 500
type logCategory string
const (
logCatUI logCategory = "[UI]"
logCatCLI logCategory = "[CLI]"
logCatFFMPEG logCategory = "[FFMPEG]"
logCatSystem logCategory = "[SYS]"
logCatModule logCategory = "[MODULE]"
)
type formatOption struct {
Label string
Ext string
VideoCodec string
}
var formatOptions = []formatOption{
{"MP4 (H.264)", ".mp4", "libx264"},
{"MKV (H.265)", ".mkv", "libx265"},
{"MOV (ProRes)", ".mov", "prores_ks"},
}
type convertConfig struct {
OutputBase string
SelectedFormat formatOption
Quality string
Mode string
InverseTelecine bool
InverseAutoNotes string
CoverArtPath string
AspectHandling string
}
func (c convertConfig) OutputFile() string {
base := strings.TrimSpace(c.OutputBase)
if base == "" {
base = "converted"
}
return base + c.SelectedFormat.Ext
}
func (c convertConfig) CoverLabel() string {
if strings.TrimSpace(c.CoverArtPath) == "" {
return "Cover: none"
}
return fmt.Sprintf("Cover: %s", filepath.Base(c.CoverArtPath))
}
type appState struct {
window fyne.Window
active string
source *videoSource
anim *previewAnimator
convert convertConfig
currentFrame string
player player.Controller
playerReady bool
playerVolume float64
playerMuted bool
lastVolume float64
playerPaused bool
playSess *playSession
}
func (s *appState) stopPreview() {
if s.anim != nil {
s.anim.Stop()
s.anim = nil
}
}
func (s *appState) startPreview(frames []string, img *canvas.Image, slider *widget.Slider) {
if len(frames) == 0 {
return
}
anim := &previewAnimator{frames: frames, img: img, slider: slider, stop: make(chan struct{}), playing: true, state: s}
s.anim = anim
anim.Start()
}
func (s *appState) hasSource() bool {
return s.source != nil
}
func (s *appState) applyInverseDefaults(src *videoSource) {
if src == nil {
return
}
if src.IsProgressive() {
s.convert.InverseTelecine = false
s.convert.InverseAutoNotes = "Progressive source detected; inverse telecine disabled."
} else {
s.convert.InverseTelecine = true
s.convert.InverseAutoNotes = "Interlaced source detected; smoothing enabled."
}
}
func (s *appState) setContent(body fyne.CanvasObject) {
bg := canvas.NewRectangle(backgroundColor)
bg.SetMinSize(fyne.NewSize(920, 540))
if body == nil {
s.window.SetContent(bg)
return
}
s.window.SetContent(container.NewMax(bg, body))
}
func (s *appState) showMainMenu() {
s.stopPreview()
s.stopPlayer()
s.active = ""
s.setContent(container.NewPadded(buildMainMenu(s)))
}
func (s *appState) showModule(id string) {
switch id {
case "convert":
s.showConvertView(nil)
default:
debugLog(logCatUI, "UI module %s not wired yet", id)
}
}
func (s *appState) showConvertView(file *videoSource) {
s.stopPreview()
s.active = "convert"
if file != nil {
s.source = file
}
if s.source == nil {
s.convert.OutputBase = "converted"
s.convert.CoverArtPath = ""
s.convert.AspectHandling = "Auto"
}
s.setContent(buildConvertView(s, s.source))
}
func (s *appState) shutdown() {
s.stopPlayer()
if s.player != nil {
s.player.Close()
}
}
func (s *appState) stopPlayer() {
if s.playSess != nil {
s.playSess.Stop()
s.playSess = nil
}
if s.player != nil {
s.player.Stop()
}
s.playerReady = false
s.playerPaused = false
}
func main() {
initLogging()
defer closeLogs()
flag.Parse()
setDebug(*debugFlag || os.Getenv("VIDEOTOOLS_DEBUG") != "")
debugLog(logCatSystem, "starting VideoTools prototype at %s", time.Now().Format(time.RFC3339))
args := flag.Args()
if len(args) > 0 {
if err := runCLI(args); err != nil {
fmt.Fprintln(os.Stderr, "videotools:", err)
fmt.Fprintln(os.Stderr)
printUsage()
os.Exit(1)
}
return
}
if display := os.Getenv("DISPLAY"); display == "" {
debugLog(logCatUI, "DISPLAY environment variable is empty; GUI may not be visible in headless mode")
} else {
debugLog(logCatUI, "DISPLAY=%s", display)
}
runGUI()
}
func runGUI() {
a := app.NewWithID("com.leaktechnologies.videotools")
a.Settings().SetTheme(&monoTheme{})
debugLog(logCatUI, "created fyne app: %#v", a)
w := a.NewWindow("VideoTools")
w.Resize(fyne.NewSize(920, 540))
debugLog(logCatUI, "window initialized (size 920x540)")
state := &appState{
window: w,
convert: convertConfig{
OutputBase: "converted",
SelectedFormat: formatOptions[0],
Quality: "Standard (CRF 23)",
Mode: "Simple",
InverseTelecine: true,
InverseAutoNotes: "Default smoothing for interlaced footage.",
},
player: player.New(),
playerVolume: 100,
lastVolume: 100,
playerMuted: false,
}
defer state.shutdown()
w.SetOnDropped(func(pos fyne.Position, items []fyne.URI) {
state.handleDrop(items)
})
state.showMainMenu()
debugLog(logCatUI, "main menu rendered with %d modules", len(modules))
w.ShowAndRun()
}
func runCLI(args []string) error {
cmd := strings.ToLower(args[0])
cmdArgs := args[1:]
debugLog(logCatCLI, "command=%s args=%v", cmd, cmdArgs)
switch cmd {
case "convert":
return runConvertCLI(cmdArgs)
case "combine", "merge":
return runCombineCLI(cmdArgs)
case "trim":
handleTrim(cmdArgs)
case "filters":
handleFilters(cmdArgs)
case "upscale":
handleUpscale(cmdArgs)
case "audio":
handleAudio(cmdArgs)
case "thumb":
handleThumb(cmdArgs)
case "inspect":
handleInspect(cmdArgs)
case "logs":
return runLogsCLI()
case "help":
printUsage()
default:
return fmt.Errorf("unknown command %q", cmd)
}
return nil
}
func runConvertCLI(args []string) error {
if len(args) < 2 {
return fmt.Errorf("convert requires input and output files (e.g. videotools convert input.avi output.mp4)")
}
in, out := args[0], args[1]
debugLog(logCatFFMPEG, "convert input=%s output=%s", in, out)
handleConvert([]string{in, out})
return nil
}
func runCombineCLI(args []string) error {
if len(args) == 0 {
return fmt.Errorf("combine requires input files and an output (e.g. videotools combine clip1.mov clip2.wav / final.mp4)")
}
inputs, outputs, err := splitIOArgs(args)
if err != nil {
return err
}
if len(inputs) == 0 || len(outputs) == 0 {
return fmt.Errorf("combine expects one or more inputs, '/', then an output file")
}
debugLog(logCatFFMPEG, "combine inputs=%v output=%v", inputs, outputs)
// For now feed inputs followed by outputs to the merge handler.
handleMerge(append(inputs, outputs...))
return nil
}
func splitIOArgs(args []string) (inputs []string, outputs []string, err error) {
sep := -1
for i, a := range args {
if a == "/" {
sep = i
break
}
}
if sep == -1 {
return nil, nil, fmt.Errorf("missing '/' separator between inputs and outputs")
}
inputs = append(inputs, args[:sep]...)
outputs = append(outputs, args[sep+1:]...)
return inputs, outputs, nil
}
func printUsage() {
fmt.Println("Usage:")
fmt.Println(" videotools convert