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.

    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.

  • 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);

    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.

  • 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];
    }];





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

Halo said…
This is great. Any way of doing this in reverse? I need to take a bunch of UIImage.view images and create a video from them. Any suggestions?

Thanks,
Ri cj
Unknown said…
hello
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
Mayank Palotra said…
Hello,

This blog is amazing and very useful for me and everyone thank you for posting.

Thanks,
Mayank Palotra
priya said…
Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you..Keep update more information..
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
shanjames said…
Nice blog, I understood the topic very clearly, and want to study more like this.
Data Science Training In Hyderabad
nikhil reddy said…
Hi, Thanks for sharing nice articles....


AI Training In Hyderabad
nikhil reddy said…
Hello thanks for share such a huge content...


AI Training In Hyderabad
nikhil reddy said…
I like your post. Everyone should do read this blog. Because this blog is important for all now I will share this post. Thank you so much for share with us.

Data Science Training in Hyderabad

Popular posts from this blog

Encrypt and Decrypt Images Using Java

Build your own Network sniffer

ASP Response.Write newline