2012-12-18 46 views
1

我已经搜索并阅读了文档,但似乎无法找到解决此(看似简单)问题的方法。我从用户的iTunes资料库导出的歌曲正常工作,并且每次都没有问题地下载到用户的文档文件夹中,但视频似乎不起作用。从用户的iTunes库导出视频

我有它显示MPMediaPickerController(allowsPickingMultipleItems = YES),允许用户从他们下载的库中选择视频或歌曲。完成后,这里是relavent代码我使用:

- (void)mediaPicker:(MPMediaPickerController*)mediaPicker didPickMediaItems:(MPMediaItemCollection*)mediaItemCollection { 
    AVAssetExportSession *exportSession; 

    for (MPMediaItem *item in mediaItemCollection.items) { 
     NSURL *assetUrl = [item valueForProperty:MPMediaItemPropertyAssetURL]; 
     MPMediaType type = [[item valueForProperty:MPMediaItemPropertyMediaType] intValue]; 
     if (type >= MPMediaTypeMovie) { 
      exportSession = [[AVAssetExportSession alloc] initWithAsset:[AVAsset assetWithURL:assetUrl] presetName:AVAssetExportPreset640x480]; 
      exportSession.outputFileType = AVFileTypeQuickTimeMovie; 
      filePath = [title stringByAppendingString:@".mov"]; 
      exportSession.outputURL = [NSURL fileURLWithPath:[[NSFileManager documentDirectory] stringByAppendingPathComponent:filePath]]; 
     } // .. check for song-types here and set session up appropriately 

     [exportSession exportAsynchronouslyWithCompletionHandler:^{ 
      // never gets into AVAssetExportSessionStatusCompleted here for videos 
     } 
    } 
} 

每次都遇到的错误是:

Error Domain=AVFoundationErrorDomain Code=-11820 "Cannot Complete Export" UserInfo=0x1e1a2180 {NSLocalizedRecoverySuggestion=Try exporting again., NSLocalizedDescription=Cannot Complete Export} 

不是非常有帮助。 :(我觉得我可能在这里错过了一些明显的东西,我是否正在以这种正确的方式进行讨论?这可能是我试图将它“强制”为MOV格式的问题吗?或者可能需要一种不同的设置方式出口会话?

仅供参考,我使用的是iOS 6.0.1在我的iPhone 5进行测试,用的6.0。感谢baseSDK提前为可在此提供的任何指导!

附加信息#1:这很奇怪,它似乎立即崩溃,如果我将outputFileType设置为“AVFileTypeAppleM4V”,则立即使用“SIGTRAP”..我想尝试M4V,因为当我执行assetURL的日志输出时,例如:ipod-library://item/item.m4v?id = 12345。不知道这是否会使不同与否,但奇怪的是,如果我尝试m4v格式,它就会崩溃。可能是因为它不在受支持的文件类型列表中(请参阅下一个信息点)。

附加信息#2:支持的文件类型,我得到(从调用 “supportedFileTypes” 的方法是: “com.apple.quicktime-电影” 和 “public.mpeg-4” 的 “exportPresetsCompatibleWithAsset” 包括。所有的视频,包括m4a,低/中/高质量,和特定尺寸的我已经尝试过所有这些的所有组合,例如AVFileTypeQuickTimeMovie和AVFileTypeMPEG4 for fileTypes,以及所有预设,包括低/中/高,所有尺寸的人的它永远不会失败,我得到了“无法完成导出”错误

附加信息#3:。我也是用的5.1部署目标,但,是的,我有特里d 6.0,它给出了相同的错误。 :(

附加信息#4:如果需要知道,我正在测试的电影是一个“Pilot”电视节目,一个视频,我在iTunes中看到的第一个免费视频。它在这个应用程序使用

附加信息#5:不知道这是重要的,但“hasProtectedContent”方法为AVAsset返回YES(和AVURLAsset如果我转换)可能不会有所作为,但认为我会把它扔出去

+0

我并不积极,但我认为'AVAssetExportSession:initWithAsset:presetName:'函数应该用'AVURLAsset'而不是'AVAsset'来调用。如果是这样,您需要用'[AVURLAsset URLAssetWithURL:assetUrl options:nil]''替换'[AVAsset assetWithURL:assetUrl]'' – lnafziger

+0

感谢您对此的回复。不幸的是,当我使用在initWithAsset代码行中复制的AVURLAsset替换AVAsset时,我得到了相同的“无法完成导出”。 :(我将预设保存为AVAssetExportPresetLowQuality,将“shouldOptimizeForNetworkUse”设置为YES,并将outputFileType设置为AVFileTypeQuickTimeMovie(将文件路径设置为.mov),但我很欣赏其上的响应,但有任何其他想法可以尝试? – svguerin3

回答

2

在尝试复制问题并进行一些测试后,我强烈怀疑受保护的内容是一个问题。为什么:

我复制了你的代码,并在我的iPod Touch(第5代,iOS 6.0.1)上进行了测试,尽管不是来自媒体选择器,而是让它循环播放我拥有的所有视频设备(其中7个)。它运行良好,并调用完成处理程序并在应用程序沙箱的文档目录中创建适当的.mov文件。我将.mov文件移到我的Mac上,他们都玩。

这些视频文件的hasProtectedContent为NO。

因此,我放置了一个来自iTunes商店的视频文件,并确认它已将hasProtectedContent设置为YES。有趣的是,当我尝试从MPMediaItemPropertyAssetURL获取URL时,我得到了保护/ iTunes获得的视频的零。

我强烈怀疑媒体保护问题。

下面是我使用的代码的变体。我没有改变你的转换代码在所有,网址是多么提供:

// select all the video files 
MPMediaPropertyPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithInteger:MPMediaTypeMovie] forProperty:MPMediaItemPropertyMediaType]; 

MPMediaQuery *query = [[MPMediaQuery alloc] init]; 
[query addFilterPredicate:predicate]; 

NSArray *items = [query items]; 

// now go through them all to export them 

NSString* title; 
NSURL * url; 
AVAssetExportSession *exportSession; 
NSString *storePath; 
AVAsset *theAsset; 

// we fill put the output at this path 
NSString *applicationDocumentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 


// loop through the items and export 
for (MPMediaItem* item in items) 
{ 
    title = [item valueForProperty:MPMediaItemPropertyTitle]; 
    url = [item valueForProperty:MPMediaItemPropertyAssetURL]; 

    NSLog(@"Title: %@, URL: %@",title,url); 

    theAsset = [AVAsset assetWithURL:url]; 

    if ([theAsset hasProtectedContent]) { 
     NSLog(@"%@ is protected.",title); 
    } else { 
     NSLog(@"%@ is NOT protected.",title); 
    } 

    exportSession = [[AVAssetExportSession alloc] initWithAsset:theAsset presetName:AVAssetExportPreset640x480]; 

    storePath = [applicationDocumentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mov",title]]; 

    exportSession.outputFileType = AVFileTypeQuickTimeMovie; 

    exportSession.outputURL = [NSURL fileURLWithPath:storePath]; 

    [exportSession exportAsynchronouslyWithCompletionHandler:^{ 
     NSLog(@"done!"); 
    }]; 

} 

出于好奇,你检查AVAsset exportable标志?

+0

Spot昨天晚上我和苹果的一个人证实了我之前没有检查过“isExportable”标志,但是现在我已经做了一个快速检查,它对这个资产返回“NO”,Apple表示任何保护的媒体DRM)内容,即可以通过“hasProtectedContent”属性进行检查,不支持诸如播放,导出,获取元数据等操作。但是,我们自己创建的视频内容应该没问题。我有点担心,并且正在寻找这是有记载的吗?!很多时间浪费..无论如何,感谢您的答案,并花时间! – svguerin3