Skip to content

Commit

Permalink
Fix: 비디오 잘리는 문제 T2Je#11
Browse files Browse the repository at this point in the history
  • Loading branch information
soobeen27 committed Sep 10, 2024
1 parent 0a4784c commit 8504eb7
Showing 1 changed file with 38 additions and 16 deletions.
54 changes: 38 additions & 16 deletions Sources/FYVideoCompressor/FYVideoCompressor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,8 @@ public class FYVideoCompressor {
return [AVVideoCodecKey: AVVideoCodecType.hevc,
AVVideoWidthKey: size.width,
AVVideoHeightKey: size.height,
AVVideoScalingModeKey: AVVideoScalingModeResizeAspectFill,
// AVVideoScalingModeKey: AVVideoScalingModeResizeAspectFill,
AVVideoScalingModeKey: AVVideoScalingModeResizeAspect,
AVVideoCompressionPropertiesKey: [AVVideoAverageBitRateKey: bitrate,
// AVVideoProfileLevelKey: AVVideoProfileLevelH264HighAutoLevel,
// AVVideoH264EntropyModeKey: AVVideoH264EntropyModeCABAC,
Expand Down Expand Up @@ -510,26 +511,47 @@ AVVideoCompressionPropertiesKey: [AVVideoAverageBitRateKey: bitrate,
var counter = 0
var index = 0

// videoInput.requestMediaDataWhenReady(on: videoCompressQueue) {
// while videoInput.isReadyForMoreMediaData {
// if let buffer = videoOutput.copyNextSampleBuffer() {
// if frameIndexArr.isEmpty {
// videoInput.append(buffer)
// } else { // reduce FPS
// // append first frame
// if index < frameIndexArr.count {
// let frameIndex = frameIndexArr[index]
// if counter == frameIndex {
// index += 1
// videoInput.append(buffer)
// }
// counter += 1
// } else {
// // Drop this frame
// CMSampleBufferInvalidate(buffer)
// }
// }
//
// } else {
// videoInput.markAsFinished()
// completion()
// break
// }
// }
// }
let dropFactor = Int(1.0 / 0.2) //example 20% frame rate drop

videoInput.requestMediaDataWhenReady(on: videoCompressQueue) {
while videoInput.isReadyForMoreMediaData {
if let buffer = videoOutput.copyNextSampleBuffer() {
if frameIndexArr.isEmpty {
counter += 1
if counter % dropFactor == 0 {
// Drop this frame
CMSampleBufferInvalidate(buffer)
print("frame \(counter) dropped")
} else {
videoInput.append(buffer)
} else { // reduce FPS
// append first frame
if index < frameIndexArr.count {
let frameIndex = frameIndexArr[index]
if counter == frameIndex {
index += 1
videoInput.append(buffer)
}
counter += 1
} else {
// Drop this frame
CMSampleBufferInvalidate(buffer)
}
print("frame \(counter) added")
}

} else {
videoInput.markAsFinished()
completion()
Expand Down

0 comments on commit 8504eb7

Please sign in to comment.