【发布时间】:2021-01-25 02:41:54
【问题描述】:
Earlier I asked a question regarding generateCGImagesAsynchronously。值得庆幸的是,它得到了回答并且效果很好。
问题在于它仅在 xcode 上用作 Cocoa 应用程序。我现在正试图将此逻辑移动到可执行的 Swift 包中,但 AVFoundation 代码,例如 generateCGImagesAsynchronously,将不起作用。也就是说,没有引发错误,但这些函数似乎被嘲笑了。我认为这可能与我的包被沙盒化有关?我能够从我之前为其编写代码的 Cocoa 应用程序中删除沙箱,但我不知道如何为这个可执行文件执行此操作。
我是 Swift 的新手,并试图理解它,但想到我希望我的代码执行的操作取决于我正在使用的 IDE,这有点令人沮丧。
如果有人能指出我在文档或其他资源中的阅读方向,了解如何在不使用 xcode 的情况下制作程序,那就太好了。谢谢!
这是我的代码:
import Darwin
import Foundation
import AppKit
import AVFoundation
import Cocoa
@discardableResult func writeCGImage(
_ image: CGImage,
to destinationURL: URL
) -> Bool {
guard let destination = CGImageDestinationCreateWithURL(
destinationURL as CFURL,
kUTTypePNG,
1,
nil
) else { return false }
CGImageDestinationAddImage(destination, image, nil)
return CGImageDestinationFinalize(destination)
}
func imageGenCompletionHandler(
requestedTime: CMTime,
image: CGImage?,
actualTime: CMTime,
result: AVAssetImageGenerator.Result,
error: Error?
) {
guard let image = image else { return }
let path = saveToPath.appendingPathComponent(
"img\(actualTime).png"
)
writeCGImage(image, to: path)
}
let arguments: [String] = Array(CommandLine.arguments.dropFirst())
// For now, we assume the second arg, which is the
// path that the user wants us to save to, always exists.
let saveToPath = URL(fileURLWithPath: arguments[1], isDirectory: true)
let vidURL = URL(fileURLWithPath: arguments[0])
let vidAsset = AVAsset(url: vidURL)
let vidDuration = vidAsset.duration
let imageGen = AVAssetImageGenerator(asset: vidAsset)
var frameForTimes = [NSValue]()
let sampleCounts = 20
let totalTimeLength = Int(truncatingIfNeeded: vidDuration.value as Int64)
let steps = totalTimeLength / sampleCounts
for sampleCount in 0 ..< sampleCounts {
let cmTime = CMTimeMake(
value: Int64(sampleCount * steps),
timescale: Int32(vidDuration.timescale)
)
frameForTimes.append(NSValue(time: cmTime))
}
imageGen.generateCGImagesAsynchronously(
forTimes: frameForTimes,
completionHandler: imageGenCompletionHandler
)
【问题讨论】: