移除目标路径下的视频
NSString *tempPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/BoomMovie.mp4"];
[[NSFileManager defaultManager] removeItemAtPath:tempPath error:NULL];
[self imagesToVideo:self.allImgsArr];
-(void) imagesToVideo:(NSMutableArray*) images
{
//视频的尺寸
int width = widthq;
int heigth = heigthq;
NSError *error = nil;
NSString *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/BoomMovie.mp4"];
AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
[NSURL fileURLWithPath:imagePath] fileType:AVFileTypeQuickTimeMovie
error:&error];
NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecJPEG, AVVideoCodecKey,
[NSNumber numberWithInt:width], AVVideoWidthKey,
[NSNumber numberWithInt:heigth], AVVideoHeightKey,
nil];
AVAssetWriterInput* writerInput = [AVAssetWriterInput
assetWriterInputWithMediaType:AVMediaTypeVideo
outputSettings:videoSettings];
NSParameterAssert(writerInput);
NSParameterAssert([videoWriter canAddInput:writerInput]);
[videoWriter addInput:writerInput];
//属性设置
NSMutableDictionary *dapattributes = [[NSMutableDictionary alloc] init];
[dapattributes setObject:[NSNumber numberWithUnsignedInt:kCVPixelFormatType_32ARGB] forKey:(NSString*)kCVPixelBufferPixelFormatTypeKey];
[dapattributes setObject:[NSNumber numberWithUnsignedInt:width] forKey:(NSString*)kCVPixelBufferWidthKey];
[dapattributes setObject:[NSNumber numberWithUnsignedInt:heigth] forKey:(NSString*)kCVPixelBufferHeightKey];
AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput sourcePixelBufferAttributes:dapattributes];
writerInput.expectsMediaDataInRealTime = YES;
if (![videoWriter startWriting]) {
// Error!
return;
}
// 开始生成
[videoWriter startSessionAtSourceTime:kCMTimeZero];
CVPixelBufferRef buffer = NULL;
int frameCount = 0;
int aaaa = images.count/videoTime/2;
int32_t fps = (int32_t)(24);
// 每一帧的时间
float durationForEachImage = 1.0 / fps;
for (UIImage *image in images) {
@autoreleasepool {
// if (!adaptor.assetWriterInput.readyForMoreMediaData) {
// break;
// }
// 视频的时间
CMTime frameTime = CMTimeMake(frameCount, fps);
// 合成流
buffer = [self pixelBufferFromCGImage:image.CGImage];
BOOL aaaaa = [adaptor appendPixelBuffer:buffer withPresentationTime:frameTime];
NSLog(@"aaaaadaptor:%d",aaaaa);
if (!aaaaa) {
// Error!
}
if (buffer) {
// CVBufferRelease(buffer);
CFRelease(buffer);
}
frameCount++;
}
}
[writerInput markAsFinished];
[videoWriter endSessionAtSourceTime:CMTimeMake((int64_t)(frameCount - 1) * fps * durationForEachImage, fps)];
[videoWriter finishWritingWithCompletionHandler:^{
// Finish!
// UISaveVideoAtPathToSavedPhotosAlbum(imagePath, nil, nil, nil);
NSLog(@"done!!");
if ([self.delegate respondsToSelector:@selector(backBoomVideoPath:)]) {
[self.delegate backBoomVideoPath:imagePath];
}
}];
// 注意释放
CVPixelBufferPoolRelease(adaptor.pixelBufferPool);
}
- (CVPixelBufferRef)pixelBufferFromCGImage:(CGImageRef)image
{
int width = widthq;
int height = heigthq;
NSDictionary *options = @{ (NSString *)kCVPixelBufferCGImageCompatibilityKey: @YES,
(NSString *)kCVPixelBufferCGBitmapContextCompatibilityKey: @YES, };
CVPixelBufferRef pxbuffer = NULL;
CVPixelBufferCreate(kCFAllocatorDefault,
width,
height,
kCVPixelFormatType_32ARGB,
(__bridge CFDictionaryRef)options,
&pxbuffer);
CVPixelBufferLockBaseAddress(pxbuffer, 0);
void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);
size_t bitsPerComponent = 8;
size_t bytesPerRow = 4 * width;
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pxdata,
width,
height,
bitsPerComponent,
CVPixelBufferGetBytesPerRow(pxbuffer),
rgbColorSpace,
(CGBitmapInfo)kCGImageAlphaPremultipliedFirst);
//
// size_t buffwidth = CVPixelBufferGetWidth(pxbuffer);
// size_t buffheight = CVPixelBufferGetHeight(pxbuffer);
NSLog(@"%zu",CVPixelBufferGetBytesPerRow(pxbuffer));
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
CGColorSpaceRelease(rgbColorSpace);
CGContextRelease(context);
CVPixelBufferUnlockBaseAddress(pxbuffer, 0);
return pxbuffer;
}