我创建了一个分享扩展程序,其目的是将视频分享到我的容器应用程序。 作为共享流程的一部分,我想检查用户是否登录等。 所以我创建了一个应用程序组,并在容器应用程序和扩展程序之间共享数据。(使用NSUserDefaults和Core Data)
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")
}
})
当我在share-extension中执行此操作时,我收到错误:
Error Domain=NSURLErrorDomain Code=-3000 "Cannot create file"
UserInfo={NSLocalizedDescription=Cannot create file, NSUnderlyingError=0x160900450
{Error Domain=NSOSStatusErrorDomain Code=-12149 "(null)"}}
当我使用文档目录在另一个应用程序中运行此代码时,它运行正常,没有任何问题,但是当我开始使用应用程序组容器时,它不允许保存文件。
任何人都可以指出我正确的方向将压缩文件保存在共享应用程序组容器中
答案 0 :(得分:2)
我找不到AVAssetExportSession的解决方案并开始直接使用AVAssetWriter,然后也通过SDAVAssetExportSession
似乎它有效,它可以保存视频。 因此,请检查您要保存的网址是否与this.
相对应我从链接到Objective-C重写了Swift代码
NSURL *groupPath = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier: @"group.**.***"];
groupPath = [groupPath URLByAppendingPathComponent: @"Library" isDirectory: YES];
groupPath = [groupPath URLByAppendingPathComponent:@"Caches" isDirectory: YES];
在Rao's评论 之后添加了 代码
希望你能解决这个问题,让我更新。
答案 1 :(得分:0)
Apps and Extensions无权写入共享容器本身,但他们确实有权写入该共享容器内的" Library / Caches" 。