微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

.mp4文件记录后无法打开

如何解决.mp4文件记录后无法打开

我的屏幕录像机需要帮助。

从设备屏幕上记录我的.mp4文件后,我尝试打开它,但它不起作用。根据我录制屏幕的时间,文件大小会发生变化,这意味着此处正在写入文件

根据状态判断,

videoWriter 效果很好(仅处于写入状态和已完成状态) 也许setUpWriter()函数需要AVAsset的更多设置?我将不胜感激。

这是我的课堂字段:

import ReplayKit

class SampleHandler: rpbroadcastsamplehandler {
    
    let screenWidth: CGFloat = 750//UIScreen.main.bounds.width
    let screenHeight: CGFloat = 1334//UIScreen.main.bounds.height
    
    var isRecording = false
    
    var videoWriterInput: AVAssetWriterInput!
    var audioWriterInput: AVAssetWriterInput!
    var videoWriter: AVAssetWriter!
    
    var sessionAtSourceTime: CMTime!
    
    var outputFileLocation: URL!

此处为开始录制的代码

override func broadcastStarted(withSetupInfo setupInfo: [String : NSObject]?) {
    guard !isRecording else { return }
    isRecording = true
    sessionAtSourceTime = nil
    setUpWriter()
}

func setUpWriter() {
    self.outputFileLocation = videoFileLocation()
    
    videoWriter = try? AVAssetWriter.init(outputURL: self.outputFileLocation,fileType: AVFileType.mp4)
    
    // add video input
    videoWriterInput = AVAssetWriterInput(mediaType: AVMediaType.video,outputSettings: [
        AVVideoCodecKey : AVVideoCodecType.h264,AVVideoWidthKey : screenWidth - (screenWidth.truncatingRemainder(dividingBy: 16)),AVVideoHeightKey : screenHeight - (screenHeight.truncatingRemainder(dividingBy: 16)),AVVideoCompressionPropertiesKey : [AVVideoAverageBitRateKey : getBitRate()]
    ])
    videoWriterInput.expectsMediaDataInRealTime = true
    
    // add audio input
    audioWriterInput = AVAssetWriterInput(mediaType: AVMediaType.audio,outputSettings: nil)
    audioWriterInput.expectsMediaDataInRealTime = true
    
    if videoWriter.canAdd(videoWriterInput) {
        videoWriter.add(videoWriterInput)
    }
    
    if videoWriter.canAdd(audioWriterInput) {
        videoWriter.add(audioWriterInput)
    }
    
    videoWriter.startWriting()
}

private func getBitRate() -> Int {
    let numPixels: CGFloat = screenWidth * screenHeight
    let bitsPerPixel: CGFloat = pow(2.0,1.0)
            
    return Int(numPixels * bitsPerPixel)
}

这是用于进行编写的功能

override func processSampleBuffer(_ sampleBuffer: CMSampleBuffer,with sampleBufferType: RPSampleBufferType) {
    super.processSampleBuffer(sampleBuffer,with: sampleBufferType)
    
    let writable = canWrite()
    
    if writable,sessionAtSourceTime == nil {
        // start writing
        sessionAtSourceTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer)
        videoWriter.startSession(atSourceTime: sessionAtSourceTime!)
    }
    
    if writable {
        switch sampleBufferType {
        case .video:
            if videoWriterInput.isReadyForMoreMediaData {
                videoWriterInput.append(sampleBuffer)
            }
        case .audioApp:
            if audioWriterInput.isReadyForMoreMediaData {
                audioWriterInput.append(sampleBuffer)
            }
        case .audioMic:
            print("mic")
        @unkNown default:
            print("unkNown")
        }
    }
}

在这里我完成广播:

override func broadcastFinished() {
    
    guard isRecording else { return }
    isRecording = false
    videoWriterInput.markAsFinished()
    audioWriterInput.markAsFinished()
    
    videoWriter.finishWriting { [weak self] in
        self?.sessionAtSourceTime = nil
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。