未捕获的异常 'NSInvalidArgumentException',原因:-[_SwiftValue floatValue]:无法识别的选择器发送到实例

Uncaught exception 'NSInvalidArgumentException', reason: -[_SwiftValue floatValue]: unrecognized selector sent to instance

由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因:'-[_SwiftValue floatValue]:无法识别的选择器发送到实例 0x17445bd80'

我在 swift 中尝试压缩视频时收到上述错误。我很不明白为什么原因甚至是 floatValue,因为我的两个词典中的 none 值都是浮点数。这导致我什至无法追查这个问题的根源。如果有人能指出正确的方向,我将不胜感激,下面是我用来压缩的两个函数。

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")
        }
    })
}

}

您的视频和音频设置值类型有误,您似乎调换了宽度和高度并且您的 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)
]