2012-12-18 41 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