gpt4 book ai didi

ios - 无法从共享扩展 ios 保存视频

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:32:01 26 4
gpt4 key购买 nike

我创建了一个共享扩展,其目的是将视频共享到我的容器应用程序。作为共享流程的一部分,我想检查用户是否登录等。所以我创建了一个应用程序组并在容器应用程序和扩展之间共享数据。(同时使用 NSUserDefaults 和核心数据)

 let  sharedUserDefaults = NSUserDefaults(suiteName:"group.app.group.name")
lazy var applicationDocumentsDirectory: NSURL = {
NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.app.group.name")!
}()

这工作正常。

现在我正尝试使用 SDAVAssetExportSession 压缩视频

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH-mm-ss"
let timeStamp = NSDate()
let asset = AVAsset(URL: self.shareVideoURL)
let exportSession = SDAVAssetExportSession(asset: asset)
let outputURL = self.applicationDocumentsDirectory.URLByAppendingPathComponent("\(dateFormatter.stringFromDate(timeStamp)).MP4")
exportSession.outputURL = outputURL
exportSession.outputFileType = AVFileTypeMPEG4
exportSession.shouldOptimizeForNetworkUse = true

let assetTrack = asset.tracksWithMediaType(AVMediaTypeVideo).first!

let videoWidth:NSNumber = assetTrack.naturalSize.width, videoHeight:NSNumber = assetTrack.naturalSize.height,
let desiredBitrate:NSNumber = 3*1024*1024 , desiredKeyFrameInterval:NSNumber = 20
exportSession.videoSettings = [AVVideoCodecKey:AVVideoCodecH264,
AVVideoWidthKey:videoWidth,
AVVideoHeightKey:videoHeight,
AVVideoCompressionPropertiesKey: [
AVVideoAverageBitRateKey:desiredBitrate,
AVVideoMaxKeyFrameIntervalKey:desiredKeyFrameInterval]]

exportSession.audioSettings = [AVFormatIDKey: NSNumber(unsignedInt: kAudioFormatMPEG4AAC),
AVNumberOfChannelsKey: 2,
AVSampleRateKey: 44100,
AVEncoderBitRateKey: 64000]

exportSession.exportAsynchronouslyWithCompletionHandler({
switch exportSession.status {
case .Completed:
print("Compressed Video size : \(Double(NSData(contentsOfURL: outputURL)!.length)/(1024*1024)) mb")
case .Failed:
print("Export Failed :\(exportSession.error)")
case .Cancelled:
print("Export Cancelled :\(exportSession.error)")
default:
print("complete")
}
})

当我在共享扩展中执行此操作时出现错误:

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

当我在另一个使用文档目录的应用程序中运行这段代码时,它工作正常,没有任何问题,但是当我开始使用应用程序组容器时,它不允许保存文件。

任何人都可以指出正确的方向以将压缩文件保存在共享应用程序组容器中

最佳答案

我找不到 AVAssetExportSession 的解决方案,开始直接使用 AVAssetWriter,然后也通过 SDAVAssetExportSession

而且它看起来很有效,它可以保存视频。所以,请检查,您要保存到的 URL 对应于 this.

我重写了从链接到 Objective-C 的 Swift 代码

NSURL *groupPath = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier: @"group.**.***"];
groupPath = [groupPath URLByAppendingPathComponent: @"Library" isDirectory: YES];
groupPath = [groupPath URLByAppendingPathComponent:@"Caches" isDirectory: YES];

Rao's 之后添加的代码评论

希望你能解决这个问题,让我了解最新情况。

关于ios - 无法从共享扩展 ios 保存视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37026547/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com