AVFoundationシリーズ7:シンプルなビデオフォーマット変換



Avfoundation Series 7



フォーマット変換、使用済みAVAssetExportSessionこのクラス

1.リソースを取得し、AVAssetを作成します



guard let path = Bundle.main.path(forResource: '1', ofType: 'mp4') else { return } let url = URL.init(fileURLWithPath: path) let anAsset = AVAsset.init(url: url)

2.出力タイプと出力URLを構成します

let outputURL = getVideoExportURL() let preset = AVAssetExportPresetHighestQuality let outFileType = AVFileType.mov

3.互換性を確認します。特定の条件下では、正常に変換されない場合があります。



print('Determining compatibility') AVAssetExportSession.determineCompatibility(ofExportPreset: preset, with: anAsset, outputFileType: outFileType, completionHandler: { (isCompatible) in if !isCompatible { return }}) print('Compatible')

4.エクスポートを決定します

guard let export = AVAssetExportSession(asset: anAsset, presetName: preset) else { return } export.outputFileType = outFileType export.outputURL = outputURL export.exportAsynchronously { () -> Void in // Handle export results. print('Conversion completed(export.status.rawValue)') }

デモ: https://github.com/quinn0809/Learn_AVFoundation