我想将.mkv文件转换为.mp4文件,我搜索了很多但没有得到任何解决方案。
我使用下面的代码进行转换,但代码不起作用,总是收到以下错误。
"导出失败:无法打开"
func convertVideoToMP4(_ videoURL: URL) {
let avAsset = AVURLAsset(url: videoURL, options: nil)
let startDate = Foundation.Date()
//Create Export session
let exportSession = AVAssetExportSession(asset: avAsset, presetName: AVAssetExportPresetPassthrough)
let documentsDirectory2 = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as URL
let filePath = documentsDirectory2.appendingPathComponent("rendered-Video.mp4")
deleteFile(filePath)
exportSession!.outputURL = filePath
exportSession!.outputFileType = AVFileTypeMPEG4
exportSession!.shouldOptimizeForNetworkUse = true
let start = CMTimeMakeWithSeconds(0.0, 0)
let range = CMTimeRangeMake(start, avAsset.duration)
exportSession?.timeRange = range
exportSession!.exportAsynchronously(completionHandler: {() -> Void in
switch exportSession!.status {
case .failed:
print("Export failed: \(String(describing: exportSession?.error?.localizedDescription))")
case .cancelled:
print("Export canceled")
case .completed:
//Video conversion finished
print("Successful!")
default:
break
}
})
}