简体   繁体   English

如何在Swift中从本地视频文件中获取帧?

[英]How to get frames from a local video file in Swift?

I need to get the frames from a local video file so i can process them before the video is played. 我需要从本地视频文件中获取帧,以便可以在播放视频之前对其进行处理。 I already tried using AVAssetReader and VideoOutput. 我已经尝试使用AVAssetReader和VideoOutput。

[EDIT] Here is the code i used from Accesing Individual Frames using AV Player [编辑]这是我使用AV Player访问单个帧时使用的代码

let asset = AVAsset(URL: inputUrl)
let reader = try! AVAssetReader(asset: asset)

let videoTrack = asset.tracksWithMediaType(AVMediaTypeVideo)[0]

// read video frames as BGRA
let trackReaderOutput = AVAssetReaderTrackOutput(track: videoTrack, outputSettings:[String(kCVPixelBufferPixelFormatTypeKey): NSNumber(unsignedInt: kCVPixelFormatType_32BGRA)])

reader.addOutput(trackReaderOutput)
reader.startReading()

while let sampleBuffer = trackReaderOutput.copyNextSampleBuffer() {
    print("sample at time \(CMSampleBufferGetPresentationTimeStamp(sampleBuffer))")
    if let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) {
        // process each CVPixelBufferRef here
        // see CVPixelBufferGetWidth, CVPixelBufferLockBaseAddress, CVPixelBufferGetBaseAddress, etc
    }
}

I believe AVAssetReader should work. 我相信AVAssetReader应该可以工作。 What did you try? 你尝试了什么? Have you seen this sample code from Apple? 您看过Apple的示例代码吗? https://developer.apple.com/library/content/samplecode/ReaderWriter/Introduction/Intro.html https://developer.apple.com/library/content/samplecode/ReaderWriter/Introduction/Intro.html

You can have a look at VideoToolbox : https://developer.apple.com/documentation/videotoolbox 您可以看一下VideoToolbox: https : //developer.apple.com/documentation/videotoolbox

But beware: this is close to the hardware decompressor and sparsely documented terrain. 但要注意:这靠近硬件解压缩器和稀疏记录的地形。

Depending on what processing you want to do, OpenCV may be a an option - in particular if you are detecting or tracking objets in your frames. 根据您要执行的处理,OpenCV可能是一个选项-特别是如果您正在检测或跟踪帧中的对象。 If your needs are simpler, then the effort to use OpenCV with swift may be a little too much - see below. 如果您的需求比较简单,那么快速使用OpenCV可能会花费太多精力-请参阅下文。

You can open a video, read it frame by frame, do your work on the frames and then display then - bearing in mind the need to be efficient to avoid delaying the display. 您可以打开视频,逐帧读取视频,在帧上进行工作,然后再显示-请注意要有效率,以避免延迟显示。

The basic code structure is quite simple - this is a python example but the same principles apply across supported languages 基本代码结构非常简单-这是一个python示例,但相同的原理适用于受支持的语言

import numpy as np
import cv2

cap = cv2.VideoCapture('vtest.avi')

while(cap.isOpened()):
    ret, frame = cap.read()

    //Do whatever work you want on the frame here - in this example
    //from the tutorial the image is being converted from one colour 
    //space to another
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    //This displays the resulting frame 
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

More info here: http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html 此处提供更多信息: http : //opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html

The one caveat is that using OpenCV with swift requires some additional effort - this is a good example, but it evolves constantly so it is worth searching for if you decide to go this way: https://medium.com/@yiweini/opencv-with-swift-step-by-step-c3cc1d1ee5f1 一个警告是,快速使用OpenCV需要付出额外的努力-这是一个很好的例子,但是它不断发展,因此如果您决定采用这种方式,则值得寻找: https : //medium.com/@yiweini/opencv -with-迅速步一步-c3cc1d1ee5f1

I found out what the problem was! 我发现了问题所在! It was with my implementation. 这是我的实现。 The code i posted is correct. 我发布的代码是正确的。 Thank you all 谢谢你们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM