iphone - Taking thumbnail image from video always returns null -
i trying first thumbnail video.
i tried following:
one
avurlasset *asset = [[avurlasset alloc] initwithurl:[nsurl fileurlwithpath:_moviepath] options:nil]; avassetimagegenerator *gen = [[avassetimagegenerator alloc] initwithasset:asset]; gen.appliespreferredtracktransform = yes; cmtime time = cmtimemakewithseconds(0.0, 600); nserror *error = nil; cmtime actualtime; cgimageref image = [gen copycgimageattime:time actualtime:&actualtime error:&error]; uiimage *thumb = [[uiimage alloc] initwithcgimage:image]; cgimagerelease(image); nslog(@"the error %@: image %@: url %@: ",error,_mainthumbnail,[nsurl fileurlwithpath:_moviepath] );
however, log is:
the error (null): image (null): url file:///private/var/mobile/applications/d1293bdc-ea7e-4ac7-ad3c-1ba3548f37d6/tmp/trim.6f0c4631-e5e8-43cd-bf32-9b8f09d4acf1.mov:
two
avurlasset *asset=[[avurlasset alloc] initwithurl:[nsurl fileurlwithpath:_moviepath] options:nil]; avassetimagegenerator *generator = [[avassetimagegenerator alloc] initwithasset:asset]; generator.appliespreferredtracktransform=true; cmtime thumbtime = cmtimemakewithseconds(0,30); avassetimagegeneratorcompletionhandler handler = ^(cmtime requestedtime, cgimageref im, cmtime actualtime, avassetimagegeneratorresult result, nserror *error){ if (result != avassetimagegeneratorsucceeded) { nslog(@"couldn't generate thumbnail, error:%@", error); } _mainthumbnail.image = [uiimage imagewithcgimage:im]; nslog(@"thumbnails %@ ",_mainthumbnail.image); }; cgsize maxsize = cgsizemake(320, 180); generator.maximumsize = maxsize; [generator generatecgimagesasynchronouslyfortimes:[nsarray arraywithobject:[nsvalue valuewithcmtime:thumbtime]] completionhandler:handler];
the log thumnails null.
why image null? looked in other forums, , suggested use fileurlwithpath -which using.
i using ios7
you can try method. takes first frame of video @ given url , returns uiimage
.
- (uiimage *)thumbnailfromvideoaturl:(nsurl *)url { avasset *asset = [avasset assetwithurl:url]; // thumbnail @ start of video cmtime thumbnailtime = [asset duration]; thumbnailtime.value = 0; // image video @ given time avassetimagegenerator *imagegenerator = [[avassetimagegenerator alloc] initwithasset:asset]; cgimageref imageref = [imagegenerator copycgimageattime:thumbnailtime actualtime:null error:null]; uiimage *thumbnail = [uiimage imagewithcgimage:imageref]; cgimagerelease(imageref); return thumbnail; }
the method not check errors code using ok nil
returned if fails.
edit: clarification of cmtime
values
cmtime
has
timescale
- think fps (frames per second) of video (25 fps -> 1s of video has 25 video frames)value
- identification of concrete frame. in code above, says frame want take thumbnail of.
for calculation of frame @ exact time, have make calculation:
value = timescale * seconds_to_skip
which equivalent to
"frame of video take" = "fps" * "number of seconds skip"
if want take example 1st frame of 2nd second of video, want in fact skip 1st second of video. use:
cmtime thumbnailtime = [asset duration]; thumbnailtime.value = thumbnailtime.timescale * 1;
another look: want skip first 1s of video , take thumbnail. i.e. on 25 fps, want skip 25 frames. cmtime.value = 25
(skip 25 frames).
Comments
Post a Comment