如何更改(修改)视频帧速率和比特率?

时间:2013-04-23 05:46:45

标签: avassetexportsession avasset

我需要从照片库重新编码视频文件以进行网站服务。 我试过下面的代码,但它发生了错误,比如'视频合成必须有合成指令'。

(代码)

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;
        }
    }];

}

}

没有改变帧速率和比特率,它完美地工作。 请给我任何建议。 感谢。

2 个答案:

答案 0 :(得分:1)

帧率我还在寻找,但比特率已经通过替换AVAssetExportSession而得到解决:https://github.com/rs/SDAVAssetExportSession

答案 1 :(得分:0)

不确定但要注意一些事情。不幸的是,在AVFoundation中,这些错误信息并没有告诉你多少。

一般来说,我会简化并慢慢添加功能。要开始,请确保所有图层从零开始,然后在最终持续时间结束。对主要构图做同样的事情。无效的时间可能会给您一个这样的错误。 为了您的指示,请确保它同时开始并在同一时间结束