在实时iOS中访问视频帧

时间:2016-09-21 14:38:17

标签: ios swift video avfoundation

我必须从服务器访问视频,我希望能够实时访问其帧,以便我可以对它们进行一些图像处理。

我知道如何使用AVAssetReader离线执行此操作,但此解决方案不适用于实时处理。

1 个答案:

答案 0 :(得分:3)

您可以使用AVPlayerItemVideoOutput实时访问远程资产的框架(感谢objc.io)。

P.S。始终检查您的远程资产是否被App Transport Security阻止。

p.p.s AVAssetReader对远程资产is complicated的支持。

import UIKit
import AVFoundation

class ViewController: UIViewController {        
    let player = AVPlayer(url: URL(string: "http://0.s3.envato.com/h264-video-previews/80fad324-9db4-11e3-bf3d-0050569255a8/490527.mp4")!)
    let videoOutput = AVPlayerItemVideoOutput(pixelBufferAttributes: [String(kCVPixelBufferPixelFormatTypeKey): NSNumber(value: kCVPixelFormatType_32BGRA)])

    override func viewDidLoad() {
        super.viewDidLoad()

        player.currentItem!.add(videoOutput)
        player.play()

        let displayLink = CADisplayLink(target: self, selector: #selector(displayLinkDidRefresh(link:)))
        displayLink.add(to: RunLoop.main, forMode: RunLoopMode.commonModes)
    }

    func displayLinkDidRefresh(link: CADisplayLink) {
        let itemTime = videoOutput.itemTime(forHostTime: CACurrentMediaTime())

        if videoOutput.hasNewPixelBuffer(forItemTime: itemTime) {
            if let pixelBuffer = videoOutput.copyPixelBuffer(forItemTime: itemTime, itemTimeForDisplay: nil) {
                print("pixelBuffer \(pixelBuffer)") // yay, pixel buffer
                let image = CIImage(cvImageBuffer: pixelBuffer) // or maybe CIImage?
                print("CIImage \(image)")
            }
        }
    }
}