OStack程序员社区-中国程序员成长平台

标题: ios - 具有修改的分辨率设置的 AVAssetExportSession [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 19:24
标题: ios - 具有修改的分辨率设置的 AVAssetExportSession

AVAssetExportSession 将预设作为其初始化参数之一:

AVAssetExportSession(asset: AVAsset, presetName: String)

预设是 AVAssetExportPreset640x480AVAssetExportPreset1920x1080 等设置。但是,如果我想使用自定义分辨率(比如 250x400)进行编码,有没有办法做到这一点,如果有,该怎么做?



Best Answer-推荐答案


这些导出选项已定义,无法让您使用自定义分辨率进行编码。或者,您可以尝试这种方法

func exportVideo(asset:AVAsset, renderedWidth: CGFloat, renderedHeight: CGFloat, exportCompletionHandler: (() -> Void)?) {
        let videoTrack: AVAssetTrack = asset.tracksWithMediaType(AVMediaTypeVideo)[0]

        let videoComposition = AVMutableVideoComposition()
        videoComposition.frameDuration = CMTimeMake(1, 30)
        videoComposition.renderSize = CGSizeMake(renderedWidth, renderedHeight)

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

        let transformer: AVMutableVideoCompositionLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: videoTrack);
        //Apply any transformer if needed
        //

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

        //Create export path
        let exportPath: NSURL = NSURL(fileURLWithPath: "export_path_here")
        //

        let exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHighestQuality)
        exporter?.videoComposition = videoComposition
        exporter?.outputURL = exportPath
        exporter?.outputFileType = AVFileTypeQuickTimeMovie

        exporter?.exportAsynchronouslyWithCompletionHandler({ () -> Void in
            //Do sth when finished
            if let handler = exportCompletionHandler {
                handler()
            }
        })
    }

我希望这会有所帮助。

引用:https://www.one-dreamer.com/cropping-video-square-like-vine-instagram-xcode/

关于ios - 具有修改的分辨率设置的 AVAssetExportSession,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36730710/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4