Skip to content

Commit

Permalink
Update Video.swift
Browse files Browse the repository at this point in the history
Allow higher bitrate for same codec when resolution increased.
Fix redundant compression not detected for portrait videos.
  • Loading branch information
starkdmi committed Apr 19, 2024
1 parent 60b143c commit 6d3272e
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions Sources/Video.swift
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,8 @@ public struct VideoTool {
// Source video resolution
let orientation = videoTrack.orientation
variables.orientation = orientation
let sourceVideoSize = videoTrack.naturalSize.oriented(orientation)
let naturalSize = videoTrack.naturalSize
let sourceVideoSize = naturalSize.oriented(orientation)

// Video settings
var videoCompressionSettings: [String: Any] = [:]
Expand Down Expand Up @@ -712,8 +713,9 @@ public struct VideoTool {
if videoCodec == .h264 || videoCodec == .hevc || videoCodec == .hevcWithAlpha {
/// Set bitrate value and update `targetBitrate` variable
func setBitrate(_ value: Int) {
// For the same codec use source bitrate as maximum value
if !videoCodecChanged {
// For the same codec and resolution use source bitrate as maximum value
if !videoCodecChanged &&
targetVideoSize.width <= naturalSize.width && targetVideoSize.height <= naturalSize.height {
if value >= Int(sourceBitrate) {
// Use source bitrate when higher value targeted
videoCompressionSettings[AVVideoAverageBitRateKey] = sourceBitrate
Expand Down Expand Up @@ -803,7 +805,7 @@ public struct VideoTool {
if videoCodecChanged == false, // output codec equals source video codec
bitrateChanged == false, // bitrate not changed
videoSettings.quality == defaultSettings.quality, // quality set to default value
targetVideoSize == sourceVideoSize, // output size equals source resolution
targetVideoSize == naturalSize, // output size equals source resolution
variables.frameRate == defaultSettings.frameRate, // output frame rate greater or equals source frame rate
!(videoSettings.preserveAlphaChannel == false && hasAlphaChannel == true), // false if alpha is removed
videoSettings.profile?.rawValue == defaultSettings.profile?.rawValue, // profile set to default value
Expand Down

0 comments on commit 6d3272e

Please sign in to comment.