From b2212e56dec047b2f6d553427e87b8b5f1b86dd4 Mon Sep 17 00:00:00 2001 From: raykim Date: Wed, 17 Jan 2024 13:01:56 +0900 Subject: [PATCH] Fix: Resolve frame drop issues in video compression and frame reduction 1. Updated the `reduce` method in `ReduceFrameEvenlySpaced` to use `originalFPS * videoDuration` for loop condition and index checks, preventing frame drop by ensuring all frames are correctly processed. 2. Altered video compression settings in `createVideoSettingsWithBitrate` to accurately use `targetFPS`, enhancing the frame rate consistency during compression. These changes collectively resolve the previously observed frame drop issue, ensuring a more reliable and accurate frame reduction in FYVideoCompressor. --- Sources/FYVideoCompressor/FYVideoCompressor.swift | 9 ++++++--- Sources/FYVideoCompressor/VideoFrameReducer.swift | 12 +++++++----- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/Sources/FYVideoCompressor/FYVideoCompressor.swift b/Sources/FYVideoCompressor/FYVideoCompressor.swift index 2849367..bfe85b7 100644 --- a/Sources/FYVideoCompressor/FYVideoCompressor.swift +++ b/Sources/FYVideoCompressor/FYVideoCompressor.swift @@ -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 @@ -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]? @@ -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, diff --git a/Sources/FYVideoCompressor/VideoFrameReducer.swift b/Sources/FYVideoCompressor/VideoFrameReducer.swift index b9c8a49..59f6a44 100644 --- a/Sources/FYVideoCompressor/VideoFrameReducer.swift +++ b/Sources/FYVideoCompressor/VideoFrameReducer.swift @@ -29,14 +29,16 @@ public struct ReduceFrameEvenlySpaced: VideoFrameReducer { let originalFrames = (0..