美文网首页
多张图片生成视频

多张图片生成视频

作者: UILabelkell | 来源:发表于2023-01-03 14:15 被阅读0次
/// 视频生成
-(void)videoGenerated
{
    self.theVideoPath = [self videoSavingPath:self.videoName];
    //定义视频的大小320 480 倍数
    
    CGSize size = CGSizeMake(320,480); // 192 256
//    CGSize size = CGSizeMake(320,480); // 192 256
    NSError *error = nil;
    //    转成UTF-8编码
    unlink([self.theVideoPath UTF8String]);
    NSLog(@"path->%@",self.theVideoPath);
    AVAssetWriter *videoWriter = [[AVAssetWriter alloc]initWithURL:[NSURL fileURLWithPath:self.theVideoPath]fileType:AVFileTypeQuickTimeMovie error:&error];
    NSParameterAssert(videoWriter);
    
    if(error) {
        NSLog(@"error =%@",[error localizedDescription]);
        return;
    }

   
    
    //mp4的格式设置 编码格式 宽度 高度
    NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:AVVideoCodecH264,AVVideoCodecKey,
                                     
                                     [NSNumber numberWithInt:size.width],AVVideoWidthKey,
                                     
                                     [NSNumber numberWithInt:size.height],AVVideoHeightKey,nil];
    
    AVAssetWriterInput *writerInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings];
    
    NSDictionary *sourcePixelBufferAttributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:kCVPixelFormatType_32ARGB],kCVPixelBufferPixelFormatTypeKey,nil];
    //    AVAssetWriterInputPixelBufferAdaptor提供CVPixelBufferPool实例,
    //    可以使用分配像素缓冲区写入输出文件。使用提供的像素为缓冲池分配通常
    //    是更有效的比添加像素缓冲区分配使用一个单独的池
    AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput sourcePixelBufferAttributes:sourcePixelBufferAttributesDictionary];
    
    NSParameterAssert(writerInput);
    
    NSParameterAssert([videoWriter canAddInput:writerInput]);
    
    if([videoWriter canAddInput:writerInput]){
        
        NSLog(@"11111");
        
    }else{
        
        NSLog(@"22222");
        
    }

    [videoWriter addInput:writerInput];
    
    [videoWriter startWriting];
    [videoWriter startSessionAtSourceTime:kCMTimeZero];
    //合成多张图片为一个视频文件
    
    dispatch_queue_t dispatchQueue = dispatch_queue_create("mediaInputQueue",NULL);
    
    int __block frame = 0;
    __weak typeof(self)weakSelf = self;
    [writerInput requestMediaDataWhenReadyOnQueue:dispatchQueue usingBlock:^{
        
        while([writerInput isReadyForMoreMediaData]) {
            
            if(++frame >= [self->_imageArray count] * 2) {
                [writerInput markAsFinished];
                
                [videoWriter finishWritingWithCompletionHandler:^{
                    NSLog(@"完成");
                    
                    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                        NSLog(@"视频合成完毕");
                        [HTipManage deallocView];
                        if (weakSelf.imageArray.count > 0 && weakSelf.imageArray!=nil) {
                            [[NSNotificationCenter defaultCenter]postNotificationName:@"SavedSuccessfuly" object:[self.imageArray objectAtIndex:0]];
                        }
                        if (weakSelf.videoUrl) {
                            weakSelf.videoUrl(weakSelf.theVideoPath);
                        }
                    }];

                }];
                break;
            }
            
            CVPixelBufferRef buffer = NULL;
            
            int idx = frame / 2;
            
            NSLog(@"idx==%d",idx);
            
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{

            }];

            
            buffer = (CVPixelBufferRef)[self pixelBufferFromCGImage:[[weakSelf.imageArray objectAtIndex:idx]CGImage]size:size];
            
            if(buffer){
                //设置每秒钟播放图片的个数
                if(![adaptor appendPixelBuffer:buffer withPresentationTime:CMTimeMake(frame,25)]) {
                    
                    NSLog(@"FAIL");
                    
                } else {
                    
                    NSLog(@"OK");
                }
                
                CFRelease(buffer);
            }
        }
    }];
}

相关文章

网友评论

      本文标题:多张图片生成视频

      本文链接:https://www.haomeiwen.com/subject/rnrlcdtx.html