由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [_ SwiftValue floatValue]:无法识别的选择器发送到实例0x17445bd80'
我在尝试使用swift压缩视频时收到上述错误。我很遗憾,为什么原因甚至是一个floatValue,因为我的任何一个字典中的值都不是浮点数。这导致我甚至无法追查这个问题的根源。如果有人能指出我正确的方向我会非常感激,下面是我用来压缩的两个函数。
func compressVideo(_ inputURL: URL, outputURL: URL, handler:@escaping (_ session: SDAVAssetExportSession)-> Void)
{
do{
let fileLocation = URL(fileURLWithPath: inputURL.path)
let videoData = try Data(contentsOf: fileLocation)
print(" \n BEFORE COMPRESSION: " + mbSizeWithData(data: videoData) + "\n")
} catch {}
let urlAsset = AVURLAsset(url: inputURL, options: nil)
//let exportSession = AVAssetExportSession(asset: urlAsset, presetName: AVAssetExportPresetHighestQuality)
let exportSession = SDAVAssetExportSession(asset: urlAsset)
exportSession?.outputURL = outputURL
exportSession?.videoSettings =
[(AVVideoCodecKey as NSString) as String : AVVideoCodecH264 as NSString
, AVVideoWidthKey : 1080 as Int64,
AVVideoHeightKey : 1920 as Int64,
AVVideoCompressionPropertiesKey : [AVVideoAverageBitRateKey as NSString: 100 as Int64,
AVVideoProfileLevelKey as NSString: AVVideoProfileLevelH264Main31, AVVideoMaxKeyFrameIntervalKey as NSString: 30 as Int64]]
exportSession?.audioSettings = [
AVFormatIDKey : kAudioFormatMPEG4AAC,
AVNumberOfChannelsKey : 2 as Int64,
AVSampleRateKey : 44100 as Int64,
AVEncoderBitRateKey : 128000 as Int64
]
exportSession?.outputFileType = AVFileTypeMPEG4
exportSession?.shouldOptimizeForNetworkUse = true
exportSession?.exportAsynchronously { () -> Void in
handler(exportSession!)
}
}
func doCompress() {
self.url = UserDefaults.standard.url(forKey: "videoURL")
print("\(self.url)")
let format = DateFormatter()
format.dateFormat="yyyy-MM-dd-HH-mm-ss"
let outputURl = self.url!.deletingLastPathComponent().appendingPathComponent("video\(format.string(from: Date())).mp4")
print("\(outputURl)")
self.compressVideo(self.url!, outputURL: outputURl, handler: { (session) in
if session.status == AVAssetExportSessionStatus.completed
{
//DEBUG :
let tempData = try? Data(contentsOf: outputURl)
print("\n AFTER COMPRESSION: " + mbSizeWithData(data: tempData!) + "\n")
self.url! = outputURl
print(self.url)
let data = try? Data(contentsOf: self.url!)
UserDefaults.standard.set(self.url, forKey: "videoURL")
UserDefaults.standard.synchronize()
print("File size after compression: \(Double(data!.count / 1048576)) mb")
self.videoData = try? Data(contentsOf: self.url!)
//print(self.videoData)
}
else if session.status == AVAssetExportSessionStatus.failed
{
print("failed")
}
})
}
}
答案 0 :(得分:0)
您的视频和音频设置值类型错误,看起来您切换了宽度和高度,AVVideoAverageBitRateKey
看起来太低了。此外,我不确定AVVideoProfileLevelH264Main31
在任何地方都得到支持:
exportSession?.videoSettings = [
AVVideoCodecKey: AVVideoCodecH264,
AVVideoWidthKey : NSNumber(value:1920),
AVVideoHeightKey : NSNumber(value: 1080),
AVVideoCompressionPropertiesKey : [
AVVideoAverageBitRateKey: NSNumber(value:100),
// AVVideoProfileLevelKey: AVVideoProfileLevelH264Main31,
AVVideoMaxKeyFrameIntervalKey: NSNumber(value:30)
]
]
exportSession?.audioSettings = [
AVFormatIDKey : NSNumber(value:kAudioFormatMPEG4AAC),
AVNumberOfChannelsKey : NSNumber(value:2),
AVSampleRateKey : NSNumber(value:44100.0),
AVEncoderBitRateKey : NSNumber(value:128000)
]