ios - AVAssetWriter 为影片输出添加绿框

标签 ios video h.264 avassetwriter

我正在完成一个将电影保存到相册的 iPhone 应用程序。电影源是​​一组图像。我用它制作电影并将它们很好地放入相册,但它总是在开始时有一个额外的绿色框架。

有什么想法吗?

我重新阅读了 Apple 的文档,晃动了电线,并用编号的图像做了一些测试,以确认它没有掉帧或类似的东西。仍然没有找到正确的方法。

    //build save path with time
NSMutableString *buildPath = [[NSMutableString alloc] init];
[buildPath setString:@"Documents/"];
[buildPath appendString:@"temporary.mp4"];
    NSString *fullPath = [NSHomeDirectory() stringByAppendingPathComponent:buildPath];
[buildPath release];


    //if the file already exists, deleate it
NSFileManager *fileMgr = [NSFileManager defaultManager];
if([fileMgr fileExistsAtPath:fullPath]){
    if([fileMgr removeItemAtPath:fullPath error:&error] != YES){
        //error
    }
}

//prepare to write the movie
    AVAssetWriter *videoWriter =
    [[AVAssetWriter alloc] initWithURL
    :[NSURL fileURLWithPath:fullPath]
    fileType:AVFileTypeMPEG4
        error:&error];

   NSParameterAssert(videoWriter); 

   NSDictionary *videoSettings =
    [NSDictionary dictionaryWithObjectsAndKeys:
    AVVideoCodecH264,AVVideoCodecKey,
    [NSNumber numberWithInt:width],AVVideoWidthKey,
    [NSNumber numberWithInt:height],AVVideoHeightKey,
        nil];

   AVAssetWriterInput* writerInput =
    [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo 
    outputSettings:videoSettings]
        retain]; 

  AVAssetWriterInputPixelBufferAdaptor *adaptor =
    [AVAssetWriterInputPixelBufferAdaptor
            assetWriterInputPixelBufferAdaptorWithAssetWriterInput
    :writerInput sourcePixelBufferAttributes
        :nil];

NSParameterAssert(writerInput); 
NSParameterAssert([videoWriter canAddInput:writerInput]); 
[videoWriter addInput:writerInput]; 

//start writing
[videoWriter startWriting]; 
[videoWriter startSessionAtSourceTime:kCMTimeZero]; 

CVPixelBufferRef buffer = NULL; 
buffer = [self pixelBufferFromCGImage
    :[[imageArray objectAtIndex:0] CGImage]
    :CGSizeMake(width,height)];

CVPixelBufferPoolCreatePixelBuffer(NULL,adaptor.pixelBufferPool,&buffer);
[adaptor appendPixelBuffer:buffer withPresentationTime:kCMTimeZero];

//loop through image array
int y = [imageArray count];
int x = 0;
while(x < y)
{
    if(writerInput.readyForMoreMediaData == YES){
        CMTime frameTime = CMTimeMake(1,24);
        CMTime lastTime = CMTimeMake(x,24);
        CMTime presentTime = CMTimeAdd(lastTime,frameTime);

        buffer = [self pixelBufferFromCGImage
            :[[imageArray objectAtIndex:x] CGImage]
            :CGSizeMake(width,height)];

        [adaptor appendPixelBuffer:buffer
            withPresentationTime:presentTime];
        x++;
    }
}

//finish writing 
[writerInput markAsFinished]; 
[videoWriter finishWriting];  

//clean up
CVPixelBufferPoolRelease(adaptor.pixelBufferPool); 
[videoWriter release]; 
[writerInput release];

//handle after save, save is asynchronous
void(^completionBlock)(NSURL *, NSError *) =
    ^(NSURL *assetURL, NSError *error)
{
    if(error != nil){
        //error
    }
    //remove temp movie file
    if([fileMgr removeItemAtPath:fullPath error:&error] != YES){
        //error
    }
};

//write the movie to photo album
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
NSURL *filePathURL = [NSURL fileURLWithPath:fullPath isDirectory:NO];
if([library videoAtPathIsCompatibleWithSavedPhotosAlbum:filePathURL]){
    [library 
        writeVideoAtPathToSavedPhotosAlbum:filePathURL 
        completionBlock:completionBlock];
}
//clean up
[library release];

最佳答案

你的第一个 PTS 应该是 0/24s 而不是 1/24s

糟糕,抱歉我的错误,您的第一个 PTS 零 - 我没有注意到 CVPixelBufferPoolCreatePixelBufferappendPixelBuffer:withPresentationTime:,所以我已经更改了我的答案。

您附加的第一个像素缓冲区与您的图像数组无关。它是未定义的吗?我猜是绿色的我不确定你在用像素缓冲池做什么 - 删除这两行并将你的循环重新设置为零应该可以摆脱绿色框架。

关于ios - AVAssetWriter 为影片输出添加绿框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8775285/

相关文章:

ios - 如何知道推送通知发送状态

video - FFMPEG 在使用 h264 编码器进行转码时会从原始视频中丢弃据称损坏的帧

python-2.7 - 如何使用 python 将 .264 文件转换为 avi\wav

iOS:在 NSUserDefaults 中保存 NSMutableArray 给我一个错误: 'Attempt to insert non-property list object'?

ios - Swift - 从外部设置 UI 元素

Android 应用程序演示记录器应用程序?

C# 客户端-服务器视频流

Ubuntu ffmpeg 无法使用 libx264

ios - 有没有适合iOS开发的开源计算器键盘?

python - 使用 ffmpeg 剪切视频的多个部分