fix: omit -t flag when duration=0; use SIGINT for graceful stop

This commit is contained in:
yumoqing 2026-06-28 21:46:38 +08:00
parent 71d321b056
commit 7d89a332ff

View File

@ -21,6 +21,7 @@ public final class FFmpegRecorder: StreamRecorderEngine {
private var process: Process?
private var duration: Double = 0
private var stopRequested = false
public init() {}
@ -46,14 +47,20 @@ public final class FFmpegRecorder: StreamRecorderEngine {
// ffmpeg
let proc = Process()
proc.executableURL = URL(fileURLWithPath: ffmpegPath)
proc.arguments = [
var args = [
"-y",
"-i", resolvedURL.absoluteString,
"-t", String(duration),
]
// duration=0 -t
if duration > 0 {
args += ["-t", String(duration)]
}
args += [
"-c", "copy",
"-movflags", "+faststart",
outputPath
]
proc.arguments = args
let stderrPipe = Pipe()
proc.standardError = stderrPipe
@ -102,7 +109,7 @@ public final class FFmpegRecorder: StreamRecorderEngine {
stderrPipe.fileHandleForReading.readabilityHandler = nil
isRecording = false
guard proc.terminationStatus == 0 else {
guard proc.terminationStatus == 0 || stopRequested else {
throw RecordingError.processFailed(Int(proc.terminationStatus))
}
@ -117,7 +124,11 @@ public final class FFmpegRecorder: StreamRecorderEngine {
}
public func stop() {
process?.terminate()
stopRequested = true
// SIGINT (Ctrl+C) ffmpeg
if let pid = process?.processIdentifier {
kill(pid, SIGINT)
}
isRecording = false
}