Swift - 从视频

时间:2017-03-08 07:22:20

标签: ios swift video

我正在关注this code以获取视频中的所有帧。在这个链接中,他试图在特定时间获得一个框架。但我需要得到所有帧。这是我的代码......

var mutableVideoURL = NSURL()
var videoFrames = [UIImage]()

let asset : AVAsset = AVAsset(url: self.mutableVideoURL as URL)
let mutableVideoDuration = CMTimeGetSeconds(asset.duration)
print("-----Mutable video duration = \(mutableVideoDuration)")
let mutableVideoDurationIntValue = Int(mutableVideoDuration)
print("-----Int value of mutable video duration = \(mutableVideoDurationIntValue)")

for index in 0..<mutableVideoDurationIntValue {
   self.generateFrames(url: self.mutableVideoURL, fromTime: Float64(index))
}

    func generateFrames(url : NSURL, fromTime:Float64) {
        let asset: AVAsset = AVAsset(url: url as URL)
        let assetImgGenerate : AVAssetImageGenerator = AVAssetImageGenerator(asset: asset)
        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!)
            UIImageWriteToSavedPhotosAlbum(frameImg, nil, nil, nil)//I saved here to check
            videoFrames.append(frameImg)
            print("-----Array of video frames *** \(videoFrames)")
        } else {
              print("error !!!")
        }
    }

我用2个视频测试了这段代码(视频的长度是5秒和3.45分钟)。此代码与小持续时间(视频长度:5秒)和长持续时间(视频长度:3.45分钟)完美配合,NSLog显示来自调试器的消息:由于内存问题而终止 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:10)

生成超过1帧时,Apple建议使用以下方法: generateCGImagesAsynchronously(forTimes:completionHandler:)

但是,如果您更愿意遵循当前的方法,可以采取一些改进来减少内存使用:

  • 您正在循环中实例化AVAssetAVAssetImageGenerator,您只需将它们实例化一次并将其发送到方法generateFrames
  • 删除行

    UIImageWriteToSavedPhotosAlbum(frameImg, nil, nil, nil)//I saved here to check

    因为您要保存照片中的每一帧 专辑,需要额外的记忆。

最终结果可能如下所示:

var videoFrames:[UIImage] = [UIImage]
let asset:AVAsset = AVAsset(url:self.mutableVideoURL as URL)
let assetImgGenerate:AVAssetImageGenerator = AVAssetImageGenerator(asset:asset)
assetImgGenerate.appliesPreferredTrackTransform = true
let duration:Float64 = CMTimeGetSeconds(asset.duration)
let durationInt:Int = Int(mutableVideoDuration)

for index:Int in 0 ..< durationInt
{
   generateFrames(
      assetImgGenerate:assetImgGenerate,
      fromTime:Float64(index))
}
func generateFrames(
   assetImgGenerate:AVAssetImageGenerator,
   fromTime:Float64)
{
   let time:CMTime = CMTimeMakeWithSeconds(fromTime, 600)
   let cgImage:CGImage?

   do
   {
      cgImage = try assetImgGenerate.copyCGImage(at:time, actualTime:nil)
   }
   catch
   {
      cgImage = nil
   }

   guard

      let img:CGImage = cgImage

   else
   {
       continue
   }

   let frameImg:UIImage = UIImage(cgImage:img)
   videoFrames.append(frameImg)
}

Swift 4.2的更新

var videoUrl:URL // use your own url
var frames:[UIImage]
private var generator:AVAssetImageGenerator!

func getAllFrames() {
   let asset:AVAsset = AVAsset(url:self.videoUrl)
   let duration:Float64 = CMTimeGetSeconds(asset.duration)
   self.generator = AVAssetImageGenerator(asset:asset)
   self.generator.appliesPreferredTrackTransform = true
   self.frames = []
   for index:Int in 0 ..< Int(duration) {
      self.getFrame(fromTime:Float64(index))
   }
   self.generator = nil
}

private func getFrame(fromTime:Float64) {
    let time:CMTime = CMTimeMakeWithSeconds(fromTime, preferredTimescale:600)
    let image:CGImage
    do {
       try image = self.generator.copyCGImage(at:time, actualTime:nil)
    } catch {
       return
    }
    self.frames.append(UIImage(cgImage:image))
}