3

I am recording video from the iPad app and I want that video may be saved in documents folder or directly we may upload that to server. I have store audio file in documents but how to save a video file. I am using following code for recording video.

  UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  picker.delegate = self;

  if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
  {
    NSArray *mediaTypes = [NSArray arrayWithObject:(NSString*)kUTTypeMovie];
    picker.mediaTypes = mediaTypes ;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo ;

    [self presentModalViewController:picker animated:NO];

    [picker release];
}
  else
    {

UIAlertView *alt=[[UIAlertView alloc]initWithTitle:@"Error" message:@" Camera Facility is not available with this Device" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alt show];
    [alt release];
    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
Developer IOS
  • 115
  • 1
  • 2
  • 11
  • @Popeye you can see that question is how to record video but problem is not saving that is why i have asked this question so there is no need to -1 for this – Developer IOS Mar 20 '13 at 09:02
  • The answer you have accepted tells you how to save and record. You should have not asked another question that is exactly the same that is why I have given you -1. If it doesn't work than you shouldn't have accepted the answer as this will mislead other users. – Popeye Mar 20 '13 at 09:05
  • @Popeye Ok i got this – Developer IOS Mar 20 '13 at 09:10
  • To be honest as well you have a question about posting data to server as well. It isn't a duplicate but you could put two and two together to get your answer. – Popeye Mar 20 '13 at 09:16

3 Answers3

12

Try this, I've stored it with current Date-Time ::

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
    [self dismissViewControllerAnimated:NO completion:nil];
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];

    if ([type isEqualToString:(NSString *)kUTTypeVideo] || [type isEqualToString:(NSString *)kUTTypeMovie])
    {
        videoURL = [info objectForKey:UIImagePickerControllerMediaURL];

        NSLog(@"found a video");

        // Code To give Name to video and store to DocumentDirectory //

        videoData = [[NSData dataWithContentsOfURL:videoURL] retain];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];

        NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
        [dateFormat setDateFormat:@"dd-MM-yyyy||HH:mm:SS"];
        NSDate *now = [[[NSDate alloc] init] autorelease];
        theDate = [dateFormat stringFromDate:now];

        NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Default Album"];

        if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
           [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil];

        NSString *videopath= [[[NSString alloc] initWithString:[NSString stringWithFormat:@"%@/%@.mov",documentsDirectory,theDate]] autorelease];

        BOOL success = [videoData writeToFile:videopath atomically:NO];

        NSLog(@"Successs:::: %@", success ? @"YES" : @"NO");
        NSLog(@"video path --> %@",videopath);
    }
}

Video Uploading ::

videoData is getting from videoData = [[NSData dataWithContentsOfURL:videoURL] retain];

- (void)uploadVideo
{
    NSData *imageData = videoData;

    NSString *urlString=[NSString stringWithFormat:@"%s", UploadVideoService];
    NSLog(@"url=== %@", urlString);

    request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    /*  body of the post */

    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    //Video Name with Date-Time
    NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"yyyy-MM-dd-hh:mm:ssa"];
    NSString *currDate = [dateFormat stringFromDate:[NSDate date]];

    NSString *str = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"video-%@.mov\"\r\n", currDate];
    NSLog(@"String name::  %@",str);

    [dateFormat release];

    [body appendData:[[NSString stringWithString:str] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [request setHTTPBody:body];

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    NSLog(@"result from webservice:::--> %@", returnString);

    [returnString release];
}

Hope, it'll help you.

Thanks.

Manann Sseth
  • 2,745
  • 2
  • 30
  • 50
1

Once try like this,

- (void)imagePickerController:(UIImagePickerController *)imagePicker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{

    NSURL *videoUrl = (NSURL *)[info objectForKey:UIImagePickerControllerMediaURL]; 

    NSDate *now = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"hh:mm:ss";
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];

    NSData *videoData = [NSData dataWithContentsOfURL:videoUrl];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *savedvedioPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:now]]];
    savedvedioPath  = [savedvedioPath stringByAppendingFormat:@".mp4"];
    [videoData writeToFile:savedvedioPath atomically:NO];

    //here is the method to upload onto server
    [self Upload_server:savedvedioPath];

    [self dismissModalViewControllerAnimated:YES];
}    

now define your method to upload vedio like,

-(void)Upload_server:(NSString*)file_path {

  NSURL *url = [NSURL URLWithString: @"YOUR_URL_TO_UPLOAD"];
  ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
  [request setUseKeychainPersistence:YES];
  [request addFile:file_path forKey:@"YOUR_KEY"];

//insted of in above line you can also use [request setData:vedioData withFileName:@"your_file_name" andContentType:@"video/mp4" forKey:@"YOUR_KEY"] by Sending  vedioData of type NSData as another perameter to this method.

[request setDelegate:self];
[request setDidFinishSelector:@selector(uploadRequestFinished:)];
[request setDidFailSelector:@selector(uploadRequestFailed:)];
[request startAsynchronous];

}

now implement ASIFormDataRequest delegatemethods like,

- (void)uploadRequestFinished:(ASIHTTPRequest *)request{ 

   NSString *responseString = [request responseString];
  //do something after sucessful upload

}

- (void)uploadRequestFailed:(ASIHTTPRequest *)request{

  NSLog(@" Error - Statistics file upload failed: \"%@\"",[[request error] localizedDescription]); 

  }

Here i took ASIFormDataRequest to upload on to server.hope it will hepls you..

iSpark
  • 952
  • 7
  • 18
0
//for video..
#import <MobileCoreServices/MobileCoreServices.h>
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/Mediaplayer.h>

#import <CoreMedia/CoreMedia.h>



UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        NSArray *mediaTypes = [NSArray arrayWithObject:(NSString*)kUTTypeMovie];
        picker.mediaTypes = mediaTypes ;
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo ;

        [self presentModalViewController:picker animated:NO];

        [picker release];
    }
    else
    {
        UIAlertView *alt=[[UIAlertView alloc]initWithTitle:@"Error" message:@" Camera Facility is not available with this Device" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alt show];
        [alt release];
    }

for saving into Document folder & it also save in photo Library

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsDirectory = [paths objectAtIndex:0];


        //for video
        NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
        NSLog(@"video url-%@",videoURL);

        NSData *videoData = [NSData dataWithContentsOfURL:videoURL];

        NSString * videoName = [NSString stringWithFormat:@"student_%d_%d.mp4",stud_id,imgVidID];

        videoPath = [documentsDirectory stringByAppendingPathComponent:videoName];

        NSLog(@"video path-%@",videoPath);

        [videoData writeToFile:videoPath atomically:YES];

        NSString *sourcePath = [[info objectForKey:@"UIImagePickerControllerMediaURL"]relativePath];

        UISaveVideoAtPathToSavedPhotosAlbum(sourcePath,nil,nil,nil);
}
Manthan
  • 3,856
  • 1
  • 27
  • 58
  • 1
    Whilst I don't mind links as answers others don't like them. So I will give you a quick warning if it is just a link please add as comment. – Popeye Mar 20 '13 at 09:13
  • yes, you will get the answer of your problem. This is a stackOverflow's link nothing else. – Manthan Mar 20 '13 at 09:16
  • You shouldn't be placing a link as an answer they belong in comments. If you do provide a link as an answer it should really be joined by a summary of what you are pointing them too. – Popeye Mar 20 '13 at 09:19
  • Please see here http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers – Popeye Mar 20 '13 at 09:23
  • Copying someone else's work like you have done from here http://stackoverflow.com/questions/15475315/how-to-record-a-video-clip-in-ipad-app-and-store-it-in-documents-folder is also not a good idea. Your taking credit for someone else's work. – Popeye Mar 20 '13 at 09:59
  • 1
    If u see that link, You will find that Its not someone else's answer. It was mine. – Manthan Mar 20 '13 at 10:03
  • In that case you should be advising them that there question is an exact duplicate of that question. And telling them they would be able to find there answer in that link. And that is why I now go back to my original comment links should provided as comments not answers. – Popeye Mar 20 '13 at 10:10