Getting an error when trying to record MPEG4 video using GPUImage2

In my application, you need to have a choice between .mov and .mp4 video formats.

Here is the code I use to start recording:

func startCapturingVideo() {
    state.isRecording = true

    //Setting output file path
    let outputPath = NSTemporaryDirectory() + Date().description + "output" + "\(state.currentFileFormat.rawValue)"
    outputFileURL = URL(fileURLWithPath: outputPath)

    //Setting file type
    let fileType = (state.currentFileFormat == .mov) ? AVFileTypeQuickTimeMovie : AVFileTypeMPEG4
    try? FileManager.default.removeItem(at: outputFileURL!)

    //Compression settings
    let compressionSettings: [String: Any] = [AVVideoAverageBitRateKey: NSNumber(value: 20000000),
                                          AVVideoMaxKeyFrameIntervalKey: NSNumber(value: 1),
                                          AVVideoProfileLevelKey: AVVideoProfileLevelH264Baseline41]


    let videoSettings: [String : Any] = [
        AVVideoCodecKey  : AVVideoCodecH264,
        AVVideoCompressionPropertiesKey: compressionSettings,
        AVVideoWidthKey  : 1920,
        AVVideoHeightKey : 1080,
        AVVideoScalingModeKey:AVVideoScalingModeResizeAspectFill
    ]

    //If fileType is AVFileTypeMPEG4, this leads to an error
    //when AVAssetWriterInput initiated inside MovieOutput
    movieOutput = try? MovieOutput(URL: outputFileURL!, 
        size: Size(width:1920, height: 1080), 
        fileType: fileType, 
        liveVideo: true,
        settings: videoSettings as [String : AnyObject])

    camera?.audioEncodingTarget = movieOutput
    crop! --> upscale! --> movieOutput!
    movieOutput?.startRecording()
    DispatchQueue.main.async {
        self.startRecordingAnimation()
        self.disableButtons()
        self.timerManager.start()
        self.registerBackgroundTask()
    }
}  

If I select AVFileTypeMPEG4, I would get this error:

-[AVAssetWriter addInput:] 
In order to perform passthrough to file type public.mpeg-4,
please provide a format hint in the AVAssetWriterInput initializer

There seems to be some solution here (in Chinese): http://www.jianshu.com/p/f58edf54e14c

I do not quite understand this.

If someone is facing the same problem, please help.

Thank!

+4
source share

Source: https://habr.com/ru/post/1680554/


All Articles