2015-10-04 105 views
1

我正在實現AVAssetExportSession在線修剪視頻,但始終返回失敗。AVAssetExportSession exportAsynchronouslyWithCompletionHandler返回失敗

這是我實現:

NSString *url = @"http://www.ebookfrenzy.com/ios_book/movie/movie.mov"; 
NSURL *fileURL = [NSURL URLWithString:url]; 
AVAsset *asset = [AVAsset assetWithURL:fileURL]; 
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality]; 
NSURL *exportUrl = [NSURL fileURLWithPath:[documentsDirectory stringByAppendingPathComponent:@"export.m4a"]]; 

exportSession.outputURL = exportUrl; 
exportSession.outputFileType = AVFileTypeQuickTimeMovie; 
CMTime time = CMTimeMake(1, 10); 
exportSession.timeRange = CMTimeRangeMake(kCMTimeZero, time); 
[exportSession exportAsynchronouslyWithCompletionHandler:^(void) { 

    switch (exportSession.status) 
    { 
     case AVAssetExportSessionStatusCompleted: 
      /*expor is completed*/ 
      NSLog(@"Completed!!"); 
      break; 
      case AVAssetExportSessionStatusFailed: 
      NSLog(@"failed!!"); 
      /*failed*/ 
      break; 
     default: 
      break; 
    } 
}]; 

任何的你知道爲什麼出現這種情況還是什麼,我做錯了什麼?

回答

1

您正嘗試使用遠程URL創建AVAsset,並且您需要知道該資產已加載,然後才能開始導出。

AVAsset符合AVAsynchronousKeyValueLoading協議,這意味着你可以觀察tracks鍵啓動出口,一旦價值的變化:

NSURL *myURL = [NSURL URLWithString:myMovieURLString]; 
AVAsset *asset = [AVAsset assetWithURL:myURL]; 

__weak typeof(self) weakSelf = self; 

[asset loadValuesAsynchronouslyForKeys:@[@"tracks"] completionHandler:^{ 

    //Error checking here - make sure there are tracks 

    [weakSelf exportAsset:asset]; 

}]; 

然後你可以有你的出口代碼在一個單獨的方法:

- (void)exportAsset:(AVAsset *)asset { 

    //Your export code here 
} 
+0

我實現了你的解決方案和AVKeyValueStatus裏面的loadValuesAsynchronouslyForKeys塊,但我總是得到AVKeyValueStatusUnknown。你知道爲什麼嗎? – user2924482

0

documentsDirectory應該是一個正在退出的路徑。如果它不,exportSession.status將等於AVAssetExportSessionStatus失敗

相關問題