Saving Movies from AVCaptureVideoDataOutput Video Frames
With the new video features in AVFoundation that are included with iOS4, it is possible to access开发者_StackOverflow the raw video frames as they are captured by the camera by using AVCaptureVideoDataOutput.
I would like to overlay text and other information on top of these frames, then output them to a movie file which at some point will be saved to the asset library.
Unfortunately it doesn't appear that Apple has given us an easy way to save these frames to a movie file.
What are my options for saving the frames to a movie file that will be compatible with the asset library?
Is there any way to accomplish this using only the iPhone SDK?
If not, what third party libraries exist that are iPhone compatible and could be utilized to accomplish this?
You can now use AVAssetWriter
to save the frames in your AVCaptureVideoDataOutput
Here's an example of how to use AVAssetWriter
in Swift 4:
assetWriter = AVAssetWriter()
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
let outputURL = URL(fileURLWithPath: documentsPath!).appendingPathComponent("test.m4v")
assetWriter = try! AVAssetWriter(outputURL: outputURL, fileType: .mp4)
assetWriterInput = AVAssetWriterInput(mediaType: .video, outputSettings: videoOutput.recommendedVideoSettingsForAssetWriter(writingTo: .mp4))
assetWriterInput.expectsMediaDataInRealTime = true
if assetWriter.canAdd(assetWriterInput) {
assetWriter.add(assetWriterInput)
} else {
print("no input added")
}
assetWriter.startWriting()
The reference documentation is here: AVAssetWriter
I hope this helps someone, I'm going through the AVFoundation struggle myself right now too.
I've found no included means for either encoding the frames captured with AVCaptureVideoDataOutput
or writing them to a Quicktime .mov file.
You may want to look at ffmpeg to see if it meets your needs—it can handle both the container format and encoding.
AVCaptureMovieFileOutput
lets you capture data to a QuickTime movie, maybe this helps...
Hey! If you just need to do some sort of watermarks over your video then it would be easy just to capture movie with your camera and then use AVVideoCompositionCoreAnimationTool of AVVideoComposition. You can add any sorts of animation, texts as overlay for your video.
精彩评论