2

我需要为网站服务重新编码照片库中的视频文件。我尝试了下面的代码,但出现了“视频合成必须有合成说明”之类的错误。

(代码)

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

}

}

无需更改帧速率和比特率,它就可以完美运行。请给我任何建议。谢谢。

4

2 回答 2

1

帧率我仍在寻找,但比特率已经通过这种替代解决了AVAssetExportSessionhttps ://github.com/rs/SDAVAssetExportSession

于 2014-07-18T15:00:37.073 回答
0

不确定,但有些事情要看。不幸的是,在 AVFoundation 中的这些情况下,错误消息并没有告诉你太多。

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

于 2013-10-25T15:43:56.417 回答