4

i needs to create a video from the image selected.

i have code it shoudl work but its giving error while appending buffer.

This is how both type of images has been saved.

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo 
{
    //    NSLog(@"Came From Here");
    imgv.image = img;   
    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    app.imgmain = img;
    [self dismissModalViewControllerAnimated:YES];
    RecordVoice *rec = [[RecordVoice alloc] initWithNibName:@"RecordVoice" bundle:nil];
    rec.hidesBottomBarWhenPushed = YES;
    // rec.img.image = img;
    [self.navigationController pushViewController:rec animated:YES];
    //[self presentModalViewController:rec animated:YES];
    [rec release];
    //  flag =@"yes";
    // need to show the upload image button now
    //  [username, ititle resignFirstResponder];



}

on the other view controller i am showing this image on a uiimage view.

and on button click i am converting that image to video with this code.

-(void)createVideoFile
{

        NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectoryPath error:nil];
        for (NSString *tString in dirContents) {
            if ([tString isEqualToString:@"test.mp4"]) 
            {
                [[NSFileManager defaultManager]removeItemAtPath:[NSString stringWithFormat:@"%@/%@",documentsDirectoryPath,tString] error:nil];

            }
        }
    NSString *nfile = [documentsDirectoryPath stringByAppendingPathComponent:@"test.mp4"];
    AVURLAsset * urlAsset = [AVURLAsset URLAssetWithURL:recordedTmpFile options:nil];

        NSLog(@"Write Started");

        NSError *error = nil;
    CGSize size = img.image.size; //CGSizeMake(320, 480);




    NSLog(@"Write Started");


    AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
                                  [NSURL fileURLWithPath:nfile] 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 = 0;

        buffer = [self pixelBufferFromCGImage:[img.image 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 = urlAsset.duration;//CMTimeMake(frameCount,(int32_t) 10);
                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++;


        [videoWriterInput markAsFinished];  
         [videoWriter finishWriting];




    [self CompileFilesToMakeMovie];
    [altv dismissWithClickedButtonIndex:0 animated:YES];
    [altv release];



        NSLog(@"Write Ended");


}

But this is not working...

i am stuck can anyone please help me in this???? :(

Darshan
  • 121
  • 1
  • 2
  • 10
  • @MuTaTeD hye when i use code JPEG then it wont give error but it wont create a video. but when i use H264Codec it gives error. I have tried your code also but it gives error. this is almost your code only. – Darshan Oct 25 '11 at 08:54
  • have you set a mixable (non exclusive) audio session category? – Rhythmic Fistman Oct 25 '11 at 11:47
  • @Rhythmic yes adding a audio file to it. but later wards after the video is created... but i have figured out the problem... i have answered it. – Darshan Oct 26 '11 at 16:25
  • hi do you have any sample code to make video from image.... – Rajneesh071 Sep 24 '12 at 14:52
  • @Rajneesh071 Yes, the above code does the same. – Darshan Sep 26 '12 at 10:03
  • hi i want to make video from image and then merge it with sound... so the resultant video is with sound... you did this? – Rajneesh071 Sep 26 '12 at 13:01
  • @Rajneesh071 Yes, I did that. And the above code is for that only. – Darshan Sep 28 '12 at 12:25
  • hi the above code is only for video making..not for sound attachment with video..:( – Rajneesh071 Oct 11 '12 at 06:51
  • hi i am able to make video but the video in not from original image...its zig zag type video... – Rajneesh071 Nov 05 '12 at 15:04
  • @Rajneesh071 Yes, if the image size is big then it happens that way. try to reduce the image size and it will work. – Darshan Nov 07 '12 at 07:07
  • no it was the problem with .png images...when i am using jpg image then working fine – Rajneesh071 Nov 07 '12 at 09:50
  • hey @Darshan did you make video of .png type image/??? – Rajneesh071 Nov 08 '12 at 06:27
  • @Rajneesh071 well not sure.. i havent tried with .png type images.. i was trying it with taking images from iphone camera itself. so not sure. but i dont think so .png would create a issue here... – Darshan Nov 08 '12 at 10:09
  • but png creating issuse for me....and i am getting one more problem... when i am playing a video and then doing recording but recording task in not performing....i dont know why but getting [MPAVController] Autoplay: Likely to keep up or full buffer: 0 2012-11-08 15:53:07.688 Liteversion Postcard[1764:1d303] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up. – Rajneesh071 Nov 08 '12 at 10:23
  • @Darshan Can u clearly tell me , how video file is to get from a single image as a constant image through out the video with audiofile mixing.Length of video should be equal to length of audio file. – Himanshu May 14 '13 at 04:48
  • @RajkumarThyadi my code that i have written does the same. you just have to make sure that whatever image file you are giving its of lower size or you have compressed it. – Darshan May 14 '13 at 08:56
  • @Darshan Thanq for responding.I tried as like u bt Im getting that when video is playing,if user forward it ..background image is missing. – Himanshu May 14 '13 at 09:40
  • @Darshan I have used same code and created video and it does works fine, but for 20+ images application is crashes in device, I looked in the simulator and found that for more images it takes 1gb + memory, so is there anything wrong with this code. Can you please let me know what could be issue... Thank you – Satish Nov 20 '14 at 09:50
  • @Satish you need to compress the image before you make a video out of it, as your size is going big the app crashes. – Darshan Dec 05 '14 at 05:50
  • @Darshan yes i did compress it but what if there is 1000 images or more.. is there any other way to do it in such a way that it works for unlimited number of images... – Satish Dec 05 '14 at 06:37

1 Answers1

0

i have figured out the problem.

if we use image with big size it wont work. like the pictures taken from camera app has a big size.

so i am compressing it to low level and then it works..

i didnt yet got why its working like but got the solution

Mat
  • 7,613
  • 4
  • 40
  • 56
Darshan
  • 121
  • 1
  • 2
  • 10