gpt4 book ai didi

ios - AVAssetExportSession 不遵守视频合成说明

转载 作者:行者123 更新时间:2023-11-28 08:11:21 53 4
gpt4 key购买 nike

我正在尝试在视频的 AVMutableComposition 上应用 AVMutableVideoCompositionLayerInstruction。问题是当使用 AVAssetExportSession 保存视频时它不遵守指令。奇怪的是,相同的组合适用于 AVPlayer(AVPlayer 遵循指令)。

代码如下:

        let path = Bundle.main.path(forResource: "flame", ofType: "mp4")
let url = NSURL(fileURLWithPath: path!)
let asset = AVAsset(url: url as URL)

let mutableComposition = AVMutableComposition()

let type = AVMediaTypeVideo
let prefTrackID = kCMPersistentTrackID_Invalid

let sourceVideoAssetTrack: AVAssetTrack = asset.tracks(withMediaType: type).first!
let sourceAudioAssetTrack: AVAssetTrack = asset.tracks(withMediaType: AVMediaTypeAudio).first!

let videoCompositionTrack1 = mutableComposition.addMutableTrack(withMediaType: type, preferredTrackID: prefTrackID)


do {
let range = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(60,600))
try videoCompositionTrack1.insertTimeRange(range, of: sourceVideoAssetTrack, at: kCMTimeZero)
}catch { print(error) }

let firstTransform = videoCompositionTrack1.preferredTransform;

let fromLayer = AVMutableVideoCompositionLayerInstruction(assetTrack: videoCompositionTrack1)
fromLayer.setTransform(firstTransform, at: kCMTimeZero)
fromLayer.setCropRectangle(CGRect.init(x: 5, y: 5, width: 200, height: 200), at: kCMTimeZero)

let instruction = AVMutableVideoCompositionInstruction()
instruction.layerInstructions = [fromLayer]
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(60,600))

videoComposition = AVMutableVideoComposition()
videoComposition!.instructions = [instruction]
videoComposition!.renderSize = CGSize.init(width: 300, height: 300)
videoComposition!.frameDuration = CMTimeMake(1, 30)


if(true){ // just to switch between the saving and playing modes
var exportPath: NSString = NSTemporaryDirectory().appendingFormat("/video.mov")
var exportUrl: NSURL = NSURL.fileURL(withPath: exportPath as String) as NSURL

var exporter = AVAssetExportSession(asset: mutableComposition, presetName: AVAssetExportPresetMediumQuality)!
exporter.outputURL = exportUrl as URL
exporter.videoComposition = videoComposition!

exporter.outputFileType = AVFileTypeMPEG4
exporter.shouldOptimizeForNetworkUse = true
exporter.canPerformMultiplePassesOverSourceMediaData = true

exporter.exportAsynchronously(completionHandler: {
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: exportUrl as URL)
}) { completed, error in
if completed {
print("Video is saved!")
}
}
})
}
else{
let playerItem = AVPlayerItem(asset: mutableComposition)
playerItem.videoComposition = videoComposition!
player = AVPlayer(playerItem: playerItem)
playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.frame
self.view.layer.addSublayer(playerLayer)
player.play()
}

AVPlayer honours cropRectangle instruction

AVPlayer 遵循如上所示的 cropRectangle 指令

Saved video is same as the original video

保存的视频与原始视频相同。

我在 iOS 9 上构建这个。我做错了什么?

最佳答案

AVMutableVideoCompositionLayerInstruction 上应用转换时,您可以从 AVAssetpreferredTransform

中获得所需的转换

编辑:事实证明这是现有文件的导出错误,要么在尝试写入时使用唯一名称,例如

String(Date) + ".mov"

或者在尝试写入之前删除

关于ios - AVAssetExportSession 不遵守视频合成说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43676210/

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