簡體   English   中英

如何生成視頻URL flv格式的縮略圖?

[英]How to generate the Thumbnail of video URL flv format?

我如何獲取我的視頻網址的網址縮略圖,如下所示

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如何獲取縮略圖圖像,請幫助我。謝謝。

使用下面的代碼,

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>頭文件

上面的代碼對我有用,希望它對您有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM