我试图从一组图像制作一个视频,然后我找到了一些代码并让它工作
但视频不会保存到照片库,而是会出现此错误:
Documentsa.mov无法保存到已保存的相册:Error Domain = NSOSStatusErrorDomain Code = 2“无法播放此电影。” UserInfo = 0x922cf60 {NSLocalizedDescription =无法播放此电影。}
这是我使用的代码:
NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
UIImage * i = [UIImage imageNamed:@"IMG_1650.JPG"];
CGAffineTransform transform = CGAffineTransformMakeRotation((M_PI/180)*90);
GPUImageTransformFilter * filter = [[GPUImageTransformFilter alloc]init];
[filter setAffineTransform:transform];
UIImage * im = [filter imageByFilteringImage:i];
im = [filter imageByFilteringImage:im];
[self writeImagesToMovieAtPath:[NSString stringWithFormat:@"%@/%@",documentsDirectoryPath,@"a.mov"] withSize:CGSizeMake(i.size.width, i.size.height)];
NSString* exportVideoPath = [NSString stringWithFormat:@"%@%@",documentsDirectoryPath,@"a.mov"];
UISaveVideoAtPathToSavedPhotosAlbum (exportVideoPath,self, @selector(video:didFinishSavingWithError: contextInfo:), nil);
这是我用来制作视频的代码:
-(void) writeImagesToMovieAtPath:(NSString *) path withSize:(CGSize) size
{
NSLog(@"Write Started");
NSError *error = nil;
AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
[NSURL fileURLWithPath:path] fileType:AVFileTypeQuickTimeMovie
error:&error];
NSParameterAssert(videoWriter);
NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey,
[NSNumber numberWithInt:size.width], AVVideoWidthKey,
[NSNumber numberWithInt:size.height], AVVideoHeightKey,
nil];
AVAssetWriterInput* videoWriterInput = [[AVAssetWriterInput
assetWriterInputWithMediaType:AVMediaTypeVideo
outputSettings:videoSettings] retain];
AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor
assetWriterInputPixelBufferAdaptorWithAssetWriterInput:videoWriterInput
sourcePixelBufferAttributes:nil];
NSParameterAssert(videoWriterInput);
NSParameterAssert([videoWriter canAddInput:videoWriterInput]);
videoWriterInput.expectsMediaDataInRealTime = YES;
[videoWriter addInput:videoWriterInput];
//Start a session:
[videoWriter startWriting];
[videoWriter startSessionAtSourceTime:kCMTimeZero];
CVPixelBufferRef buffer = NULL;
//convert uiimage to CGImage.
int frameCount = 1;
int kRecordingFPS = 30;
UIImage * im = [UIImage imageNamed:@"IMG_1650.JPG"];
CGAffineTransform transform = CGAffineTransformMakeRotation((M_PI/180)*90);
GPUImageTransformFilter * filter = [[GPUImageTransformFilter alloc]init];
[filter setAffineTransform:transform];
UIImage * i = [filter imageByFilteringImage:im];
i = [filter imageByFilteringImage:im];
NSArray * imageArray = [[NSArray alloc]initWithObjects:i,i,i,i,i,i,i,i,i,i,i,i,i,i,i,i, nil];
for(UIImage * img in imageArray)
{
buffer = [self pixelBufferFromCGImage:[img CGImage] andSize:size];
BOOL append_ok = NO;
int j = 0;
while (!append_ok && j < 30)
{
if (adaptor.assetWriterInput.readyForMoreMediaData)
{
printf("appending %d attemp %d\n", frameCount, j);
CMTime frameTime = CMTimeMake(frameCount,(int32_t) kRecordingFPS);
append_ok = [adaptor appendPixelBuffer:buffer withPresentationTime:frameTime];
if(buffer)
CVBufferRelease(buffer);
[NSThread sleepForTimeInterval:0.05];
}
else
{
printf("adaptor not ready %d, %d\n", frameCount, j);
[NSThread sleepForTimeInterval:0.1];
}
j++;
}
if (!append_ok) {
printf("error appending image %d times %d\n", frameCount, j);
}
frameCount++;
}
//Finish the session:
[videoWriterInput markAsFinished];
[videoWriter finishWriting];
NSLog(@"Write Ended");
}
我尝试使用
检查视频是否兼容UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(videoPath)
我发现该视频不兼容
但我不知道为什么或如何解决这个问题
答案 0 :(得分:1)
videoPath
NSString* exportVideoPath = [NSString stringWithFormat:@"%@%@",documentsDirectoryPath,@"a.mov"];
错了。你缺少'/'分隔符。
将其更改为此
NSString* exportVideoPath = [NSString stringWithFormat:@"%@/%@",documentsDirectoryPath,@"a.mov"];
希望这可以解决你的问题。
答案 1 :(得分:0)
我在之前的评论中尝试了A-Live的解决方案
问题原来是图像太大了
有没有办法克服这个问题?
答案 2 :(得分:0)
克服你的意思是什么?我相信您可以将文件从文档发送到任何后端服务器以存储它和其他设备更好(您知道我的意思)视频支持将能够播放它。但是,即使可能通过某些“黑客”,超出操作系统限制也不太可能。
我的意思是1080p是图书馆中最高的视频文件分辨率,因为库的媒体必须可播放。您仍然可以将文件存储在应用程序文档中并手动解码,听起来像一个有趣的任务:)确保您不会将其与iCloud同步,以避免巨大的流量泄漏使客户发疯。