有没有办法在Iphone上播放3gpp视频?



我从Twilio服务器获得的视频是3gpp视频格式,当我试图将3gpp视频转换为mp4时,它没有转换。

为了将视频转换为mp4,我使用以下代码。

func encodeVideo(videoURL: URL){
let avAsset = AVURLAsset(url: videoURL)
let startDate = Date()
let exportSession = AVAssetExportSession(asset: avAsset, presetName: AVAssetExportPresetPassthrough)

let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let myDocPath = NSURL(fileURLWithPath: docDir).appendingPathComponent("temp.mp4")?.absoluteString

let docDir2 = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as NSURL

let filePath = docDir2.appendingPathComponent("rendered-Video.mp4")
deleteFile(filePath!)

if FileManager.default.fileExists(atPath: myDocPath!){
do{
try FileManager.default.removeItem(atPath: myDocPath!)
}catch let error{
print(error)
}
}

exportSession?.outputURL = filePath
exportSession?.outputFileType = AVFileType.mp4
exportSession?.shouldOptimizeForNetworkUse = true

let start = CMTimeMakeWithSeconds(0.0, preferredTimescale: 0)
let range = CMTimeRange(start: start, duration: avAsset.duration)
exportSession?.timeRange = range

exportSession!.exportAsynchronously{() -> Void in
switch exportSession!.status{
case .failed:
print("(exportSession!.error!)")
case .cancelled:
print("Export cancelled")
case .completed:
let endDate = Date()
let time = endDate.timeIntervalSince(startDate)
print(time)
print("Successful")
print(exportSession?.outputURL ?? "")
default:
break
}

}
}
func deleteFile(_ filePath:URL) {
guard FileManager.default.fileExists(atPath: filePath.path) else{
return
}
do {
try FileManager.default.removeItem(atPath: filePath.path)
}catch{
fatalError("Unable to delete file: (error) : (#function).")
}
}

}

当使用这个代码时,我得到这个错误。

AVFoundationErrorDomain Code=-11838 "操作停止"UserInfo={nslocalizedfailurerreason =该媒体不支持该操作。, NSLocalizedDescription=Operation Stopped, NSUnderlyingError=0x2834f0240{错误域=NSOSStatusErrorDomain Code=-16976 "(null)"}}

我遵循的转换步骤:

  1. 首先我从Twilio URL下载视频
  2. 将视频保存到文档目录
  3. ,然后传递上述函数中的文档目录视频URL。

您需要使用AVAssetExportSession将视频转换为.mp4格式,下面的方法将.3gpp格式的视频转换为.mp4

检查exportSession?.outputFileType = .mp4行。它指定了视频的输出格式。

这里inputURL是需要转换的视频的url,outputURL是视频的最终目的地。

还有一点,不要忘记在outputURL视频文件中指定.mp4扩展名

let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
let filePath = URL(fileURLWithPath: documentsDirectory).appendingPathComponent("Video.mp4").absoluteString
let outputURL = URL(fileURLWithPath: filePath)
convertVideoToLowQuailty(withInputURL: inputUrl, outputURL: outputURL, handler: { exportSession in
if exportSession?.status == .completed {
// Video conversation completed
}
})
func convertVideoToLowQuailty(withInputURL inputURL: URL?, outputURL: URL?, handler: @escaping (AVAssetExportSession?) -> Void) {
if let anURL = outputURL {
try? FileManager.default.removeItem(at: anURL)
}
var asset: AVURLAsset? = nil
if let anURL = inputURL {
asset = AVURLAsset(url: anURL, options: nil)
}
var exportSession: AVAssetExportSession? = nil
if let anAsset = asset {
exportSession = AVAssetExportSession(asset: anAsset, presetName: AVAssetExportPresetPassthrough)
}
exportSession?.outputURL = outputURL
exportSession?.outputFileType = .mp4
exportSession?.exportAsynchronously(completionHandler: {
handler(exportSession)
})
}

最新更新