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
let outputPath = NSTemporaryDirectory() + Date().description + "output" + "\(state.currentFileFormat.rawValue)"
outputFileURL = URL(fileURLWithPath: outputPath)
let fileType = (state.currentFileFormat == .mov) ? AVFileTypeQuickTimeMovie : AVFileTypeMPEG4
try? FileManager.default.removeItem(at: outputFileURL!)
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
]
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!
source
share