Make audio/video settings Hashable, Sendable, and Codable

This commit is contained in:
Sami Samhuri 2024-10-18 15:51:56 -07:00
parent 6a6938817b
commit baf7d5e72a
No known key found for this signature in database
3 changed files with 14 additions and 16 deletions

View file

@ -5,12 +5,12 @@
// Created by Sami Samhuri on 2024-07-07. // Created by Sami Samhuri on 2024-07-07.
// //
public import AVFoundation import AVFoundation
/// A convenient API for constructing audio settings dictionaries. /// A convenient API for constructing audio settings dictionaries.
/// ///
/// Construct this by starting with ``AudioOutputSettings/default`` or ``AudioOutputSettings/format(_:)`` and then chain calls to further customize it, if desired, using ``channels(_:)``, ``sampleRate(_:)``, and ``mix(_:)``. /// Construct this by starting with ``AudioOutputSettings/default`` or ``AudioOutputSettings/format(_:)`` and then chain calls to further customize it, if desired, using ``channels(_:)``, and ``sampleRate(_:)``.
public struct AudioOutputSettings { public struct AudioOutputSettings: Hashable, Sendable, Codable {
/// Describes the output file format. /// Describes the output file format.
public enum Format { public enum Format {
/// Advanced Audio Codec. The audio format typically used for MPEG-4 audio. /// Advanced Audio Codec. The audio format typically used for MPEG-4 audio.
@ -29,7 +29,6 @@ public struct AudioOutputSettings {
let format: AudioFormatID let format: AudioFormatID
let channels: Int let channels: Int
let sampleRate: Int? let sampleRate: Int?
let mix: AVAudioMix?
/// Specifies the AAC format with 2 channels at a 44.1 KHz sample rate. /// Specifies the AAC format with 2 channels at a 44.1 KHz sample rate.
public static var `default`: AudioOutputSettings { public static var `default`: AudioOutputSettings {
@ -38,19 +37,15 @@ public struct AudioOutputSettings {
/// Specifies the given format with 2 channels. /// Specifies the given format with 2 channels.
public static func format(_ format: Format) -> AudioOutputSettings { public static func format(_ format: Format) -> AudioOutputSettings {
.init(format: format.formatID, channels: 2, sampleRate: nil, mix: nil) .init(format: format.formatID, channels: 2, sampleRate: nil)
} }
public func channels(_ channels: Int) -> AudioOutputSettings { public func channels(_ channels: Int) -> AudioOutputSettings {
.init(format: format, channels: channels, sampleRate: sampleRate, mix: mix) .init(format: format, channels: channels, sampleRate: sampleRate)
} }
public func sampleRate(_ sampleRate: Int?) -> AudioOutputSettings { public func sampleRate(_ sampleRate: Int?) -> AudioOutputSettings {
.init(format: format, channels: channels, sampleRate: sampleRate, mix: mix) .init(format: format, channels: channels, sampleRate: sampleRate)
}
public func mix(_ mix: sending AVAudioMix?) -> AudioOutputSettings {
.init(format: format, channels: channels, sampleRate: sampleRate, mix: mix)
} }
public var settingsDictionary: [String: any Sendable] { public var settingsDictionary: [String: any Sendable] {

View file

@ -30,6 +30,8 @@ public final class ExportSession: Sendable {
- audio: Optional audio settings using ``AudioOutputSettings``. Defaults to ``AudioOutputSettings/default``. - audio: Optional audio settings using ``AudioOutputSettings``. Defaults to ``AudioOutputSettings/default``.
- mix: An optional mix that can be used to manipulate the audio in some way.
- video: Video settings using ``VideoOutputSettings``. - video: Video settings using ``VideoOutputSettings``.
- outputURL: The file `URL` where the exported video will be written. - outputURL: The file `URL` where the exported video will be written.
@ -44,6 +46,7 @@ public final class ExportSession: Sendable {
metadata: sending [AVMetadataItem] = [], metadata: sending [AVMetadataItem] = [],
timeRange: CMTimeRange? = nil, timeRange: CMTimeRange? = nil,
audio: sending AudioOutputSettings = .default, audio: sending AudioOutputSettings = .default,
mix: sending AVAudioMix? = nil,
video: sending VideoOutputSettings, video: sending VideoOutputSettings,
to outputURL: URL, to outputURL: URL,
as fileType: AVFileType as fileType: AVFileType
@ -54,7 +57,7 @@ public final class ExportSession: Sendable {
let sampleWriter = try await SampleWriter( let sampleWriter = try await SampleWriter(
asset: asset, asset: asset,
audioOutputSettings: audio.settingsDictionary, audioOutputSettings: audio.settingsDictionary,
audioMix: audio.mix, audioMix: mix,
videoOutputSettings: video.settingsDictionary, videoOutputSettings: video.settingsDictionary,
videoComposition: videoComposition, videoComposition: videoComposition,
timeRange: timeRange, timeRange: timeRange,

View file

@ -12,9 +12,9 @@ import AVFoundation
/// Construct this by starting with ``VideoOutputSettings/codec(_:size:)`` or ``VideoOutputSettings/codec(_:width:height:)`` and then chaining calls to further customize it, if desired, using ``fps(_:)``, ``bitrate(_:)``, and ``color(_:)``. /// Construct this by starting with ``VideoOutputSettings/codec(_:size:)`` or ``VideoOutputSettings/codec(_:width:height:)`` and then chaining calls to further customize it, if desired, using ``fps(_:)``, ``bitrate(_:)``, and ``color(_:)``.
/// ///
/// Setting the fps and colour also needs support from the `AVVideoComposition` and these settings can be applied to them with ``VideoOutputSettings/apply(to:)``. /// Setting the fps and colour also needs support from the `AVVideoComposition` and these settings can be applied to them with ``VideoOutputSettings/apply(to:)``.
public struct VideoOutputSettings { public struct VideoOutputSettings: Hashable, Sendable, Codable {
/// Describes an H.264 encoding profile. /// Describes an H.264 encoding profile.
public enum H264Profile { public enum H264Profile: Hashable, Sendable, Codable {
case baselineAuto, baseline30, baseline31, baseline41 case baselineAuto, baseline30, baseline31, baseline41
case mainAuto, main31, main32, main41 case mainAuto, main31, main32, main41
case highAuto, high40, high41 case highAuto, high40, high41
@ -37,7 +37,7 @@ public struct VideoOutputSettings {
} }
/// Specifies the output codec. /// Specifies the output codec.
public enum Codec { public enum Codec: Hashable, Sendable, Codable {
/// H.264 using the associated encoding profile. /// H.264 using the associated encoding profile.
case h264(H264Profile) case h264(H264Profile)
/// HEVC / H.265 /// HEVC / H.265
@ -64,7 +64,7 @@ public struct VideoOutputSettings {
} }
/// Specifies whether to use Standard Dynamic Range or High Dynamic Range colours. /// Specifies whether to use Standard Dynamic Range or High Dynamic Range colours.
public enum Color { public enum Color: Hashable, Sendable, Codable {
/// Standard dynamic range colours (BT.709 which roughly corresponds to SRGB) /// Standard dynamic range colours (BT.709 which roughly corresponds to SRGB)
case sdr case sdr
/// High dynamic range colours (BT.2020) /// High dynamic range colours (BT.2020)