見出し画像

AVCaptureSession.Presetと動画エンコード設定

AVCaptureSessionで動画を撮影し、AVAssetWriterで動画ファイルに書き出す場合、
出力する動画ファイルのエンコーディング設定をこんな感じで AVCaptureVideoDataOutput / AVCaptureAudioDataOutput オブジェクトから取得して、

// Video
videoDataOutput.recommendedVideoSettingsForAssetWriter(writingTo: .mov)

// Audio
audioDataOutput.recommendedAudioSettingsForAssetWriter(writingTo: .mov)

AVAssetWriterInputのイニシャライザに渡す、ということをやる。

// Video
AVAssetWriterInput(mediaType: .video, outputSettings: videoSettings)

// Audio
AVAssetWriterInput(mediaType: .audio, outputSettings: audioSettings)

recommendedVideoSettingsForAssetWriter〜, recommendedAudioSettingsForAssetWriter〜 で返される設定の辞書は、AVCaptureSessionの入力の設定によって変わる。

This dictionary contains keys and values described in Video Settings and is suitable for use when creating an AVAssetWriterInput with the init(mediaType:outputSettings:) initializer. 
この辞書には、ビデオ設定で説明されているキーと値が含まれており、AVAssetWriterInputをinit(mediaType:outputSettings:)イニシャライザで作成する際に使用するのに適しています。
Note that the dictionary of settings is dependent on the current configuration of the output’s AVCaptureSession and its inputs. The settings dictionary may change if the session’s configuration changes. As such, configure your session first, then query the recommended video settings. (設定の辞書は、出力のAVCaptureSessionとその入力の現在の設定に依存することに注意してください。セッションの設定が変更されると、設定の辞書も変更される可能性があります。そのため、まずセッションを設定してから、推奨されるビデオ設定を照会してください。)

(recommendedVideoSettingsForAssetWriter〜 のドキュメントより)

AVCaptureSession に指定した AVCaptureSession.Preset と、このrecommendedな動画エンコード設定との関係を調べてみる。

## 調査条件

・使用デバイス: iPhone 12 Pro
・使用OS: iOS 15 RC
・使用カメラ: リアの `.builtInWideAngleCamera`

この条件で、各プリセット指定時のrecommendedVideoSettingsForAssetWriter(writingTo:) , recommendedAudioSettingsForAssetWriter(writingTo:) の出力を比較してみる。

## .high (AVCaptureSessionPresetHigh)

・video settings:

["AVVideoWidthKey": 1080, "AVVideoCodecKey": hvc1, "AVVideoHeightKey": 1920, "AVVideoCompressionPropertiesKey": {
   AllowFrameReordering = 1;
   AllowOpenGOP = 1;
   AverageBitRate = 7651584;
   ExpectedFrameRate = 30;
   MaxKeyFrameIntervalDuration = 1;
   MaxQuantizationParameter = 41;
   MinimizeMemoryUsage = 1;
   Priority = 80;
   ProfileLevel = "HEVC_Main_AutoLevel";
   RealTime = 1;
   RelaxAverageBitRateTarget = 1;
   SoftMinQuantizationParameter = 18;
}]

・audio settings:

["AVSampleRateKey": 44100, "AVEncoderBitRateStrategyKey": AVAudioBitRateStrategy_Variable, "AVNumberOfChannelsKey": 1, "AVFormatIDKey": 1633772320, "AVEncoderQualityForVBRKey": 91, "AVEncoderBitRatePerChannelKey": 96000]

## .medium (AVCaptureSessionPresetMedium)

・video settings:

["AVVideoHeightKey": 480, "AVVideoCompressionPropertiesKey": {
   AllowFrameReordering = 1;
   AllowOpenGOP = 1;
   AverageBitRate = 1575936;
   ExpectedFrameRate = 30;
   MaxKeyFrameIntervalDuration = 1;
   MaxQuantizationParameter = 41;
   MinimizeMemoryUsage = 1;
   Priority = 80;
   ProfileLevel = "HEVC_Main_AutoLevel";
   RealTime = 1;
   RelaxAverageBitRateTarget = 1;
   SoftMinQuantizationParameter = 18;
}, "AVVideoCodecKey": hvc1, "AVVideoWidthKey": 360]

・audio settings:

["AVFormatIDKey": 1633772320, "AVNumberOfChannelsKey": 1, "AVSampleRateKey": 44100, "AVEncoderQualityForVBRKey": 91, "AVEncoderBitRateStrategyKey": AVAudioBitRateStrategy_Variable, "AVEncoderBitRatePerChannelKey": 96000]

(コメント)
解像度が480x360...!
.highから.mediumへの落差が極端...

オーディオフォーマットは同じ。

## .hd1920x1080 (AVCaptureSessionPreset1920x1080)

・video settings:

["AVVideoCompressionPropertiesKey": {
   AllowFrameReordering = 1;
   AllowOpenGOP = 1;
   AverageBitRate = 7651584;
   ExpectedFrameRate = 30;
   MaxKeyFrameIntervalDuration = 1;
   MaxQuantizationParameter = 41;
   MinimizeMemoryUsage = 1;
   Priority = 80;
   ProfileLevel = "HEVC_Main_AutoLevel";
   RealTime = 1;
   RelaxAverageBitRateTarget = 1;
   SoftMinQuantizationParameter = 18;
}, "AVVideoWidthKey": 1080, "AVVideoHeightKey": 1920, "AVVideoCodecKey": hvc1]

・audio settings:

["AVSampleRateKey": 44100, "AVEncoderBitRatePerChannelKey": 96000, "AVFormatIDKey": 1633772320, "AVEncoderQualityForVBRKey": 91, "AVNumberOfChannelsKey": 1, "AVEncoderBitRateStrategyKey": AVAudioBitRateStrategy_Variable]

(コメント)
.high と同じっぽい
AverageBitRate も同じ

## .hd1280x720 (AVCaptureSessionPreset1280x720)

・video settings:

ここから先は

3,404字
文章やサンプルコードは多少荒削りかもしれませんが、ブログや書籍にはまだ書いていないことを日々大量に載せています。たったの400円で、すぐに購読解除してもその月は過去記事もさかのぼって読めるので、少しでも気になる内容がある方にはオトクかと思います。

技術的なメモやサンプルコード、思いついたアイデア、考えたこと、お金の話等々、頭をよぎった諸々を気軽に垂れ流しています。

最後まで読んでいただきありがとうございます!もし参考になる部分があれば、スキを押していただけると励みになります。 Twitterもフォローしていただけたら嬉しいです。 https://twitter.com/shu223/