如何从静止图像 (png) 渲染固定长度的视频文件
How to render a video file of fixed length from a still image (png)
我正在尝试从静止图像 (png) 创建指定长度的视频。
我的代码基于 How do I export UIImage as a movie
中给出的答案
我的代码创建了一个长度正确的视频,但图像确实出现到最后一帧。我希望在视频的整个持续时间内显示图像。 (这些视频将成为无缝时间轴中的占位符。)
我的代码是:
- (NSURL*)exportImage:(NSImage*)image asMovieLength:(double)seconds {
NSURL *outFile;
AppDelegate *theApp;
NSError *error=nil;
AVAssetWriter *videoWriter;
NSDictionary *videoSettings;
AVAssetWriterInput* writerInput;
CVPixelBufferRef buffer = NULL;
AVAssetWriterInputPixelBufferAdaptor *adaptor;
theApp = (AppDelegate*)[[NSApplication sharedApplication] delegate];
outFile = [theApp getFilenameForFillerFile];
videoWriter = [[AVAssetWriter alloc] initWithURL:outFile fileType:AVFileTypeMPEG4 error:&error];
NSParameterAssert(videoWriter);
videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:AVVideoCodecH264, AVVideoCodecKey,
[NSNumber numberWithInt:880], AVVideoWidthKey,
[NSNumber numberWithInt:880], AVVideoHeightKey,
nil];
writerInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings];
adaptor = [AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput
sourcePixelBufferAttributes:nil];
NSParameterAssert(writerInput);
NSParameterAssert([videoWriter canAddInput:writerInput]);
[videoWriter addInput:writerInput];
// Start the writing session
[videoWriter startWriting];
[videoWriter startSessionAtSourceTime:kCMTimeZero];
if(writerInput.readyForMoreMediaData)
{
CMTime zeroTime = CMTimeMake(0, 600);
CMTime frameTime = CMTimeMake(seconds*600, 600);
buffer = [self pixelBufferFromImage:image];
[adaptor appendPixelBuffer:buffer withPresentationTime:frameTime];
[writerInput markAsFinished];
[videoWriter finishWritingWithCompletionHandler:^{
NSLog(@"Finished writing...checking completion status...");
if (videoWriter.status != AVAssetWriterStatusFailed && videoWriter.status == AVAssetWriterStatusCompleted)
{
NSLog(@"Video writing succeeded.");
} else
{
NSLog(@"Video writing failed: %@", videoWriter.error);
}
}];
CVPixelBufferPoolRelease(adaptor.pixelBufferPool);
NSLog (@"Done");
}
return outFile;
}
如何让图像从头开始显示并持续指定的时间?
我对代码进行了以下更改。
CMTime zeroTime = CMTimeMake(0, 600);
CMTime frameTime = CMTimeMake((seconds * 0.5) * 600, 600);
buffer = [self pixelBufferFromImage:image];
[adaptor appendPixelBuffer:buffer withPresentationTime:zeroTime];
[adaptor appendPixelBuffer:buffer withPresentationTime:frameTime];
我正在尝试从静止图像 (png) 创建指定长度的视频。 我的代码基于 How do I export UIImage as a movie
中给出的答案我的代码创建了一个长度正确的视频,但图像确实出现到最后一帧。我希望在视频的整个持续时间内显示图像。 (这些视频将成为无缝时间轴中的占位符。)
我的代码是:
- (NSURL*)exportImage:(NSImage*)image asMovieLength:(double)seconds {
NSURL *outFile;
AppDelegate *theApp;
NSError *error=nil;
AVAssetWriter *videoWriter;
NSDictionary *videoSettings;
AVAssetWriterInput* writerInput;
CVPixelBufferRef buffer = NULL;
AVAssetWriterInputPixelBufferAdaptor *adaptor;
theApp = (AppDelegate*)[[NSApplication sharedApplication] delegate];
outFile = [theApp getFilenameForFillerFile];
videoWriter = [[AVAssetWriter alloc] initWithURL:outFile fileType:AVFileTypeMPEG4 error:&error];
NSParameterAssert(videoWriter);
videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:AVVideoCodecH264, AVVideoCodecKey,
[NSNumber numberWithInt:880], AVVideoWidthKey,
[NSNumber numberWithInt:880], AVVideoHeightKey,
nil];
writerInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings];
adaptor = [AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput
sourcePixelBufferAttributes:nil];
NSParameterAssert(writerInput);
NSParameterAssert([videoWriter canAddInput:writerInput]);
[videoWriter addInput:writerInput];
// Start the writing session
[videoWriter startWriting];
[videoWriter startSessionAtSourceTime:kCMTimeZero];
if(writerInput.readyForMoreMediaData)
{
CMTime zeroTime = CMTimeMake(0, 600);
CMTime frameTime = CMTimeMake(seconds*600, 600);
buffer = [self pixelBufferFromImage:image];
[adaptor appendPixelBuffer:buffer withPresentationTime:frameTime];
[writerInput markAsFinished];
[videoWriter finishWritingWithCompletionHandler:^{
NSLog(@"Finished writing...checking completion status...");
if (videoWriter.status != AVAssetWriterStatusFailed && videoWriter.status == AVAssetWriterStatusCompleted)
{
NSLog(@"Video writing succeeded.");
} else
{
NSLog(@"Video writing failed: %@", videoWriter.error);
}
}];
CVPixelBufferPoolRelease(adaptor.pixelBufferPool);
NSLog (@"Done");
}
return outFile;
}
如何让图像从头开始显示并持续指定的时间?
我对代码进行了以下更改。
CMTime zeroTime = CMTimeMake(0, 600);
CMTime frameTime = CMTimeMake((seconds * 0.5) * 600, 600);
buffer = [self pixelBufferFromImage:image];
[adaptor appendPixelBuffer:buffer withPresentationTime:zeroTime];
[adaptor appendPixelBuffer:buffer withPresentationTime:frameTime];