【发布时间】:2023-03-10 07:49:01
【问题描述】:
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[_SwiftValue floatValue]:无法识别的选择器已发送到实例 0x17445bd80”
我在尝试快速压缩视频时收到上述错误。我对为什么甚至是 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")
}
})
}
}
【问题讨论】:
-
项目是否从之前的版本转为swift 3?
标签: ios swift avfoundation