简体   繁体   English

如何在Swift中更改视频分辨率

[英]How to change video resolution in Swift

Is there a way to export a video with resolution 480 x 960 ? 有没有办法导出分辨率为480 x 960的视频? I know there are libraries for this but I'd rather do it without installing more pods on my project if possible. 我知道有用于此的库,但我希望在可能的情况下不要在我的项目上安装更多的Pod。

I am converting a captured video in .MOV to .MP4. 我正在将.MOV中的捕获视频转换为.MP4​​。 I used the method suggested on this thread . 我使用了此线程上建议的方法。

The available options from AVAssetExport are these: AVAssetExport的可用选项包括:

AVAssetExportPresetLowQuality  
AVAssetExportPresetMediumQuality 
AVAssetExportPresetHighestQuality 
AVAssetExportPresetHEVCHighestQuality  
AVAssetExportPreset640x480 
AVAssetExportPreset960x540  
AVAssetExportPreset1280x720 
AVAssetExportPreset1920x1080  
AVAssetExportPreset3840x2160

Is this the correct approach if the exported video is MP4? 如果导出的视频是MP4,这是正确的方法吗? The documentation for AVAssetExportSession says this is for quicktime movies so I am a bit confused about this. AVAssetExportSession的文档说这是用于快速电影的,所以对此我有些困惑。

    func exportVideo(inputurl: URL,
                 presetName: String = AVAssetExportPresetHighestQuality,
                 outputFileType: AVFileType = .mp4,
                 fileExtension: String = "mp4",
                 then completion: @escaping (URL?) -> Void)
{
    let asset = AVAsset(url: inputurl)


    let filename = filePath.deletingPathExtension().appendingPathExtension(fileExtension).lastPathComponent
    outputURL = FileManager.default.temporaryDirectory.appendingPathComponent(filename)

    if let session = AVAssetExportSession(asset: asset, presetName: presetName) {
        session.outputURL = outputURL
        session.outputFileType = outputFileType

        session.shouldOptimizeForNetworkUse = true
        session.exportAsynchronously {
            switch session.status {
            case .completed:
                completion(self.outputURL)
            case .cancelled:
                debugPrint("Video export cancelled.")
                completion(nil)
            case .failed:
                let errorMessage = session.error?.localizedDescription ?? "n/a"
                debugPrint("Video export failed with error: \(errorMessage)")
                completion(nil)
            default:
                break
            }
        }
    } else {
        completion(nil)
    }
}

You must transform video tranck and video composition to your export session... 您必须将视频记录和视频合成转换为导出会话...

You have to do something like this: 您必须执行以下操作:

    //transform video
    let rotationTransform = CGAffineTransform(rotationAngle: .pi)
    videoTrack.preferredTransform = rotationTransform;

    let layerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: videoTrack)
    layerInstruction.setTransform(videoAssetTrack.preferredTransform, at: kCMTimeZero)

    let videoCompositionInstruction = AVMutableVideoCompositionInstruction()
    videoCompositionInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
    videoCompositionInstruction.layerInstructions = [layerInstruction]

    let videoComposition = AVMutableVideoComposition()
    videoComposition.instructions = [videoCompositionInstruction]
    videoComposition.frameDuration = CMTime(value: 1, timescale: 30)
    videoComposition.renderSize = videoSize

    //saving...
    guard let exportSession = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetMediumQuality) else { return }
    //exportSession.outputURL = your output url
    exportSession.videoComposition = videoComposition

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

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