Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a bitrate command line argument to control bitrate of output video #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions Sources/Upscaling/UpscalingExportSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class UpscalingExportSession {
outputCodec: AVVideoCodecType? = nil,
preferredOutputURL: URL,
outputSize: CGSize,
outputBitRate: Int? = nil,
creator: String? = nil
) {
self.asset = asset
Expand All @@ -23,6 +24,7 @@ public class UpscalingExportSession {
outputURL = preferredOutputURL
}
self.outputSize = outputSize
self.outputBitRate = outputBitRate
self.creator = creator
progress = Progress(parent: nil, userInfo: [
.fileURLKey: outputURL
Expand All @@ -42,6 +44,7 @@ public class UpscalingExportSession {
public let outputCodec: AVVideoCodecType?
public let outputURL: URL
public let outputSize: CGSize
public let outputBitRate: Int?
public let creator: String?

public let progress: Progress
Expand Down Expand Up @@ -80,7 +83,8 @@ public class UpscalingExportSession {
for: track,
formatDescription: formatDescription,
outputSize: outputSize,
outputCodec: outputCodec
outputCodec: outputCodec,
outputBitRate: outputBitRate
) else { continue }

if assetReader.canAdd(assetReaderOutput) {
Expand Down Expand Up @@ -275,7 +279,8 @@ public class UpscalingExportSession {
for track: AVAssetTrack,
formatDescription: CMFormatDescription?,
outputSize: CGSize,
outputCodec: AVVideoCodecType?
outputCodec: AVVideoCodecType?,
outputBitRate: Int?
) async throws -> AVAssetWriterInput? {
switch track.mediaType {
case .video:
Expand All @@ -297,6 +302,11 @@ public class UpscalingExportSession {
formatDescription?.hasLeftAndRightEye ?? false {
var compressionProperties: [CFString: Any] = [:]
compressionProperties[kVTCompressionPropertyKey_MVHEVCVideoLayerIDs] = [0, 1]

if let outputBitRate {
compressionProperties[AVVideoAverageBitRateKey as CFString] = outputBitRate
}

if let extensions = formatDescription?.extensions {
for key in [
kVTCompressionPropertyKey_HeroEye,
Expand Down
2 changes: 2 additions & 0 deletions Sources/fx-upscale/FXUpscale.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Upscaling

@Option(name: .shortAndLong, help: "The output file width") var width: Int?
@Option(name: .shortAndLong, help: "The output file height") var height: Int?
@Option(name: .shortAndLong, help: "The output file bitrate") var bitrate: Int?

mutating func run() async throws {
guard ["mov", "m4v", "mp4"].contains(url.pathExtension.lowercased()) else {
Expand Down Expand Up @@ -60,6 +61,7 @@ import Upscaling
outputCodec: convertToProRes ? .proRes422 : nil,
preferredOutputURL: url.renamed { "\($0) Upscaled" },
outputSize: outputSize,
outputBitRate: bitrate,
creator: ProcessInfo.processInfo.processName
)

Expand Down