ios - 未捕获的异常 'NSInvalidArgumentException',原因 : -[_SwiftValue floatValue]: unrecognized selector sent to instance

标签 ios swift avfoundation

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

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

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)
]

关于ios - 未捕获的异常 'NSInvalidArgumentException',原因 : -[_SwiftValue floatValue]: unrecognized selector sent to instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39822974/

相关文章:

ios - AVCaptureVideoPreviewLayer 看起来是 iPhone 4 大小

ios - 使用 AVFoundation 随机播放 mp3

ios - Swift 3 尝试将音频路径/音频文件传递给音频播放器 VC

ios - 控制 Sprite 套件中的最大速度

ios - 是否可以更改搜索栏中取消按钮的垂直位置?

objective-c - 任何人都有如何使用 AVAssetWriter 同时编写音频和视频的示例?

ios - 没有der文件的RSA加密

ios - 本地化 Storyboard的修改 - 转换为基础 Storyboard

ios - UICollectionView : How do I implement auto-resizing of UICollectionViewCell in terms of its height?

ios - XCode 8 无法编译适用于 Xcode 9 的 Swift 3 代码