- (void) testCompressionSession
{
NSArray *imageArr = [NSArray arrayWithObjects:[[UIImage imageNamed:@"114.png"] CGImage],[[UIImage imageNamed:@"114.png"] CGImage],[[UIImage imageNamed:@"placeholderImage.png"] CGImage],[[UIImage imageNamed:@"dfd.png"] CGImage],[[UIImage imageNamed:@"viewbg.png"] CGImage], nil];
CGSize size = CGSizeMake(480, 320);
NSString *betaCompressionDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Movie.m4v"];
NSError *error = nil;
unlink([betaCompressionDirectory UTF8String]);
//----initialize compression engine
AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:[NSURL fileURLWithPath:betaCompressionDirectory]
fileType:AVFileTypeQuickTimeMovie
error:&error];
NSParameterAssert(videoWriter);
if(error)
NSLog(@"error = %@", [error localizedDescription]);
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 *adaptor = [AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput
sourcePixelBufferAttributes:sourcePixelBufferAttributesDictionary];
NSParameterAssert(writerInput);
NSParameterAssert([videoWriter canAddInput:writerInput]);
if ([videoWriter canAddInput:writerInput])
NSLog(@"I can add this input");
else
NSLog(@"i can't add this input");
[videoWriter addInput:writerInput];
[videoWriter startWriting];
[videoWriter startSessionAtSourceTime:kCMTimeZero];
//---
// insert demo debugging code to write the same image repeated as a movie
CGImageRef theImage = [[UIImage imageNamed:@"114.png"] CGImage];
dispatch_queue_t dispatchQueue = dispatch_queue_create("mediaInputQueue", NULL);
int __block frame = 0;
[writerInput requestMediaDataWhenReadyOnQueue:dispatchQueue usingBlock:^{
while ([writerInput isReadyForMoreMediaData])
{
if(++frame >= imageArr.count * 40)
{
[writerInput markAsFinished];
[videoWriter finishWriting];
break;
}
int idx = frame/40;
CVPixelBufferRef buffer = (CVPixelBufferRef)[self pixelBufferFromCGImage:(__bridge CGImageRef)([imageArr objectAtIndex:idx]) size:size];
if (buffer)
{
if(![adaptor appendPixelBuffer:buffer withPresentationTime:CMTimeMake(frame, 20)])
NSLog(@"FAIL");
else
NSLog(@"Success:%d", frame);
CFRelease(buffer);
}
}
}];
NSLog(@"outside for loop");
}
- (CVPixelBufferRef )pixelBufferFromCGImage:(CGImageRef)image size:(CGSize)size
{
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
[NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey, nil];
CVPixelBufferRef pxbuffer = NULL;
CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, size.width, size.height, kCVPixelFormatType_32ARGB, (__bridge CFDictionaryRef) options, &pxbuffer);
// CVReturn status = CVPixelBufferPoolCreatePixelBuffer(NULL, adaptor.pixelBufferPool, &pxbuffer);
NSParameterAssert(status == kCVReturnSuccess && pxbuffer != NULL);
CVPixelBufferLockBaseAddress(pxbuffer, 0);
void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);
NSParameterAssert(pxdata != NULL);
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pxdata, size.width, size.height, 8, 4*size.width, rgbColorSpace, kCGImageAlphaPremultipliedFirst);
NSParameterAssert(context);
CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image)), image);
CGColorSpaceRelease(rgbColorSpace);
CGContextRelease(context);
CVPixelBufferUnlockBaseAddress(pxbuffer, 0);
return pxbuffer;
}
圖片轉(zhuǎn)視頻
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來温艇,“玉大人因悲,你說我怎么就攤上這事∩装” “怎么了晃琳?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長琐鲁。 經(jīng)常有香客問我卫旱,道長,這世上最難降的妖魔是什么围段? 我笑而不...
- 正文 為了忘掉前任顾翼,我火速辦了婚禮,結(jié)果婚禮上奈泪,老公的妹妹穿的比我還像新娘适贸。我一直安慰自己灸芳,他們只是感情好,可當我...
- 文/花漫 我一把揭開白布拜姿。 她就那樣靜靜地躺著烙样,像睡著了一般。 火紅的嫁衣襯著肌膚如雪蕊肥。 梳的紋絲不亂的頭發(fā)上谒获,一...
- 文/蒼蘭香墨 我猛地睜開眼琅锻,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了向胡?” 一聲冷哼從身側(cè)響起恼蓬,我...
- 正文 年R本政府宣布曾掂,位于F島的核電站惫谤,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏珠洗。R本人自食惡果不足惜溜歪,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望许蓖。 院中可真熱鬧蝴猪,春花似錦调衰、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至动壤,卻和暖如春萝喘,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背琼懊。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- JavaCV JavaCpp 如果下載不了,可以下載此處 1. 使用 注意:存儲8張源圖的路徑是"/sdcard/...
- 將圖片按照一定命名方式存放在一個文件夾下车胡,例如zhn_0,zhn_1...檬输,然后 ffmpeg -r 5 -i z...
- 發(fā)現(xiàn)一個在線視頻工具,可以快速的將新的文字/文章/圖片快速轉(zhuǎn)換為視頻匈棘。 https://lumen5.com 快到...