来自图像阵列的视频无法播放

时间:2014-01-03 04:55:24

标签: ios iphone xcode video avassetwriter

使用以下代码从图像阵列创建视频并将其保存在文档目录中。

 - (void)createVideo:(NSMutableArray *)passedArray
 {
        NSError *error = nil;
        NSFileManager *fileMgr = [NSFileManager defaultManager];
        NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
        NSString *videoOutputPath = [documentsDirectory stringByAppendingPathComponent:@"output.mov"];

        // get rid of existing mp4 if exists...
        if ([fileMgr removeItemAtPath:videoOutputPath error:&error] != YES)
            NSLog(@"Unable to delete file: %@", [error localizedDescription]);

        CGSize imageSize = CGSizeMake(640, 1136);
        NSUInteger fps = 5;
      //////////////     end setup    ///////////////////////////////////
      AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
                                      [NSURL fileURLWithPath:videoOutputPath] fileType:AVFileTypeQuickTimeMovie                                                           error:&error];
        NSParameterAssert(videoWriter);
        NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                       AVVideoCodecH264, AVVideoCodecKey,
                                       [NSNumber numberWithInt:imageSize.width], AVVideoWidthKey,
                                       [NSNumber numberWithInt:imageSize.height], AVVideoHeightKey,
                                       nil];

        AVAssetWriterInput* videoWriterInput = [AVAssetWriterInput
                                                assetWriterInputWithMediaType:AVMediaTypeVideo
                                                outputSettings:videoSettings];              AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor
                                                         assetWriterInputPixelBufferAdaptorWithAssetWriterInput:videoWriterInput
                                                         sourcePixelBufferAttributes:nil];
        NSParameterAssert(videoWriterInput);
        NSParameterAssert([videoWriter canAddInput:videoWriterInput]);
        videoWriterInput.expectsMediaDataInRealTime = YES;
        [videoWriter addInput:videoWriterInput];

        //Start a session:
        [videoWriter startWriting];
        [videoWriter startSessionAtSourceTime:kCMTimeZero];

        CVPixelBufferRef buffer = NULL;
            //convert uiimage to CGImage.
        int frameCount = 0;
        double numberOfSecondsPerFrame = 1;
        double frameDuration = fps * numberOfSecondsPerFrame;

        for(UIImage * img in passedArray)
        {
            buffer = [self pixelBufferFromCGImage:[img CGImage]];
            BOOL append_ok = NO;
            int j = 0;
            while (!append_ok && j < 5) {
                if (adaptor.assetWriterInput.readyForMoreMediaData)  {
                    CMTime frameTime = CMTimeMake(frameCount*frameDuration,(int32_t) fps);
                    append_ok = [adaptor appendPixelBuffer:buffer withPresentationTime:frameTime];
                    if(!append_ok){
                        NSError *error = videoWriter.error;
                        if(error!=nil) {
                            NSLog(@"Unresolved error %@,%@.", error, [error userInfo]);
                        }
                    }
                }
                else {
                    printf("adaptor not ready %d, %d\n", frameCount, j);
                    [NSThread sleepForTimeInterval:0.1];
                }
                j++;
            }
            if (!append_ok) {
                printf("error appending image %d times %d\n, with error.", frameCount, j);                }
            frameCount++;
        }
        [videoWriterInput markAsFinished];
}

名称为“output.mov”的文件保存在文档目录中,但没有播放。请帮助我在其中缺少。

1 个答案:

答案 0 :(得分:1)

我错过了一条线  [videoWriter finishWriting];

在完成视频创建后添加后,保存的视频可播放。