gpt4 book ai didi

swift - 索引 0 超出空 NSArray 的范围与 asset.tracks(withMediaType :)

转载 作者:行者123 更新时间:2023-11-28 13:55:32 24 4
gpt4 key购买 nike

我尝试通过获取视频的精确大小来裁剪视频,但我在 asset.tracks(withMediaType: AVMediaType.video)[0] 返回索引的行中出错0 越界为空。我确定那个 url 下有一个视频...

iOS: [NSArray0 objectAtIndex:] index 0 beyond bounds for empty NSArray with asset.tracks(withMediaType: AVMediaType.video)[0]

    // 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?

最佳答案

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

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

if acceptableVideoExtensions.contains(videoURL.pathExtension) {

// Process the video
}
else {

//Alert the user
}

关于swift - 索引 0 超出空 NSArray 的范围与 asset.tracks(withMediaType :),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53931387/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com