package main
import (
"bufio"
"flag"
"fmt"
"image/color"
"log"
"os"
"strings"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
)
// 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]"
)
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.New()
debugLog(logCatUI, "created fyne app: %#v", a)
w := a.NewWindow("VideoTools")
w.Resize(fyne.NewSize(920, 540))
debugLog(logCatUI, "window initialized (size 920x540)")
menu := buildMainMenu()
bg := canvas.NewRectangle(backgroundColor)
bg.SetMinSize(fyne.NewSize(920, 540))
w.SetContent(container.NewMax(bg, menu))
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