commit 4988a512cf64e9976ed186efc8134c8a2b722d11
parent 08cdf338b57b7f4da9e621304fbeb020901250c9
Author: breadcat <breadcat@users.noreply.github.com>
Date: Fri, 6 Feb 2026 11:54:44 +0000
Only allow a single transcode process at one time
Diffstat:
| M | main.go | | | 27 | +++++++++++++++++++++++++++ |
1 file changed, 27 insertions(+), 0 deletions(-)
diff --git a/main.go b/main.go
@@ -11,9 +11,14 @@ import (
"os/exec"
"path/filepath"
"strings"
+ "sync"
)
var rootDir string
+var (
+ transcodeMutex sync.Mutex
+ activeCmd *exec.Cmd
+)
type FileInfo struct {
Name string `json:"name"`
@@ -414,6 +419,16 @@ func handleStream(w http.ResponseWriter, r *http.Request) {
return
}
+ // Kill any existing transcoding process before starting a new one
+ transcodeMutex.Lock()
+ if activeCmd != nil && activeCmd.Process != nil {
+ log.Printf("Killing existing ffmpeg process to start new transcode")
+ activeCmd.Process.Kill()
+ activeCmd.Wait() // Wait for it to fully exit
+ activeCmd = nil
+ }
+ transcodeMutex.Unlock()
+
// Set headers for streaming
w.Header().Set("Content-Type", "video/mp4")
w.Header().Set("Cache-Control", "no-cache")
@@ -440,6 +455,11 @@ func handleStream(w http.ResponseWriter, r *http.Request) {
"pipe:1",
)
+ // Track this as the active command
+ transcodeMutex.Lock()
+ activeCmd = cmd
+ transcodeMutex.Unlock()
+
// Capture stderr for debugging
stderr, err := cmd.StderrPipe()
if err != nil {
@@ -500,6 +520,13 @@ func handleStream(w http.ResponseWriter, r *http.Request) {
}
}
+ // Clean up active command reference
+ transcodeMutex.Lock()
+ if activeCmd == cmd {
+ activeCmd = nil
+ }
+ transcodeMutex.Unlock()
+
// Wait for command to finish
if err := cmd.Wait(); err != nil {
// Don't log error if we killed the process intentionally