ios - Video converted from mov to mp4 on iOS can not be played on browser etc
问题描述
I wanna to convert .mov
to .mp4
on iOS.
I could convert it to .mp4
, but I can not play the converted file on a browser such as Chrome.
Playable on iOS and mac Safari.
Here is the code for conversion.
private func encodeVideo(at avAsset: AVURLAsset, completionHandler: ((URL?, Error?) -> Void)?) {
let startDate = Date()
//Create Export session
guard let exportSession = AVAssetExportSession(asset: avAsset, presetName: AVAssetExportPresetPassthrough) else {
completionHandler?(nil, nil)
return
}
//Creating temp path to save the converted video
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as URL
let filePath = documentsDirectory.appendingPathComponent("rendered-Video.mp4")
//Check if the file already exists then remove the previous file
if FileManager.default.fileExists(atPath: filePath.path) {
do {
try FileManager.default.removeItem(at: filePath)
} catch {
completionHandler?(nil, error)
}
}
exportSession.outputURL = filePath
exportSession.outputFileType = .mp4
exportSession.shouldOptimizeForNetworkUse = true
let start = CMTimeMakeWithSeconds(0.0, preferredTimescale: 0)
let range = CMTimeRangeMake(start: start, duration: avAsset.duration)
exportSession.timeRange = range
exportSession.exportAsynchronously(completionHandler: {() -> Void in
switch exportSession.status {
case .failed:
print(exportSession.error ?? "NO ERROR")
completionHandler?(nil, exportSession.error)
case .cancelled:
print("Export canceled")
completionHandler?(nil, nil)
case .completed:
//Video conversion finished
let endDate = Date()
let time = endDate.timeIntervalSince(startDate)
print(time)
print("Successful!")
print(exportSession.outputURL ?? "NO OUTPUT URL")
completionHandler?(exportSession.outputURL, nil)
default:
break
}
})
}
How can I convert it to playable mp4 anywhere?
解决方案
尝试使用另一个AVAssetExportPreset
,根据此线程AVAssetExportSession 使用 AVAssetExportPresetPassthrough 破坏输出具有已知的兼容性错误。
在此处阅读更多信息:https ://developer.apple.com/documentation/avfoundation/avassetexportsession
推荐阅读
- python - 用随机名称填充数组
- php - 如何将ajax数据显示到while循环中?
- filepond - 如何在filepond中获取还原文件的原始属性
- excel - 查找和删除具有多个条件的行
- python - 使用海龟模块创建函数时,Python 说我的海龟名称变量未定义
- c++ - C++ 2 骰子滚动 1000 万次 BEGINNER
- react-native - 如何在加载时将“动态”值从商店加载到 TextInput 并处理其 onChangeText?
- php - 如果只定义了这行代码代码,它是如何执行的?
- karate - 空手道:获取所有用户并使用参考 id 删除
- javascript - 将 nl2br 移植到 Javascript/React - 如何手动转义文本?