Playing CMSampleBufferRef's with AVPlayer - ios

I am working on an OSX video editing app and have a set of CMSampleBufferRef's in an array representing each frame of the video.
I want to render a preview of the video using AVPlayer - is it possible feed in these samples directly into AVPlayer?
I've looked at most of the AVFoundation classes. I know I can use AVAssetWriter to write to a file, but want to avoid this as the user will still be doing more editing (so good to have the raw frame data).
Any thoughts?

Yes you can.
First of all, you should convert CMSampleBufferRef to CGImageRef, this will allow you to display frame samples in screen.
This answer has the all necessary code to make this.
About to play with AVPlayer, I'm not sure if you really need to do this, since you have full access through your CMSampleBufferRef array and you are able to convert and render those samples properly, I think is not necessary to put those samples at AVPlayer, instead, you can render directly CGImage at CALayer.
I hope this can help you.

Related

Capture multiple frames and prepare a video like boomerang

I am trying to integrate a functionality where i have to capture multiple frames let say 3 frames in a second and at last combines all captured frames to form a video and upload that video on server.
You can refer the functionality same as happens in boomerang, i have searched a lot about the most effective way to do the same but didn't found anything helpful.
Any guidance is appreciated.
Video From Image Array
For combining your image array to video please make use of widely accepted answer here.
By using AVAssetWriter and CVPixelBufferRef .
To Make Video in reverse order
This can be done effectively Using the AVFoundation library.
Use AVAssetReader to read the video and Use AVAssetWriter to write the video in reverse order.
Refer the tutorial Reverse Video AVFoundation

iOS postprocessing - overlay timestamp to video and export

I am working on an application where video and time/GPS/accelerometer data is simultaneously recorded to separate files.
I can play the video and have my overlay appear perfectly in realtime, but I cannot simply export this.
I am wanting to post-process the video and overlay the time,coordinates and on the video.
There are other shapes that will be overlayed which change size/position on each frame.
I have tried using AVMutableComposition and adding CALayers with limited results-
This works to an extent but I cannot synchronise the timestamp with the video. I could use a CAKeyframeAnimation with values+keyTimes, but the amount of values I need to work with is excessive.
My current approach is to render a separate video consisting of CGImages created using the data. This works well but I will need to use a ChromaKey to have transparency in the overlay. I have read that there will likely be quality issues after doing this.
Is there a simpler approach that I should be looking at?
I understand that render speed will not be fantastic, however I do not wish to require a separate 'PC' application to render the video.
Use AVAssetReader for recorded video. Get the CMSampleBufferRef, get it timestamp, draw time on sample buffer, write buffer to AVAssetWriterInputPixelBufferAdaptor. Similar approach for video being recorded.
Use the AVVideoCompositing protocol https://developer.apple.com/library/mac/documentation/AVFoundation/Reference/AVVideoCompositing_Protocol/index.html
This will allow you to get frame by frame call backs with the pixel buffers to do what you want.
With this protocol you will be able to take one frame and overlay whoever you would like. Take a look at This sample - https://developer.apple.com/library/ios/samplecode/AVCustomEdit/Introduction/Intro.html to see how to handle frame by frame modifications. If you take advantage of the AVVideoCompositing protocol you can set a custom video compositor and a video composition on your AVPlayerItem and AVExportSession to render/export what you want.

iOS Getting last frame of video

I have video in .mp4 format and I would like to get last frame of this video then blur it and put into UIImageView. My problem is that I dont know how to get last frame of video. How can I do this in iOS?
Use AVAssetImageGenerator.
https://developer.apple.com/librarY/mac/documentation/AVFoundation/Reference/AVAssetImageGenerator_Class/Reference/Reference.html#//apple_ref/occ/cl/AVAssetImageGenerator
I haven't personally used it, but it looks pretty straight forward if you've used AVFoundation before. Looks like you just initialize it with an asset and then you can generate a CGImage at a specific time using copyCGImageAtTime:actualTime:error:
You may try MPMoviePlayer, here is document related:
https://developer.apple.com/library/ios/documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006953-CH3-SW69

Load video from iPhone library, modify frame and play it in real-time

I'm looking for a tips to develop an application for iPhone/iPad that will be able to process video (let's consider only local files stored on the device for simplicity) and play it in real-time. For example you can choose any movie and choose "Old movie" filter and want it like on old lamp TV.
In order to make this idea real i need to implement two key features:
1) Grab frames and audio stream from a movie file and get access to separate frames (I'm interested in raw pixel buffer in BGRA or at least YUV color space).
2) Display processed frames somehow. I know it's possible to render processed frame to OpenGL texture, but i would like to have more powerful component with playback controls. Is there any class of media player that supports playing custom image and audio buffers?
The processing function is done and it's fast (less than duration on one frame).
I'm not asking for ready solution, but any tips are welcome!
Answer
Frame grabbing.
It seems the only way to grab video and audio frames is to use AVAssetReader class. Although it's not recommended to use for real-time grabbing it does the job. In my tests on iPad2 grabbing single frame needs about 7-8 ms. Seeking across the video is a tricky. Maybe someone can point to more efficient solution?
Video playback. I've done this with custom view and GLES to render a rectangle texture with a video frame inside of it. As far as i know it's the fastest way to draw bitmaps.
Problems
Need to manually play a sound samples
AVAssetReader grabbing should be synchronized with a movie frame rate. Otherwise movie will go too fast or too slow.
AVAssetReader allows only continuous frame access. You can't seek forward and backward. Only proposed solution is to delete old reader and create a new with trimmed time range.
This is how you would load a video from the camera roll..
This is a way to start processing video. Brad Larson did a great job..
How to grab video frames..
You can use AVPlayer+ AVPlayerItem, it provide you a chance to apply a filter on the display image.

iOS4: how do I use video file as an OpenGL texture?

I'm trying to display the contents of a video file (let's just say without the audio for now) onto a UV mapped 3D object in OpenGL. I've done a fair bit in OpenGL but have no idea where to begin in video file handling, and most of the examples out there seems to be for getting video frames from cameras, which is not what I'm after.
At the moment I feel if I can get individual frames of the video as CGImageRef I'd be set, so I'm wondering how to do this? Perhaps there are even be better ways to do this? Where should I start and what's the most straight forward file format for video playback on iOS? .mov?
Apologies; typing on an iPhone so I'll be a little brief.
Create an AVURLAsset with the URL of your video - which can be a local file URL if you like. Anything QuickTime can do is fine, so MOV or M4V in H.264 is probably the best source.
Query the asset for tracks of type AVMediaTypeVideo. You should get just one unless your source video has multiple camera angles of something like that, so just taking objectAtIndex:0 should give you the AVAssetTrack you want.
Use that to create an AVAssetReaderTrackOutput. Probably you want to specify kCVPixelFormatType_32BGRA.
Create an AVAssetReader using the asset; attach the asset reader track output as an output. And call startReading.
Henceforth you can call copyNextSampleBuffer on the track output to get new CMSampleBuffers, putting you in the same position as if you were taking input from the camera. So you can lock that to get at pixel contents and push those to OpenGL via Apple's BGRA extension.
You're probably going to have to use a player layer and flatten its contents into a bitmap context. See the documentation for AVPlayerLayer. The performance might be very poor though.

Resources