2015-07-13 58 views
4

我在使用AVExportSession修剪和下載在線視頻。AVAssetExportSession修剪和下載

代碼:

FileMove *fileMove = (FileMove*)data; 
    NSString *url = @"http://download.wavetlan.com/SVV/Media/HTTP/H264/Talkinghead_Media/H264_test1_Talkinghead_mp4_480x360.mp4"; 
    NSURL *videoURL = [NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
    NSLog(@"VideoURL: %@",videoURL); 


    AVAsset *anAsset = [AVURLAsset URLAssetWithURL:videoURL options:nil];  
    NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:anAsset]; 
    if ([compatiblePresets containsObject:AVAssetExportPresetLowQuality]) { 
     AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] 
               initWithAsset:anAsset presetName:AVAssetExportPresetLowQuality]; 
     NSURL *outputURL = [NSURL fileURLWithPath:fileMove.dst]; 
     NSLog(@"outputURL: %@",outputURL); 
     exportSession.outputURL = outputURL; 
     exportSession.outputFileType = AVFileTypeQuickTimeMovie; 

     CMTime start = CMTimeMakeWithSeconds(1.0, 600); 
     CMTime duration = CMTimeMakeWithSeconds(3.0, 600); 
     CMTimeRange range = CMTimeRangeMake(start, duration); 
     exportSession.timeRange = range; 

     if ([[NSFileManager defaultManager] fileExistsAtPath:fileMove.dst]) 
      [[NSFileManager defaultManager] removeItemAtPath:fileMove.dst error:nil]; 

     [exportSession exportAsynchronouslyWithCompletionHandler:^{ 

      switch ([exportSession status]) { 
       case AVAssetExportSessionStatusFailed: 
        NSLog(@"Export failed: %@", [[exportSession error]description ]); 
        break; 
       case AVAssetExportSessionStatusCancelled: 
        NSLog(@"Export canceled"); 
        break; 
       default: 
        break; 
      } 
     }]; 
    } 

錯誤:

Export failed: Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo=0x635a820 {NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x1ff4240 "The operation couldn’t be completed. (OSStatus error -12780.)", NSLocalizedFailureReason=An unknown error occurred (-12780)}

你們是否看到任何問題的代碼? AVExportSession訪問在線視頻有沒有限制?

回答

0

我想我們是不是能夠出口在線視頻 在這裏看到: Unable to export AVPlayerItem

所以我想,你可以下載視頻首先在本地,然後修剪,保存和使用它。

+0

我必須在線修剪。請以其他方式提出建議。 – objectivecdeveloper

1

用於創建AVAsset的URL需要是您有權訪問的本地文件。您需要在創建AVAsset之前將其下載到設備上like so

+0

我必須在線修剪。請以其他方式提出建議。 – objectivecdeveloper

+0

首先在本地下載文件 – Lefteris

+0

如果您無法將其下載到設備,您需要設置一個Web服務器,該服務器可以下載該文件並修剪該文件並向客戶端返回一個新的URL。這是一個可怕的和昂貴的解決方案,所以我懷疑你會通過它。你幾乎沒有其他選擇。 –

1

我設法使用AVFoundation修剪遠程視頻。這裏是寫在斯威夫特的示例代碼:

let range: CMTimeRange 
let sourceURL: NSURL 
let targetFileURL: NSURL 
let requiredKeys = [ "exportable", "tracks" ] 
let asset = AVAsset(URL: sourceURL) 
asset.loadValuesAsynchronouslyForKeys(requiredKeys) { 

    // Error handling code here 
    precondition(asset.statusOfValueForKey("exportable", error: nil) == .Loaded) 
    precondition(asset.statusOfValueForKey("tracks", error: nil) == .Loaded) 
    precondition(asset.exportable) 


    let composition = AVMutableComposition() 
    do { 
     try composition.insertTimeRange(range, ofAsset: asset, atTime: kCMTimeZero) 
    } catch { 
     // Error handling code here 
     return 
    } 

    let finalComposition = composition.copy() as! AVComposition 
    guard let export = AVAssetExportSession(asset: finalComposition, presetName: AVAssetExportPresetPassthrough) else { 
     // Error handling code here 
     return 
    } 

    export.outputURL = targetFileURL 
    export.outputFileType = AVFileTypeMPEG4 
    export.exportAsynchronouslyWithCompletionHandler { 
     switch export.status { 
     case .Completed: 
      // Alright! 
      break 

     case .Cancelled, .Failed: 
      // Error handling code here 
      break 

     default: 
      fatalError("Shouldn't be called") 
     } 
    } 
}