In my app it is necessary 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 choose AVFileTypeMPEG4, I would get an error like this:
-[AVAssetWriter addInput:]
In order to perform passthrough to file type public.mpeg-4,
please provide a format hint in the AVAssetWriterInput initializer
Looks like there is some sort of solution here (in Chinese): http://www.jianshu.com/p/f58edf54e14c
I don't quite understand it, though.
If anybody encountered the same issue, please help.
Thanks!