2017-12-03 250 views
0

我正在Swift3中開發基於視頻的應用程序。根據視頻持續時間,我有一個視頻網址和一個範圍滑塊,用戶可以從滑塊中選擇任何最小值和最大值。如果假設用戶選擇了最小值3秒和最大值7秒,那麼在這段時間內,我需要生成一個視頻縮略圖。對於這個我使用AVAssetImageGenerator生成這個,我都嘗試下面的代碼來實現這一目標:每次從Swift的Url獲取相同的視頻縮略圖iOS

func createThumbnailOfVideoFromFileURL(_ strVideoURL: URL) -> UIImage?{ 

     let asset = AVAsset(url: strVideoURL) 
     let assetImgGenerate : AVAssetImageGenerator = AVAssetImageGenerator(asset: asset) 
     assetImgGenerate.appliesPreferredTrackTransform = true 
     let time = CMTimeMake(1, 30) 
     let img = try? assetImgGenerate.copyCGImage(at: time, actualTime: nil) 

     guard let cgImage = img else { return nil } 

     let frameImg = UIImage(cgImage: cgImage) 
     return frameImg 
    } 

func generateThumbnailForUrl(vidUrl:URL) -> UIImage { 

     let asset = AVURLAsset(url: vidUrl, options: nil) 
     let imgGenerator = AVAssetImageGenerator(asset: asset) 
     var thmbnlImg = UIImage() 
     do{ 
      let cgImage = try imgGenerator.copyCGImage(at: CMTimeMake(0, 1), actualTime: nil) 
      thmbnlImg = UIImage(cgImage: cgImage) 
      thmbnlImg = thmbnlImg.imageRotatedByDegrees(degrees: 90.0, flip: false) 
     } 
     catch{ 
      print(error) 
     } 
     // !! check the error before proceeding 
     return thmbnlImg 
    } 

但問題是我使用上述兩種方法獲得相同的縮略圖,BCOS我這兩種方法都沒有設定持續時間。我如何添加最小和最大持續時間來爲每個不同的持續時間生成不同的縮略圖。請幫我解決我的問題。謝謝!

編輯:我試圖設置像時間:

let time: CMTime = CMTimeMakeWithSeconds(rangeSlider!.lowerValue, 1)

然後我得到不同的縮略圖,但對於一些滑塊範圍我得到零縮略圖也。任何人都可以有一些想法如何設置CMTimeMakeWithSeconds preferredTimeScale值?

回答

-1

假設您修剪過的視頻網址是videoURL。成功修剪視頻後,添加此代碼段。實際上,這段代碼將幫助您在每一秒鐘從裁剪的視頻中提取圖像(也就是說,如果您的裁剪視頻的持續時間爲10秒,則此代碼將在每秒提取10幅圖像,並且所有這些圖像都將保存在一個陣列中,命名爲videoFrames)。最後,你可以用這些圖像做任何你想做的事情。您還可以在此過程正在進行時添加活動指示器。希望這可以幫助。

var videoFrames = [UIImage]() 
let asset : AVAsset = AVAsset(url: videoURL as URL) 
let videoDuration = CMTimeGetSeconds(asset.duration) 
let integerValueOFVideoDuration = Int(videoDuration) 

//start activity indicator here 

for index in 0..<integerValueOFVideoDuration + 1 { 
    self.generateFrames(url: videoURL, fromTime: Float64(index)) 
} 

func generateFrames(url: NSURL, fromTime: Float64) { 
     if videoFrames.count == integerValueOFVideoDuration { 
      //end activity indicator here 
      return 
     } 
     let asset: AVAsset = AVAsset(url: url as URL) 
     let assetImgGenerate: AVAssetImageGenerator = AVAssetImageGenerator(asset: asset) 
     assetImgGenerate.maximumSize = CGSize(width: 300, height: 300) 
     assetImgGenerate.appliesPreferredTrackTransform = true 
     let time: CMTime = CMTimeMakeWithSeconds(fromTime, 600) 
     var img: CGImage? 
     do { 
      img = try assetImgGenerate.copyCGImage(at: time, actualTime: nil) 
     } catch { 
     } 
     if img != nil { 
      let frameImg: UIImage = UIImage(cgImage: img!) 
      videoFrames.append(frameImg) 
     } else { 
      //return nil 
     } 
    } 
+0

感謝您的回覆,在這裏我沒有修剪視頻Url ..我只有一個主視頻網址和一個範圍滑塊,用戶將從滑塊中選擇最小值和最大值。使用該範圍值我需要生成縮略圖圖像。任何建議......你明白嗎? –

+0

好的...您需要將滑塊的最小值和最大值轉換爲CMTimeMakeWithSeconds,並將這些值傳遞給上述函數。 fromTime:Float64是這裏的參數。 –

+0

你可以在代碼中顯示我嗎,我更改了CMTimeMake值,但每次都是相同的縮略圖。 –