fix: omit -t flag when duration=0; use SIGINT for graceful stop
This commit is contained in:
parent
71d321b056
commit
7d89a332ff
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user