Skip to content

Commit

Permalink
Fix division by zero for min audio quality
Browse files Browse the repository at this point in the history
  • Loading branch information
starkdmi committed Apr 7, 2024
1 parent 55a1d82 commit adf9042
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions Sources/Video.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1147,20 +1147,24 @@ public struct VideoTool {
bitrate = 128_000
}
let quality = (audioSettings.quality ?? .high).rawValue
if quality != 0 {
// Formula: bitrate / (8 * channels * quality)
let doubleValue: Double = Double(bitrate) / (8 * Double(channelsPerFrame!) * Double(quality))

// Formula: bitrate / (8 * channels * quality)
let doubleValue: Double = Double(bitrate) / (8 * Double(channelsPerFrame!) * Double(quality))
// Limit values in range 0-31
let intValue = Int(doubleValue + 0.5) & 0x1F

// Limit values in range 0-31
let intValue = Int(doubleValue + 0.5) & 0x1F
// Find closest divadable by 8 value
let remainder = intValue % 8
bitsPerChannel = remainder == 0 ? intValue : intValue + (8 - remainder)

// Find closest divadable by 8 value
let remainder = intValue % 8
bitsPerChannel = remainder == 0 ? intValue : intValue + (8 - remainder)

// Bit depth can only be one of: 8, 16, 24, 32
if ![8, 16, 24, 32].contains(bitsPerChannel) {
bitsPerChannel = 16
// Bit depth can only be one of: 8, 16, 24, 32
if ![8, 16, 24, 32].contains(bitsPerChannel) {
bitsPerChannel = 16
}
} else {
// Minimal quality
bitsPerChannel = 8
}
}

Expand Down

0 comments on commit adf9042

Please sign in to comment.