Skip to content

Commit

Permalink
Add option to choose output codec (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
christianselig authored Jul 16, 2024
1 parent 59f4660 commit b4ead0f
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 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: "Output codec: 'hevc', 'prores', or 'h264' (default: hevc)") var codec: String = "hevc"

mutating func run() async throws {
guard ["mov", "m4v", "mp4"].contains(url.pathExtension.lowercased()) else {
Expand Down Expand Up @@ -51,21 +52,33 @@ import Upscaling
}

let outputSize = CGSize(width: width, height: height)
let outputCodec: AVVideoCodecType? = {
switch codec.lowercased() {
case "prores": return .proRes422
case "h264": return .h264
default: return .hevc
}
}()

let convertToProRes = (outputSize.width * outputSize.height) > (3840 * 2160) &&
!(formatDescription?.videoCodecType?.isProRes ?? false)
// Through anecdotal tests anything beyond 14.5K fails to encode for anything other than ProRes
let convertToProRes = (outputSize.width * outputSize.height) > (14500 * 8156)

if (convertToProRes) {
CommandLine.info("Forced ProRes conversion due to output size being larger than 14.5K (will fail otherwise)")
}

let exportSession = UpscalingExportSession(
asset: asset,
outputCodec: convertToProRes ? .proRes422 : nil,
outputCodec: convertToProRes ? .proRes422 : outputCodec,
preferredOutputURL: url.renamed { "\($0) Upscaled" },
outputSize: outputSize,
creator: ProcessInfo.processInfo.processName
)

CommandLine.info([
"Upscaling from \(Int(inputSize.width))x\(Int(inputSize.height)) ",
"to \(Int(outputSize.width))x\(Int(outputSize.height)) "
"to \(Int(outputSize.width))x\(Int(outputSize.height)) ",
"using codec: \(outputCodec?.rawValue ?? "hevc")"
].joined())
ProgressBar.start(progress: exportSession.progress)
try await exportSession.export()
Expand Down

0 comments on commit b4ead0f

Please sign in to comment.