Skip to content

Commit

Permalink
add video scaling mode
Browse files Browse the repository at this point in the history
  • Loading branch information
YuAo committed Aug 12, 2020
1 parent 3f1495a commit ebbdb07
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Sources/VideoIO/AudioVideoSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ extension AVVideoCodecType: Codable {

public struct VideoSettings: Codable {

public struct ScalingMode: RawRepresentable {
public let rawValue: String
public init(rawValue: String) {
self.rawValue = rawValue
}
public init(_ value: String) {
self.init(rawValue: value)
}
public static let fit = ScalingMode(AVVideoScalingModeFit)
public static let resize = ScalingMode(AVVideoScalingModeResize)
public static let resizeAspect = ScalingMode(AVVideoScalingModeResizeAspect)
public static let resizeAspectFill = ScalingMode(AVVideoScalingModeResizeAspectFill)
}

public struct CompressionProperties: Codable {
public var averageBitRate: Int?
public var profileLevel: String?
Expand Down Expand Up @@ -66,6 +80,7 @@ public struct VideoSettings: Codable {
public var width: Int
public var height: Int
public var codec: AVVideoCodecType
public var scalingMode: ScalingMode?

public var compressionProperties: CompressionProperties?

Expand All @@ -92,13 +107,15 @@ public struct VideoSettings: Codable {
public static let height = CodingKeys(AVVideoHeightKey)
public static let codec = CodingKeys(AVVideoCodecKey)
public static let compressionProperties = CodingKeys(AVVideoCompressionPropertiesKey)
public static let scalingMode = CodingKeys(AVVideoScalingModeKey)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(width, forKey: .width)
try container.encode(height, forKey: .height)
try container.encode(codec, forKey: .codec)
try container.encodeIfPresent(scalingMode?.rawValue, forKey: .scalingMode)
try container.encodeIfPresent(compressionProperties, forKey: .compressionProperties)
}

Expand All @@ -107,6 +124,7 @@ public struct VideoSettings: Codable {
self.width = try container.decode(Int.self, forKey: .width)
self.height = try container.decode(Int.self, forKey: .height)
self.codec = try container.decode(AVVideoCodecType.self, forKey: .codec)
self.scalingMode = (try container.decodeIfPresent(String.self, forKey: .scalingMode)).map(ScalingMode.init(rawValue:))
self.compressionProperties = try container.decodeIfPresent(CompressionProperties.self, forKey: .compressionProperties)
}

Expand Down
8 changes: 8 additions & 0 deletions Tests/VideoIOTests/VideoIOTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ final class VideoIOTests: XCTestCase {
AVVideoHeightKey: 720,
AVVideoCodecKey: "avc1",
AVVideoCompressionPropertiesKey: [AVVideoAverageBitRateKey: 3000000, AVVideoProfileLevelKey: AVVideoProfileLevelH264HighAutoLevel]] as NSDictionary)

var videoSettings2: VideoSettings = .h264(videoSize: CGSize(width: 1280, height: 720), averageBitRate: 3000000)
videoSettings2.scalingMode = .resizeAspectFill
XCTAssert(videoSettings2.toDictionary() as NSDictionary == [AVVideoWidthKey: 1280,
AVVideoHeightKey: 720,
AVVideoCodecKey: "avc1",
AVVideoScalingModeKey: AVVideoScalingModeResizeAspectFill,
AVVideoCompressionPropertiesKey: [AVVideoAverageBitRateKey: 3000000, AVVideoProfileLevelKey: AVVideoProfileLevelH264HighAutoLevel]] as NSDictionary)
}

func testVideoExport() {
Expand Down

0 comments on commit ebbdb07

Please sign in to comment.