2013-04-23 166 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中并没有告诉你这些情况。

一般来说,我会让事情变得简单并慢慢添加功能。要开始,请确保所有图层都从零开始,然后在最终持续时间结束。为主要作品做同样的事情。无效的时间可能会给你这样的错误。 对于您的指导,请确保它在同一时间开始,并在同一时间结束