【问题标题】:how to create a VTCompressionSession in swift?如何快速创建 VTCompressionSession?
【发布时间】:2014-06-10 05:45:38
【问题描述】:

我一直在寻找如何创建一个 VTCompressionSessionin swift)在2014 WWDC Video - 'Direct Access to Video Encoding and Decoding' 中提到。

以下代码适用于 Objective-C:

#import <Foundation/Foundation.h>
#import <VideoToolbox/VideoToolbox.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        VTCompressionSessionRef session;
        VTCompressionSessionCreate(NULL, 500, 500, kCMVideoCodecType_H264, NULL, NULL, NULL, NULL, NULL, &session);

        NSLog(@"created VTCompressionSession");
    }
    return 0;
}

但无论我尝试了什么,我都找不到将VTCompressionSessionCreate 导入到 swift 的方法。

import Foundation
import VideoToolbox

VideoToolbox.VTCompressionSessionCreate()

println("created VTCompressionSession")

此代码例如中断:Module 'VideoToolbox' has no member named 'VTCompressionSessionCreate'
只需调用 VTCompressionSessionCreate 就会创建错误消息 Use of unresolved identifier 'VTCompressionSessionCreate'

看起来 API 没有在 swift 中公开,因为我可以调用像 VTCompressionSessionEncodeFrame 这样的方法就好了。我错过了什么明显的东西吗?

【问题讨论】:

    标签: objective-c macos cocoa swift xcode6


    【解决方案1】:

    我的解决方法是编写一个objective-c 函数并通过桥接头将其公开给swift:

    压缩.h

    #import <VideoToolbox/VideoToolbox.h>
    VTCompressionSessionRef CreateCompressionSession();
    

    压缩.m

    #import <Cocoa/Cocoa.h>
    #import <Foundation/Foundation.h>
    #import <VideoToolbox/VideoToolbox.h>
    
    VTCompressionSessionRef CreateCompressionSession(){
        NSLog(@"CreateCompressionSession");
        VTCompressionSessionRef session;
        VTCompressionSessionCreate(NULL, 500, 500, kCMVideoCodecType_H264, NULL, NULL, NULL, NULL, NULL, &session);
        return session;
    }
    

    stream-Bridging-Header.h

    #import "CompressionSession.h"
    

    现在您可以在 swift 中运行以下代码:

    import Cocoa
    import Foundation
    import VideoToolbox
    
    let session = CreateCompressionSession()
    

    【讨论】:

      【解决方案2】:

      这是我在 swift 中的做法:

      var session: Unmanaged<VTCompressionSession>?
      VTCompressionSessionCreate(nil, 320, 200, CMVideoCodecType(kCMVideoCodecType_H264), nil, nil, nil, nil, nil, &session)
      compressionSession = session?.takeRetainedValue()
      

      在哪里

      var compressionSession: VTCompressionSessionRef?
      

      别忘了import CoreMedia

      【讨论】:

      • 这个例子不再(如果它曾经工作过)在 OS X 10.10 或 iOS 8 上工作。为 VTCompressionSessionCreate outputCallback 参数(第 8 个参数)传递 nil 会给出错误代码 -12092。跨度>
      • 上面会给出类型错误。在 xcode 7/swift 2 中,以下可能有效: var session: UnsafeMutablePointer = nil let status = VTCompressionSessionCreate(kCFAllocatorDefault, Int32(pixelWidth), Int32(pixelHeight), kCMVideoCodecType_H264, nil, nil, nil /* 使用默认值 * /,无,无,会话)
      【解决方案3】:

      对于 Swift 3:

      var compressionSesionOut = UnsafeMutablePointer<VTCompressionSession?>.allocate(capacity: 1)
      VTCompressionSessionCreate(nil, 100, 100, kCMVideoCodecType_H264, nil, nil, nil, nil, nil, compressionSesionOut)
      

      然后,你可以这样访问它,例如:

      let vtCompressionSession: VTCompressionSession = compressionSesionOut.pointee.unsafelyUnwrapped
      VTSessionSetProperty(vtCompressionSession, kVTCompressionPropertyKey_RealTime, kCFBooleanTrue)
      

      【讨论】:

        猜你喜欢
        • 2023-03-28
        • 1970-01-01
        • 1970-01-01
        • 2015-10-13
        • 2015-06-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-13
        相关资源
        最近更新 更多