gpt4 book ai didi

ios AVAssetWriter 输出无效的视频格式

转载 作者:行者123 更新时间:2023-11-29 13:11:32 29 4
gpt4 key购买 nike

此代码适用于从图像创建视频。 AVAssetWritter 工作正常,没有错误并导出输出视频 - (.mov)。但在某些图像大小的情况下它会失败。120 x 160 - 失败180 x 640、240 x 320、160 x 213.333、96 x 128 - 通过

P/s: 在 iPod 4th ios 5.1 上测试

This is screenshot http://postimg.org/image/gih1ofvbn/尺寸为 120 x 160 的失败视频,请帮我解决这个问题。或者任何对其他视频编码器库的推荐都将受到欢迎。谢谢

这是我的代码

- (void) writeImagesAsMovie:(NSArray *)images withDelaySec:(float)delaySec toPath:(NSString*)path   {
//delete file
NSFileManager *fileMgr = [[NSFileManager alloc] init];
NSError *error2;
if ([fileMgr removeItemAtPath:path error:&error2] != YES)
NSLog(@"Unable to delete file: %@", [error2 localizedDescription]);


UIImage *first = [images objectAtIndex:0];


CGSize frameSize = first.size;
NSLog(@"__create video: %f %f", frameSize.width, frameSize.height);

NSError *error = nil;
AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
[NSURL fileURLWithPath:path] fileType:AVFileTypeQuickTimeMovie
error:&error];

if(error) {
NSLog(@"error creating AssetWriter: %@",[error description]);
}

int numPixels = frameSize.width * frameSize.height;
int bitsPerSecond;
float bitsPerPixel;
// Assume that lower-than-SD resolutions are intended for streaming, and use a lower bitrate
if ( numPixels < (640 * 480) )
bitsPerPixel = 4.05; // This bitrate matches the quality produced by AVCaptureSessionPresetMedium or Low.
else
bitsPerPixel = 11.4; // This bitrate matches the quality produced by AVCaptureSessionPresetHigh.

bitsPerSecond = numPixels * bitsPerPixel;

NSDictionary *videoCompressionSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey,
[NSNumber numberWithInteger:frameSize.width], AVVideoWidthKey,
[NSNumber numberWithInteger:frameSize.height], AVVideoHeightKey,
[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInteger:bitsPerSecond], AVVideoAverageBitRateKey,
[NSNumber numberWithInteger:30], AVVideoMaxKeyFrameIntervalKey,
nil], AVVideoCompressionPropertiesKey,
nil];
AVAssetWriterInput *writerInput = [[AVAssetWriterInput alloc] initWithMediaType:AVMediaTypeVideo outputSettings:videoCompressionSettings];

writerInput.expectsMediaDataInRealTime = YES;

NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
[attributes setObject:[NSNumber numberWithUnsignedInt:kCVPixelFormatType_32ARGB] forKey:(NSString*)kCVPixelBufferPixelFormatTypeKey];
[attributes setObject:[NSNumber numberWithUnsignedInt:frameSize.width] forKey:(NSString*)kCVPixelBufferWidthKey];
[attributes setObject:[NSNumber numberWithUnsignedInt:frameSize.height] forKey:(NSString*)kCVPixelBufferHeightKey];


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

[videoWriter addInput:writerInput];

// fixes all errors
writerInput.expectsMediaDataInRealTime = YES;

//Start a session:
[videoWriter startWriting];

[videoWriter startSessionAtSourceTime:kCMTimeZero];

CVPixelBufferRef buffer = NULL;
buffer = [self pixelBufferFromCGImage:[first CGImage]];
BOOL result = [adaptor appendPixelBuffer:buffer withPresentationTime:kCMTimeZero];

if (result == NO) //failes on 3GS, but works on iphone 4
NSLog(@"failed to append buffer");

if(buffer) {
CVBufferRelease(buffer);
}


float delayMili = delaySec * 25;
int fps = 30;


int i = 0;
for (UIImage *imgFrame in images)
{
if (adaptor.assetWriterInput.readyForMoreMediaData) {

i++;
CMTime frameTime = CMTimeMake(delayMili, fps);
CMTime lastTime = CMTimeMake(delayMili * i, fps);
CMTime presentTime = CMTimeAdd(lastTime, frameTime);

NSLog(@"__presentTime: %f", CMTimeGetSeconds(presentTime));

buffer = [self pixelBufferFromCGImage:[imgFrame CGImage]];
BOOL result = [adaptor appendPixelBuffer:buffer withPresentationTime:presentTime];

if (result == NO) //failes on 3GS, but works on iphone 4
{
NSLog(@"failed to append buffer");
NSLog(@"The error is %@", [videoWriter error]);
}

if(buffer) {
CVBufferRelease(buffer);
}

} else {
NSLog(@"error");
i--;
}

}

//Finish the session:
[writerInput markAsFinished];
[videoWriter finishWriting];
CVPixelBufferPoolRelease(adaptor.pixelBufferPool);
[videoWriter release];
[writerInput release];

NSLog(@"Movie created successfully");

最佳答案

视频的宽度应能被 16 整除。

关于ios AVAssetWriter 输出无效的视频格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17247284/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com