我是通过SwiftUI开发macOS的新手。我通过选择了MP4文件后,尝试运行FFMPEG进程NSOpenPanel
。但是,FFMPEG
回应为:
file:///Users/MyUsername/Documents/Video.mp4:无此类文件或目录
这是我的简单代码:
import SwiftUI
struct ContentView: View {
@State var selectedURL: URL?
var body: some View {
VStack {
if selectedURL != nil {
Text("Selected: \(selectedURL!.absoluteString)")
} else {
Text("Nothing selected")
}
Button(action: {
let panel = NSOpenPanel()
panel.allowedFileTypes = ["mp4"]
panel.canChooseDirectories = false
panel.canCreateDirectories = false
panel.allowsMultipleSelection = false
let result = panel.runModal()
if result == .OK {
self.selectedURL = panel.url
let savePath = self.getDownloadDirectory().appendingPathComponent("video.webm")
self.convertVideo(inputFilePath: self.selectedURL!.absoluteString, outputFilePath: savePath.absoluteString, callback: {(s) in
// omit the callback at this moment
})
}
}) {
Text("Select File")
}
}
.frame(width: 640, height: 480)
}
func getDownloadDirectory() -> URL {
let paths = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask)
return paths[0]
}
func convertVideo(inputFilePath: String, outputFilePath: String,
callback: @escaping (Bool) -> Void) -> (Process, DispatchWorkItem)? {
guard let launchPath = Bundle.main.path(forResource: "ffmpeg", ofType: "") else {
return nil
}
let process = Process()
let task = DispatchWorkItem {
process.launchPath = launchPath
process.arguments = [
"-y",
"-i", inputFilePath,
"-vcodec", "vp8",
"-acodec", "libvorbis",
"-pix_fmt", "yuva420p",
"-metadata:s:v:0",
"alpha_mode=\"1\"",
"-auto-alt-ref", "0",
outputFilePath
]
process.standardInput = FileHandle.nullDevice
process.launch()
process.terminationHandler = { process in
callback(process.terminationStatus == 0)
}
}
DispatchQueue.global(qos: .userInitiated).async(execute: task)
return (process, task)
}
}
我错过了什么让FFMPEG处理访问我选择的文件?谢谢!
尝试使用.path
代替.absoluteString
self.convertVideo(inputFilePath: self.selectedURL!.path,
outputFilePath: savePath.absoluteString, callback: {(s) in
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句