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