1

How can i get the Thumbnail of URL my video url like below

http://ServerDomain/streams/16476084045/3d4659d8e93bdfbdb3619a4a684c93be.flv

I am using this below code

 AVURLAsset *asset1 = [[AVURLAsset alloc] initWithURL:url options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset1];
generator.appliesPreferredTrackTransform = YES;

//Set the time and size of thumbnail for image
NSError *err = NULL;
CMTime thumbTime = CMTimeMakeWithSeconds(0,30);
CGSize maxSize = CGSizeMake(425,355);
generator.maximumSize = maxSize;

CGImageRef imgRef = [generator copyCGImageAtTime:thumbTime actualTime:NULL error:&err];
UIImage *thumbnail = [[UIImage alloc] initWithCGImage:imgRef];
cell.videoImage1.image=thumbnail;

But i am getting below error

Error Domain=AVFoundationErrorDomain Code=-11828 "Cannot Open" UserInfo={NSUnderlyingError=0x7fe8d3057900 {Error Domain=NSOSStatusErrorDomain Code=-12847 "(null)"}, NSLocalizedFailureReason=This media format is not supported., NSLocalizedDescription=Cannot Open}

And I am not using MPMoviePlayer how to get the Thumbnail image Please Help me.Thankyou.

Bittoo
  • 579
  • 7
  • 21
  • check this http://stackoverflow.com/questions/1347562/getting-thumbnail-from-a-video-url-or-data-in-iphone-sdk.@Bittoo – Madhu May 20 '16 at 09:48
  • First make sure that your FLV contains H.264 for video codec, Now manually find a keyframe's bytes within the file. Extract the H.264 frame data and feed that to your iOS decoder to get a visual image of the keyframe. That's your thumbnail. I don't use iOS so tell me do you know how to handle bytes with that system? & also is it possible to just feed (append) raw H.264 bytes to the decoder (without involving container like MP4)? – VC.One May 21 '16 at 18:15

1 Answers1

2

Use this below code,

AVAsset *avAsset = [AVURLAsset URLAssetWithURL:videoURL options:nil];

if ([[avAsset tracksWithMediaType:AVMediaTypeVideo] count] > 0)
{
     AVAssetImageGenerator *imageGenerator =[AVAssetImageGenerator assetImageGeneratorWithAsset:avAsset];
     Float64 durationSeconds = CMTimeGetSeconds([avAsset duration]);
     CMTime midpoint = CMTimeMakeWithSeconds(durationSeconds/2.0, 600);
     NSError *error;
     CMTime actualTime;

     CGImageRef halfWayImage = [imageGenerator copyCGImageAtTime:kCMTimeZero actualTime:&actualTime error:&error];


     if (halfWayImage != NULL)
     {

          NSString *actualTimeString = (NSString *)CFBridgingRelease(CMTimeCopyDescription(NULL, actualTime));
          NSString *requestedTimeString = (NSString *)CFBridgingRelease(CMTimeCopyDescription(NULL, midpoint));
          NSLog(@"Got halfWayImage: Asked for %@, got %@", requestedTimeString, actualTimeString);

          UIImage *img=[UIImage imageWithCGImage:halfWayImage];

          self.myimageView.image= img; //img is thumbnail image ;

      }

}

don't forget #import <AssetsLibrary/AssetsLibrary.h> header file

the above code is working for me, hope its helpful for you

Iyyappan Ravi
  • 3,205
  • 2
  • 16
  • 30
  • NO for me it is not showing any thing fail the if condition every time. I think this is the problem of video format. – Bittoo May 20 '16 at 09:57
  • first if condition or second if condition – Iyyappan Ravi May 20 '16 at 09:58
  • first if condition – Bittoo May 20 '16 at 09:59
  • how to get the url? if you get the `imagepickercontroller` – Iyyappan Ravi May 20 '16 at 10:00
  • @IyyappanRavi look at the question again, they said they get error `This media format is not supported`. Do you think **FLV** format is supported by iOS? I think only MPEG codecs are supported since Apple is already a member of the MPEG cartel. Don't expect FLV, OGV, WebM, AVI or other "competition" formats to work on iOS without custom decoder code... – VC.One May 21 '16 at 17:44
  • Thanks man, It worked for me. I have tried many AVAsset code but this worked for me. Really thanks saved my day. It worked for me for any video. – Arpit B Parekh Mar 17 '17 at 13:11
  • This should not be the accepted answer, unless you're simply wanting a thumbnail for a "SUPPORTED" file with SUPPORTED codecs. – gcadmes Jul 08 '22 at 16:36