2

I was trying to make a video from a set of images and I found some code and got it to work

but the video won't be saved to the photo library it gives me this error :

Documentsa.mov cannot be saved to the saved photos album: Error Domain=NSOSStatusErrorDomain Code=2 "This movie could not be played." UserInfo=0x922cf60 {NSLocalizedDescription=This movie could not be played.}

Here's the code I use :

 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);

And this is the code I use to create the video :

-(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");
}

I tried to check if the video is compatible using

UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(videoPath)

and I found out that the video isn't compatible

but i don't know why or how to fix this

4

3 回答 3

1

视频路径

NSString* exportVideoPath = [NSString stringWithFormat:@"%@%@",documentsDirectoryPath,@"a.mov"]; 

是错的。您缺少“/”分隔符。

改成这个

NSString* exportVideoPath = [NSString stringWithFormat:@"%@/%@",documentsDirectoryPath,@"a.mov"]; 

希望这可以解决您的问题。

于 2013-02-26T10:57:24.317 回答
0

你说的克服是什么意思?我相信您可以将文件从文档发送到任何后端服务器以存储它,并且具有更好(您知道我的意思)视频支持的其他设备将能够播放它。但是,即使可能通过一些“黑客”来超越操作系统限制也不太可能顺利。

我的意思是 1080p 是库中最大的视频文件分辨率,因为库中的媒体必须是可播放的。您仍然可以将文件存储在应用程序文档中并手动解码,这听起来像是一项有趣的任务:) 确保您不会将其与 iCloud 同步,以避免大量流量泄漏使客户发疯。

于 2012-10-23T13:38:53.427 回答
0

我在之前的评论中尝试了 A-Live 的解决方案

问题原来是图像的尺寸太大

有没有办法克服这个?

于 2012-10-23T10:51:11.457 回答