iOS Split Video into frames
Dear all,
You may have encountered scenarios where you would like to split a video into several frames at different time intervals.
This post is for you if you intend to do the splitting at device level.
I presume that you have some reasonable experience with Objective C and XCode such as adding a framework and adding a file.
There are several steps to do to get this going.
- 1. Build an XCode project and add a video file to project. I've worked with .mp4 files. Should work with other standard formats.
- 2. Add AssetsLibrary framework and AVFoundation frameworks to your project
- 3. Create a AVAsset object with your video. You need to pass the location of the video as a NSURL object. I added the video(myvid.mp4) as an asset to my project. Therefore, I will use the following method to create the asset object.
- 4. Create CMTime objects indicating the times which you need to take frames at
- CMTime t1= CMTimeMake(1, 1);
- CMTime t2= CMTimeMake(2, 1);
- CMTime t3= CMTimeMake(3, 1);
- 5. Next create an array with the time objects. Since CMTime are structs, you need to wrap them around NSValue objects. You can do that as below.
- NSValue *v1= [NSValue valueWithCMTime:t1];
- NSValue *v2= [NSValue valueWithCMTime:t2];
- NSValue *v3= [NSValue valueWithCMTime:t3];
NSArray *array=[NSArray arrayWithObjects:v1,v2,v3,nil];
- 6. Next, you need to create an AVAssetImageGenerator to do the actual work. You can pass the asset object you created before as follows.
AVAssetImageGenerator *generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
- 7. Next, you need to call generateCGImagesAsynchronouslyForTimes method of the generator object to actually start making the frames. It accepts an array. You need to pass the array you created above.
[generator generateCGImagesAsynchronouslyForTimes:timesArray completionHandler:^(CMTime requestedTime, CGImageRef image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error) {
//This gets fired when a image is made. So, for our scenario, this method will fire 3 times(we requested 3 frames)
UIImage *generatedImage=[UIImage imageWithCGImage:image];
}];
AVAsset *asset=[AVAsset assetWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"myvid" ofType:@"mp4"]]];
NOTE: If you need to record the video in your app and then load that video, you can pass the correct file path for building the NSURL object.
I've created three CMTime structs to take frames at t=1,t=2,t=3 seconds.
NOTE: CMTimeMake takes two arguments. The second is the timescale. Use value=1 for seconds. The first parameter is the amount of time in the required timescale.
This is the fundamental of creating images. Your app's logic will determine what you do with the images. Generally it's a good idea to save the images in the file system such that you could access them from anywhere in the app.
If you need any extra help, please feel free to comment. If you need a working copy of a Proof of concept which I created, please drop me a mail. I will be willing to email you the code.
Comments
Thanks,
Ri cj
I want to make whatsapplike gallery...when i go through the gallery the respected frame should be in motion of their respected video....so i have to get frame at every instants pls mail me code @"aashi.godil@gmail.com"
even vice versa case also I have lots of images and i want to make video out of it...do give me suggestion and reply as soon as possible
This blog is amazing and very useful for me and everyone thank you for posting.
Thanks,
Mayank Palotra
Data Science course in Chennai
Data science course in bangalore
Data science course in pune
Data science online course
Data Science Interview questions and answers
Data Science Tutorial
Data science course in bangalore
Data Science Training In Hyderabad
Data Science Training Course In Chennai | Data Science Training Course In Anna Nagar | Data Science Training Course In OMR | Data Science Training Course In Porur | Data Science Training Course In Tambaram | Data Science Training Course In Velachery
AI Training In Hyderabad
AI Training In Hyderabad
Data Science Training in Hyderabad