目前,我使用SDAVAssetExportSession
https://github.com/rs/SDAVAssetExportSession项目而不是标准AVAssetExportSession
,因为SDAVAssetExportSession
授予我设置以下设置的权限
exportSessionSDA!.outputFileType = AVFileTypeMPEG4
exportSessionSDA!.shouldOptimizeForNetworkUse = true
exportSessionSDA.videoSettings = [AVVideoCodecKey: AVVideoCodecH264, AVVideoWidthKey: 800, AVVideoHeightKey: 600, AVVideoCompressionPropertiesKey: [AVVideoAverageBitRateKey: 6000000, AVVideoProfileLevelKey: AVVideoProfileLevelH264High40]]
exportSessionSDA.audioSettings = [AVFormatIDKey: kAudioFormatMPEG4AAC, AVNumberOfChannelsKey: 2, AVSampleRateKey: 44100, AVEncoderBitRateKey: 128000]
我想要标准AVAssetExportSession
中的这些设置,因为SDAVAssetExportSession
正在创建错误
SDAVAssetExportSession错误
-[AVAssetWriter startSessionAtSourceTime:] invalid parameter not satisfying: CMTIME_IS_NUMERIC(startTime)
主要是我想要的是压缩视频大小。怎么做到这一点?
录制的视频源名为mp4 ..
mycode的
func sdExport(_ videoURL: URL)
{
let avAsset1 = AVURLAsset(url: videoURL, options: nil)
print("AVURLAsset1 ",avAsset1)
let avAsset = AVURLAsset(url: ((videoURL as NSURL) as URL), options: nil)
print("AVURLAsset2 ",avAsset)
let startDate = Foundation.Date()
//Create Export session
// exportSessionSDA = SDAVAssetExportSession(asset: avAsset, presetName: AVAssetExportPresetPassthrough)
exportSessionSDA = SDAVAssetExportSession(asset: avAsset)
// exportSession = AVAssetExportSession(asset: composition, presetName: mp4Quality)
//Creating temp path to save the converted video
let documentsDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let myDocumentPath = URL(fileURLWithPath: documentsDirectory).appendingPathComponent("temp.mp4").absoluteString
let url = URL(fileURLWithPath: myDocumentPath)
let documentsDirectory2 = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as URL
let filePath = documentsDirectory2.appendingPathComponent("tempConverted/rendered-Video.mp4")
// deleteFile(filePath)
//Check if the file already exists then remove the previous file
if FileManager.default.fileExists(atPath: myDocumentPath) {
do {
try FileManager.default.removeItem(atPath: myDocumentPath)
}
catch let error {
print(error)
}
}
exportSessionSDA!.outputURL = filePath
exportSessionSDA!.outputFileType = AVFileTypeMPEG4
exportSessionSDA!.shouldOptimizeForNetworkUse = true
exportSessionSDA.videoSettings = [AVVideoCodecKey: AVVideoCodecH264, AVVideoWidthKey: 800, AVVideoHeightKey: 600, AVVideoCompressionPropertiesKey: [AVVideoAverageBitRateKey: 6000000, AVVideoProfileLevelKey: AVVideoProfileLevelH264High40]]
exportSessionSDA.audioSettings = [AVFormatIDKey: kAudioFormatMPEG4AAC, AVNumberOfChannelsKey: 2, AVSampleRateKey: 44100, AVEncoderBitRateKey: 128000]
let start = CMTimeMakeWithSeconds(0.0, 0)
let range = CMTimeRangeMake(start, avAsset.duration)
exportSessionSDA.timeRange = range
exportSessionSDA!.exportAsynchronously(completionHandler: {() -> Void in
switch self.exportSessionSDA!.status {
case .failed:
print("%@",self.exportSessionSDA?.error)
case .cancelled:
print("Export canceled")
case .completed:
//Video conversion finished
let endDate = Foundation.Date()
let time = endDate.timeIntervalSince(startDate)
print(time)
print("Successful!")
guard let data = NSData(contentsOf: (self.singleTonFile?.fileUrl)!) else {
return
}
print("File size before compression: \(Double(data.length / 1048576)) mb")
print("elf.exportSession.outputURL= ",self.exportSessionSDA.outputURL)
guard let compressedData = NSData(contentsOf: self.exportSessionSDA.outputURL!) else {
return
}
print("File size after compression: \(Double(compressedData.length / 1048576)) mb")
let when = DispatchTime.now() + 8
DispatchQueue.main.asyncAfter(deadline: when)
{
print("video_duration", self.singleTon.video_duration)
self.videoUrl() // calling upload
}
// self.mediaPath = self.exportSession.outputURL?.path as NSString!
// self.singleTon.mediaUrl = URL(self.exportSession.outputURL?.path)
// print("self.singleTon.mediaUrl ",self.singleTon.mediaUrl)
//self.mediaPath = String(self.exportSession.outputURL!)
// self.mediaPath = self.mediaPath.substringFromIndex(7)
default:
break
}
})
}