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 option to choose output codec #21

Merged
merged 1 commit into from
Jul 16, 2024
Merged
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
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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably limited by the h264/hevc profile used and could be actually determined somehow, but hardcoded works fine for now


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
Loading