带有asset.tracks(withMediaType :)的空NSArray的索引0超出范围

Posted

技术标签:

【中文标题】带有asset.tracks(withMediaType :)的空NSArray的索引0超出范围【英文标题】:index 0 beyond bounds for empty NSArray with asset.tracks(withMediaType:) 【发布时间】:2018-12-26 11:21:30 【问题描述】:

我尝试通过精确尺寸来裁剪视频,但 asset.tracks(withMediaType: AVMediaType.video)[0] 行出错,返回索引0 超出范围为空。我确切地说该网址下有一个视频...

ios: [NSArray0 objectAtIndex:] index 0 超出了带有asset.tracks(withMediaType: AVMediaType.video)[0]的空NSArray的范围

    // input file
            let composition = AVMutableComposition()
            composition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid)

            // input clip
            let asset = AVAsset(url: URL(fileURLWithPath: video))

            let videoTrack: AVAssetTrack = asset.tracks(withMediaType: AVMediaType.video)[0]  
//=> problem is here

            // crop clip to screen ratio
            let orientation: UIInterfaceOrientation = self.orientation(forTrack: asset)

            // make render size square
            let videoComposition = AVMutableVideoComposition.init()
            let height: CGFloat = 960
            let width: CGFloat = 960

            videoComposition.renderSize = CGSize(width: CGFloat(width), height: CGFloat(height))
            videoComposition.frameDuration = CMTimeMake(1, 30)

            let instruction = AVMutableVideoCompositionInstruction()
            instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(60, 30))

            // rotate and position video
            let transformer = AVMutableVideoCompositionLayerInstruction(assetTrack: videoTrack)

            var txWidth: CGFloat = (videoTrack.naturalSize.width - width) / 2
            let txHeight: CGFloat = (videoTrack.naturalSize.height - height) / 2

            if orientation == .portrait || orientation == .landscapeRight 
                // invert translation
                txWidth *= -1
            

            // t1: rotate and position video since it may have been cropped to screen ratio
            let t1: CGAffineTransform = videoTrack.preferredTransform.translatedBy(x: txWidth, y: -txHeight)

            transformer.setTransform(t1, at: kCMTimeZero)


            instruction.layerInstructions = [transformer]
            videoComposition.instructions = [instruction] as [AVVideoCompositionInstructionProtocol]

            // export
            let exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHighestQuality)
            exporter?.videoComposition = videoComposition
            exporter?.outputURL = URL(fileURLWithPath: video)
            exporter?.outputFileType = .mov

            exporter?.exportAsynchronously(completionHandler: 
                print("Exporting done!")
            )

Any ideas?

【问题讨论】:

***.com/questions/29200418/… @SPatel 这太笼统了 【参考方案1】:

在处理视频时,您需要确保所有导入的文件都具有可接受的扩展名,例如这样做

let acceptableVideoExtensions = ["mov", "mp4", "m4v"]

if acceptableVideoExtensions.contains(videoURL.pathExtension) 

   // Process the video

else 

    //Alert the user

【讨论】:

以上是关于带有asset.tracks(withMediaType :)的空NSArray的索引0超出范围的主要内容,如果未能解决你的问题,请参考以下文章

带有多个链接的 NSAttributedString 的 UILabel,带有行限制,显示尾部截断,带有未见文本的 NSBackgroundColorAttributeName

使用带有 uuencode 的“sendmail”发送邮件,并带有主题

带有和不带有聚合的 sql 查询

如何翻转正面带有标签而背面带有另一个标签的视图 - 参见图片

CakePHP 如何处理带有/不带有 'id' 字段的 HABTM 表?

带有滚动的 Div 和带有绝对位置的内容