OGeek|极客世界-中国程序员成长平台

标题: ios - 共享扩展中的 AVAssetExportSession [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 20:07
标题: ios - 共享扩展中的 AVAssetExportSession

我正在尝试在共享扩展中选择的视频上使用 AVAssetExportSession 并获取

Error Domain=NSURLErrorDomain Code=-3000 "Cannot create file" UserInfo={NSLocalizedDescription=Cannot create file, NSUnderlyingError=0x14811fdb0 {Error Domain=NSOSStatusErrorDomain Code=-12124 "(null)"}}

但是我可以在同一个 NSURL 上手动创建文件而不会出错。这是我正在使用的功能

func reencodeVideo() {
    let videoAsset = AVURLAsset(URL: video.url)

    let videoTrack = videoAsset.tracksWithMediaType(AVMediaTypeVideo)[0] as AVAssetTrack
    print(videoTrack.estimatedDataRate)
    let exportSession = AVAssetExportSession(asset: videoAsset, presetName: AVAssetExportPreset1920x1080)
    guard let outputURL = uploadableFileURL else {
        return
    }
    let fileManager = NSFileManager.defaultManager()
    // let created = fileManager.createFileAtPath(outputURL.path!, contents: nil, attributes: nil)
    if let path = outputURL.path where fileManager.fileExistsAtPath(path) {
        print("file exists")
    }
    do {
        try fileManager.removeItemAtURL(outputURL)
        print("deleted")
    } catch {
        print(error)
    }
    exportSession?.outputURL = outputURL
    exportSession?.outputFileType = AVFileTypeQuickTimeMovie

    exportSession?.exportAsynchronouslyWithCompletionHandler{
        print(exportSession?.status)
    }
}

private var uploadableFileURL: NSURL? {
    guard let tempFileName = video.url.lastPathComponent else {
        return nil
    }
    let fileManager = NSFileManager.defaultManager()
    guard let containerURL = fileManager.containerURLForSecurityApplicationGroupIdentifier(Constants.appGroupIdentifier) else {
        return nil
    }
    return containerURL.URLByAppendingPathComponent("videoFile.mov")
}

我已在同一目录中成功创建文件,但 AVAssetExportSession 在那里返回错误。 任何想法我做错了什么?

我尝试使用 AVAssetReaderAVAssetWriter,而 AVAssetWriter 在尝试启动时返回相同的错误。如果我使用 Documents 目录,则编码过程成功完成,并且仅在使用共享应用程序组容器时失败。



Best Answer-推荐答案


您的问题可能与使用文档文件夹和 icloud 同步有关。 见 https://forums.developer.apple.com/message/77495#77495

如果你这样做:

guard let containerURL = fileManager.containerURLForSecurityApplicationGroupIdentifier(Constants.appGroupIdentifier) else {
        return nil
}

let libraryURL = containerURL.URLByAppendingPathComponent("Library", isDirectory: true)
let cachesURL = libraryURL.URLByAppendingPathComponent("Caches", isDirectory: true)
return cachesURL.URLByAppendingPathComponent("videoFile.mov")

关于ios - 共享扩展中的 AVAssetExportSession,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37427804/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4