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

Resolve Frame Drop Issue in Video Frame Reduction #12

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
9 changes: 6 additions & 3 deletions Sources/FYVideoCompressor/FYVideoCompressor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ public class FYVideoCompressor {

let videoSettings = createVideoSettingsWithBitrate(targetVideoBitrate,
maxKeyFrameInterval: 10,
size: scaleSize)
size: scaleSize,
targetFPS: quality.value.fps)
#if DEBUG
print("************** Video info **************")
#endif
Expand Down Expand Up @@ -256,7 +257,8 @@ public class FYVideoCompressor {
let targetSize = calculateSizeWithScale(config.scale, originalSize: videoTrack.naturalSize)
let videoSettings = createVideoSettingsWithBitrate(targetVideoBitrate,
maxKeyFrameInterval: config.videomaxKeyFrameInterval,
size: targetSize)
size: targetSize,
targetFPS: config.fps)

var audioTrack: AVAssetTrack?
var audioSettings: [String: Any]?
Expand Down Expand Up @@ -457,11 +459,12 @@ public class FYVideoCompressor {

}

private func createVideoSettingsWithBitrate(_ bitrate: Float, maxKeyFrameInterval: Int, size: CGSize) -> [String: Any] {
private func createVideoSettingsWithBitrate(_ bitrate: Float, maxKeyFrameInterval: Int, size: CGSize, targetFPS: Float = 30) -> [String: Any] {
return [AVVideoCodecKey: AVVideoCodecType.h264,
AVVideoWidthKey: size.width,
AVVideoHeightKey: size.height,
AVVideoScalingModeKey: AVVideoScalingModeResizeAspectFill,
AVVideoExpectedSourceFrameRateKey: targetFPS,
AVVideoCompressionPropertiesKey: [AVVideoAverageBitRateKey: bitrate,
AVVideoProfileLevelKey: AVVideoProfileLevelH264HighAutoLevel,
AVVideoH264EntropyModeKey: AVVideoH264EntropyModeCABAC,
Expand Down
12 changes: 7 additions & 5 deletions Sources/FYVideoCompressor/VideoFrameReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ public struct ReduceFrameEvenlySpaced: VideoFrameReducer {
let originalFrames = (0..<Int(originalFPS * videoDuration)).map({ $0 })

var res = [Int]()
while res.count < Int(targetFPS * videoDuration) {

while res.count < Int(originalFPS * videoDuration) && counter * stride < originalFrames.count {
let index = counter * stride
let frame = originalFrames[index]
res.append(frame)
if index < originalFrames.count {
let frame = originalFrames[index]
res.append(frame)
}
counter += 1
}

return res
}
}
Expand Down