如何生成视频URL flv格式的缩略图? [英] How to generate the Thumbnail of video URL flv format?

查看:301
本文介绍了如何生成视频URL flv格式的缩略图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取URL的缩略图我的视频网址如下所示

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

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

我在下面使用以下代码

 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;

但我收到以下错误

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}

我没有使用MPMoviePlayer
如何获得缩略图请帮助我.Thankyou。

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

推荐答案

使用以下代码,

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 ;

      }

}

不要忘记 #import< AssetsLibrary / AssetsLibrary.h> 头文件

以上代码对我有用,希望它对你有帮助

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

这篇关于如何生成视频URL flv格式的缩略图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆