2013-04-23 821 views
1

我需要從網站服務的照片庫重新編碼視頻文件。 我嘗試了下面的代碼,但它發生錯誤,如'視頻作品必須有作曲說明'。如何更改(修改)視頻幀速率和比特率?

(代碼)

AVAsset *anAsset = [[AVURLAsset alloc] initWithURL:videoFileUrl options:nil]; 
NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:anAsset]; 
if ([compatiblePresets containsObject:AVAssetExportPresetMediumQuality]) { 

    self.exportSession = [[AVAssetExportSession alloc] 
          initWithAsset:anAsset presetName:AVAssetExportPresetPassthrough]; 



    AVMutableComposition* mixComposition = [[AVMutableComposition alloc] init]; 

    AVMutableCompositionTrack *firstTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; 
    [firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, anAsset.duration) ofTrack:[[anAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil]; 

    AVMutableVideoCompositionLayerInstruction *FirstlayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:firstTrack]; 


    AVMutableVideoCompositionInstruction * MainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction]; 
    MainInstruction.layerInstructions = [NSArray arrayWithObjects:FirstlayerInstruction,nil]; 


    AVMutableVideoComposition *MainCompositionInst = [AVMutableVideoComposition videoComposition]; 
    MainCompositionInst.frameDuration = CMTimeMake(1, 30); // bit rate 
    MainCompositionInst.renderSize = CGSizeMake(640, 480); // frame rate 

    [self.exportSession setVideoComposition:MainCompositionInst]; 

    NSURL *furl = [NSURL fileURLWithPath:self.tmpVideoPath]; 

    self.exportSession.outputURL = furl; 

    self.exportSession.outputFileType = AVFileTypeQuickTimeMovie; 

    CMTime start = CMTimeMakeWithSeconds(self.startTime, anAsset.duration.timescale); 
    CMTime duration = CMTimeMakeWithSeconds(self.stopTime-self.startTime, anAsset.duration.timescale); 
    CMTimeRange range = CMTimeRangeMake(start, duration); 
    self.exportSession.timeRange = range; 

    self.trimBtn.hidden = YES; 
    self.myActivityIndicator.hidden = NO; 
    [self.myActivityIndicator startAnimating]; 
    [self.exportSession exportAsynchronouslyWithCompletionHandler:^{ 

     switch ([self.exportSession status]) { 
      case AVAssetExportSessionStatusFailed: 
       NSLog(@"Export failed: %@", [[self.exportSession error] localizedDescription]); 
       break; 
      case AVAssetExportSessionStatusCancelled: 
       NSLog(@"Export canceled"); 
       break; 
      default: 
       NSLog(@"NONE"); 
       dispatch_async(dispatch_get_main_queue(), ^{ 
        [self.myActivityIndicator stopAnimating]; 
        self.myActivityIndicator.hidden = YES; 
        self.trimBtn.hidden = NO; 
        [self playMovie:self.tmpVideoPath]; 
       }); 

       break; 
     } 
    }]; 

} 

}

而不改變幀速率和位速率,它完美。 請給我任何建議。 謝謝。

+0

如果我設置MainCompositionInst.instructions = [NSArray的arrayWithObject:MainInstruction]。 ,我收到其他錯誤'操作無法完成。 (AVFoundationErrorDomain錯誤-11841' – user1471568 2013-04-23 05:58:41

回答

0

不確定,但有些事情要看。 Unfortunatley的錯誤信息在AVFoundation中並沒有告訴你這些情況。

一般來說,我會讓事情變得簡單並慢慢添加功能。要開始,請確保所有圖層都從零開始,然後在最終持續時間結束。爲主要作品做同樣的事情。無效的時間可能會給你這樣的錯誤。 對於您的指導,請確保它在同一時間開始,並在同一時間結束