简体   繁体   中英

Swift iPad - AVAssetWriter AVCapture session recording video gets a green video after few seconds in iPad iOS12.4

I am using AVCaptureSession to record video with AVAssetWriter to a file and everything is working fine with iOS 13.+ devices(iPhone or iPad). But it is not working in iOS 12.4 iPad device , in that after a few seconds its video shows as a green screen(Most of the time it's getting the green screen but some times working it perfectly).

The configurations used as follows:

AVAssetWriter - fileType : AVFileType.mp4

let formatDescription = format.formatDescription let dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription)

//Have issue in iOS 12 device. So made the chnage. var videoCodecKey: AVVideoCodecType =.h264 let compressionDictionary: [String: Any] = [AVVideoExpectedSourceFrameRateKey: 25] if Common.isOSAtLeast(majorVersion: 13, minorVersion: 0) { videoCodecKey =.hevc }

            let outputSetups: [String : Any] = [        AVVideoCodecKey: videoCodecKey,
                                                  AVVideoScalingModeKey: AVVideoScalingModeResizeAspectFill,
                                                        AVVideoWidthKey: (dimensions.width),
                                                       AVVideoHeightKey: dimensions.height,
                                        AVVideoCompressionPropertiesKey: compressionDictionary]

            videoWriterInput = AVAssetWriterInput(mediaType: AVMediaType.video,outputSettings: outputSetups)

            let audioSettings: [String : Any] = [         AVFormatIDKey : kAudioFormatMPEG4AAC,
                                                          AVNumberOfChannelsKey : 2,
                                                          AVSampleRateKey : 44100.0,
                                                          AVEncoderBitRateKey: 192000 ]

            audioInput = AVAssetWriterInput(mediaType: AVMediaType.audio,
                                            outputSettings: audioSettings)

            audioInput?.expectsMediaDataInRealTime = true
            if videoWriter?.canAdd(audioInput) ?? false {
                videoWriter?.add(audioInput)
                print("audio input added")
            }

            //Add Video
            videoWriterInput?.expectsMediaDataInRealTime = true
            if videoWriter?.canAdd(videoWriterInput) ?? false {
                videoWriter?.add(videoWriterInput)
                print("video input added")
            } else { print("no input added") }

            videoWriter?.startWriting()

Please look into it and inform any issues found or require more information.

When working more on the issue its found that the issue is not related to the AVAssetWriter, In the video recording, we were embedding an image to the video . So that embedding made this issue .

In the issue, we have embedded an image to the output data. That image was made as the same size to the receiving buffer image size, for that, we were found a scaling factor, some times which gets as infinity which made the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM