简体   繁体   English

DictionaryLiteral <_,_>类型的值在强制上不符合'Any'

[英]Value of type DictionaryLiteral<_,_> does not conform to 'Any' in coersion

I am attempting to set up custom video compression in swift based off of this SO post that used Obj-C instead ( How can I reduce the file size of a video created with UIImagePickerController? ). 我试图根据使用Obj-C的SO帖子快速设置自定义视频压缩( 如何减少使用UIImagePickerController创建的视频的文件大小? )。 However, I am having a few issues converting the syntax, specifically the error above which is highlighted over the dictionary. 但是,我在转换语法时遇到一些问题,特别是上面的错误在字典中突出显示。 The compression function is below: 压缩功能如下:

func convertVideoToLowQuailty(withInputURL inputURL: URL, outputURL: URL) {
    //setup video writer
    var videoAsset = AVURLAsset(url: inputURL, options: nil)
    var videoTrack = videoAsset.tracks(withMediaType: AVMediaTypeVideo)[0]
    var videoSize = videoTrack.naturalSize
    var videoWriterCompressionSettings = [
        AVVideoAverageBitRateKey : Int(1250000)
    ]

    var videoWriterSettings : NSDictionary = [
        DictionaryLiteral : (Key: AVVideoCodecKey, Object: AVVideoCodecH264),
        AVVideoCompressionPropertiesKey : videoWriterCompressionSettings,
        AVVideoWidthKey : Int(videoSize.width),
        AVVideoHeightKey : Int(videoSize.height)
    ]

    var videoWriterInput = AVAssetWriterInput(mediaType: AVMediaTypeVideo, outputSettings: videoWriterSettings as! [String : Any?])
    videoWriterInput.expectsMediaDataInRealTime = true
    videoWriterInput.transform = videoTrack.preferredTransform
    var videoWriter = try! AVAssetWriter(outputURL: outputURL, fileType: AVFileTypeMPEG4)
    videoWriter.add(videoWriterInput)
    //setup video reader
    var videoReaderSettings = [ (kCVPixelBufferPixelFormatTypeKey as String) : Int(kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange) ]
    var videoReaderOutput = AVAssetReaderTrackOutput(track: videoTrack, outputSettings: videoReaderSettings)
    var videoReader = try! AVAssetReader(asset: videoAsset)
    videoReader.add(videoReaderOutput)
    //setup audio writer
    var audioWriterInput = AVAssetWriterInput(mediaType: AVMediaTypeAudio, outputSettings: nil)
    audioWriterInput.expectsMediaDataInRealTime = false
    videoWriter.add(audioWriterInput)
    //setup audio reader
    var audioTrack = videoAsset.tracks(withMediaType: AVMediaTypeAudio)[0]
    var audioReaderOutput = AVAssetReaderTrackOutput(track: audioTrack, outputSettings: nil)
    var audioReader = try! AVAssetReader(asset: videoAsset)
    audioReader.add(audioReaderOutput)
    videoWriter.startWriting()
    //start writing from video reader
    videoReader.startReading()
    videoWriter.startSession(atSourceTime: kCMTimeZero)
    var processingQueue = DispatchQueue(label: "processingQueue1")
    videoWriterInput.requestMediaDataWhenReady(on: processingQueue, using: {() -> Void in
        while videoWriterInput.isReadyForMoreMediaData {
            var sampleBuffer: CMSampleBuffer
            if videoReader.status == .reading && (sampleBuffer == videoReaderOutput.copyNextSampleBuffer()!) {
                videoWriterInput.append(sampleBuffer)

            }
            else {
                videoWriterInput.markAsFinished()
                if videoReader.status == .completed {
                    //start writing from audio reader
                    audioReader.startReading()
                    videoWriter.startSession(atSourceTime: kCMTimeZero)
                    var processingQueue = DispatchQueue(label: "processingQueue2")
                    audioWriterInput.requestMediaDataWhenReady(on: processingQueue, using: {() -> Void in
                        while audioWriterInput.isReadyForMoreMediaData {
                            var sampleBuffer: CMSampleBuffer
                            if audioReader.status == .reading && (sampleBuffer == (audioReaderOutput.copyNextSampleBuffer()!)) {
                                audioWriterInput.append(sampleBuffer)
                            }
                            else {
                                audioWriterInput.markAsFinished()
                                if audioReader.status == .completed {
                                    videoWriter.finishWriting(completionHandler: {() -> Void in
                                        self.sendMovieFile(at: outputURL)
                                    })
                                }
                            }
                        }
                    })
                }
            }

        }
    })
}

I do not understand why you need this line: 我不明白您为什么需要此行:

    DictionaryLiteral : (Key: AVVideoCodecKey, Object: AVVideoCodecH264),

Seeing the linked thread, you can write something like this: 看到链接的线程,您可以编写如下内容:

    var videoWriterCompressionSettings: [String: AnyObject] = [
        AVVideoAverageBitRateKey : 1250000 as NSNumber
    ]

    var videoWriterSettings : [String: AnyObject] = [
        AVVideoCodecKey: AVVideoCodecH264 as NSString,
        AVVideoCompressionPropertiesKey : videoWriterCompressionSettings as NSDictionary,
        AVVideoWidthKey : videoSize.width as NSNumber,
        AVVideoHeightKey : videoSize.height as NSNumber
    ]

    var videoWriterInput = AVAssetWriterInput(mediaType: AVMediaTypeVideo, outputSettings: videoWriterSettings)

(Someone prefers [String: Any] than [String: AnyObject] , saying it's more Swifty in Swift 3. With using Any , you can remove some castings, but may mistakingly contain some bad things which would be revealed only in runtime.) (有人更喜欢[String: Any]不是[String: AnyObject] ,说它在Swift 3中更迅速。使用Any ,您可以删除某些强制转换,但可能会误以为包含一些不好的东西,这些东西只有在运行时才能显示出来。)

And another very bad part of your code is as! [String : Any?] 代码的另一个非常糟糕的部分是as! [String : Any?] as! [String : Any?] . as! [String : Any?] You need to pass [String: Any]? 您需要传递[String: Any]? to AVAssetWriterInput.init(mediaType:outputSettings:) , not [String: Any?] . AVAssetWriterInput.init(mediaType:outputSettings:) ,而不是[String: Any?]

(There may be some other bad parts, which I have not checked...) (可能还有其他一些不良零件,我没有检查过...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM