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

时间:2019-09-02 07:13:08

标签: ios swift video-capture avassetexportsession

是否可以导出分辨率为 480 x 960 的视频?我知道有用于此的库,但我宁愿在可能的情况下也不要在项目上安装更多Pod。

我正在将.MOV中捕获的视频转换为.MP4​​。我使用了此thread上建议的方法。

AVAssetExport中的可用选项包括:

AVAssetExportPresetLowQuality  
AVAssetExportPresetMediumQuality 
AVAssetExportPresetHighestQuality 
AVAssetExportPresetHEVCHighestQuality  
AVAssetExportPreset640x480 
AVAssetExportPreset960x540  
AVAssetExportPreset1280x720 
AVAssetExportPreset1920x1080  
AVAssetExportPreset3840x2160

如果导出的视频是MP4,这是正确的方法吗? 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)
    }
}

1 个答案:

答案 0 :(得分:0)

您必须将视频记录和视频合成转换为导出会话...

您必须执行以下操作:

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