iPhone Read UIimage (frames) from video with AVFoundation
You're talking about using the calls for generating what Apple calls thumbnail images from videos at specific times.
For an MPMoviePlayerController (what iOS uses to hold a video from a file or other source), there are two commands to do this. The first one generates a single thumbnail (image) from a movie at a specific point in time, and the second one generates a set of thumbnails for a time range.
This example gets an image at 10 seconds into a movie clip, myMovie.mp4:
MPMoviePlayerController *movie = [[MPMoviePlayerController alloc]
initWithContentURL:[NSURL URLWithString:@"myMovie.mp4"]];
UIImage *singleFrameImage = [movie thumbnailImageAtTime:10
timeOption:MPMovieTimeOptionExact];
Note that this performs synchronously - i.e. the user will be forced to wait while you get the screenshot.
The other option is to get a series of images from a movie, from an array of times:
MPMoviePlayerController *movie = [[MPMoviePlayerController alloc]
initWithContentURL [NSURL URLWithString:@"myMovie.mp4"]];
NSNumber time1 = 10;
NSNumber time2 = 11;
NSNumber time3 = 12;
NSArray *times = [NSArray arrayWithObjects:time1,time2,time3,nil];
[movie requestThumbnailImagesAtTimes:times timeOption:MPMovieTimeOptionExact];
This second way will trigger a notification of type MPMoviePlayerThumbnailImageRequestDidFinishNotification
each time a new image is generated. You can set up an observer to monitor this and process the image - I'll leave you to work that bit out on your own!
iOS extracting video frames as images
I think the answer in this question is what you are looking for.
iPhone Read UIimage (frames) from video with AVFoundation.
There are 2 methods specified by the accepted answer. You can use either one according to your requirement.
Reading video frame-by-frame under iOS
The two links above actually answer my question and the empty copyNextBufferSample
is an issue with iOS SDK 5.0b3, it works on the device.
When reading frames from a video on an iPad with AVAssetReader, the images are not properly oriented
After some exploration, I came across the following that allowed me to obtain the video's orientation from the AVAssetTrack
:
let transform = assetTrack.preferredTransform
radians = atan2(transform.b, transform.a)
Once I had that, I was able to convert it to degrees:
let degrees = (radians * 180.0) / .pi
Then, using a switch statement I could determine how to rotate the image:
Switch Int(degrees) {
case -90, 90:
// Rotate accordingly
case 180:
// Flip
default:
// Do nothing
}
Output from AVAssetWriter (UIImages written to video) distorted
Ok, this turned out to be an issue with the aspect ratio of the images I was reading in being different from the aspect ratio of the PixelBuffer I was creating. Made them equal and it works fine.
Grab frames from video using Swift
Thanks to @eric-d who found this post:
iOS Take Multiple Screen Shots
I manage to find out that adding:
assetImgGenerate.requestedTimeToleranceAfter = kCMTimeZero;
assetImgGenerate.requestedTimeToleranceBefore = kCMTimeZero;
...to my function will do the trick.
My updated function looks like this:
func generateThumnail(url : NSURL, fromTime:Float64) -> UIImage {
var asset :AVAsset = AVAsset.assetWithURL(url) as! AVAsset
var assetImgGenerate : AVAssetImageGenerator = AVAssetImageGenerator(asset: asset)
assetImgGenerate.appliesPreferredTrackTransform = true
assetImgGenerate.requestedTimeToleranceAfter = kCMTimeZero;
assetImgGenerate.requestedTimeToleranceBefore = kCMTimeZero;
var error : NSError? = nil
var time : CMTime = CMTimeMakeWithSeconds(fromTime, 600)
var img : CGImageRef = assetImgGenerate.copyCGImageAtTime(time, actualTime: nil, error: &error)
var frameImg : UIImage = UIImage(CGImage: img)!
return frameImg
}
var grabTime = 1.22
img = generateThumnail(urlVideo, fromTime: Float64(grabTime))
How to extract images from a video?
Simple way to extract thumbnails from a movie is to use MPMoviePlayerController
class and its - thumbnailImageAtTime:timeOption:
method.
For example you've got a filepath url:
NSURL *tempFilePathURL = ...;
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: tempFilePathURL];
UIImage *thumbnail = [player thumbnailImageAtTime:THUMBNAIL_TIME timeOption:MPMovieTimeOptionExact];
[player release];
I used it in my code and it worked.
Related Topics
Iphone:Daily Local Notifications
How to Install iOS 7 and Onwards Simulators in Xcode 7 Beta 5
-Canopenurl: Failed for Url: "Fbauth2:/" (Osstatus Error -10814.)"
App Installation Failed Due to Application-Identifier Entitlement
How to Change Text on a Back Button
Change String Color with Nsattributedstring
iOS 11 - Keyboard Height Is Returning 0 in Keyboard Notification
Firebase Undefined Symbols for Architecture X86_64
Capture Redirect Url in Wkwebview in iOS
Swift 4 "This Class Is Not Key Value Coding Compliant"
How to Export Data to a CSV File with iOS
Swift Version of Componentsseparatedbystring
How to Lookup a String Constant at Runtime in Objective-C
Reasons for Rejecting iPhone Application by Apple Store
Draw a Circle of 1000M Radius Around Users Location in Mkmapview
Swift - How to Hide Back Button in Navigation Item
Uiapplication.Registerforremotenotifications() Must Be Called from Main Thread Only
Error: Invalid Bitcode Version (Producer: '800.0.35.0_0' Reader: '703.0.31_0')