0

I'm trying to compress a video which has a frame rate of 30 fps into 20 fps in order to reduce the size of video using AVAssetWriter. However, during appending the new copy of sample buffer, my apps get terminated due to memory issues. I was successful to compress it if I do not change the frame rate which just directly appending the sample into the input.

This coding was implement by combining both issue and compressor from here: AVAssetWriter AVVideoExpectedSourceFrameRateKey (frame rate) ignored

And here: https://pastebin.com/CQPthmjg

videoInput.requestMediaDataWhenReady(on: videoInputQueue) {
    //request data here
    var previousTimingInfo: CMTime = kCMTimeZero
    while (videoInput.isReadyForMoreMediaData) {
        let sample = assetReaderVideoOutput.copyNextSampleBuffer()
        if (sample != nil) {
            
            autoreleasepool {
                let newSample = UnsafeMutablePointer<CMSampleBuffer?>.allocate(capacity: 1) 
                let timingInfo = UnsafeMutablePointer<CMSampleTimingInfo>.allocate(capacity: 1) 
                
                CMSampleBufferGetSampleTimingInfo(sample!, 0, timingInfo)
                
                timingInfo.pointee.duration = CMTimeMake(1, 20)
                
                timingInfo.pointee.presentationTimeStamp = CMTimeAdd(previousTimingInfo, timingInfo.pointee.duration)
                previousTimingInfo = timingInfo.pointee.presentationTimeStamp
                
                
                let status: OSStatus = CMSampleBufferCreateCopyWithNewTiming(kCFAllocatorDefault, sample!, 1, timingInfo, newSample)
                if status == noErr {
                    videoInput.append(newSample.pointee!)
                    print("appending sample buffer...")
                } else {
                    print("status != noErr;\nstatus: \(status)")
                }
                //videoInput.append(sample!)
            }
            
        } else {
            videoInput.markAsFinished()
            DispatchQueue.main.async {
                videoFinished = true
                closeWriter()
            }
            break;
        }
    }
}

Error happened as:

appending sample buffer...
appending sample buffer...
appending sample buffer...
appending sample buffer...
appending sample buffer...
appending sample buffer...
appending sample buffer...
appending sample buffer...
appending sample buffer...
Message from debugger: Terminated due to memory issue
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • So you basically want to compress a video. right? – bestiosdeveloper Jul 22 '18 at 12:50
  • Yes, basically my boss force me to reduce the frame rate, so I'm looking for issue and solving. –  Jul 22 '18 at 12:52
  • remove the autoreleasepool block and try, also set the variables as nil after using it at the end manually and check. – vivekDas Jul 22 '18 at 13:24
  • @vivekDas thanks for the correction. After doing what you said, I facing another problem which the video I did compress to another frame rate did not work as I expected, it turns out exactly blank video, the source video was a 3second of 3m bit rate and 30fps mp4. Is it I miss some calculation on the frame rate? Thanks in advanced. –  Jul 22 '18 at 14:48

0 Answers0