在我的应用中,有必要在.mov和.mp4视频格式之间进行选择。
以下是我用来开始录制的代码:
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()
}
}
如果我选择AVFileTypeMPEG4,我会收到如下错误:
-[AVAssetWriter addInput:]
In order to perform passthrough to file type public.mpeg-4,
please provide a format hint in the AVAssetWriterInput initializer
看起来这里有某种解决方案(中文): http://www.jianshu.com/p/f58edf54e14c
但是,我不太了解它。如果有人遇到同样的问题,请提供帮助。
谢谢!