2

As i select a video from Gallery i am getting selected file extension as .MOV so wanted to convert it to .MP4 researched but not getting how to do that in this case. If u have any idea please help.

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

    selectedVideoURL = info[UIImagePickerControllerMediaURL];
if( [picker sourceType] == UIImagePickerControllerSourceTypeCamera )
{
    NSURL *videoURL=info[UIImagePickerControllerMediaURL];
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc]init];
    [library writeVideoAtPathToSavedPhotosAlbum:videoURL completionBlock:^(NSURL *assetURL, NSError *error )
     {
         //here is your URL : assetURL
         NSLog(@"url is%@",assetURL);

     // here in debugger it shows as  assets.MOV  i want .MP4 

         PHFetchResult<PHAsset*> *assets = [PHAsset fetchAssetsWithALAssetURLs:@[assetURL]
                                                                       options:nil];

         PHAsset *asset1 = assets.firstObject;
         [[PHImageManager defaultManager] requestAVAssetForVideo:asset1 options:nil resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
             if ([asset isKindOfClass:[AVURLAsset class]]) {
                 AVURLAsset* urlAsset = (AVURLAsset*)asset;
                 NSNumber *size;

                 [urlAsset.URL getResourceValue:&size forKey:NSURLFileSizeKey error:nil];
                 NSLog(@"size video of camera %f",[size floatValue]/(1024.0*1024.0)); //size is 43.703005
                 NSData *data = [NSData dataWithContentsOfURL:urlAsset.URL];
                 NSLog(@"length of camera %f",[data length]/(1024.0*1024.0)); // data size is 43.703005

                 sizeofVideo=[size floatValue]/(1024.0*1024.0);

                 NSLog(@"sizeofVideo arvin %f",sizeofVideo);
                 if (!sizeofVideo>50.00) {
                     selectedVideoURL=nil;
                     UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Alert" message:@"size of video exist 50MB" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
                     [alert show];
                     attachnoteLbl.text=[NSString stringWithFormat:@"Attach Note:0"];
                 }
                     attachnoteLbl.text=[NSString stringWithFormat:@"Attach Note:1"];

             }
         }];
     }];
}

I have tried the following way by deleting the file extension from.MOV to .MP4 but din't worked.So i think need to convert it in the begging only while selecting it from gallery or camera.

-(void)afnetworking{

     NSString *videoUrl=[selectedVideoURL path];
   if(videoUrl){
    NSString *videoName = [[videoUrl lastPathComponent]stringByDeletingPathExtension];
    NSData *videoData  = [NSData dataWithContentsOfURL:selectedVideoURL];
    [bodyData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    NSString *tmpfileinfo = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"video_clip\"; filename=\"%@.mp4\"\r\n",videoName];
    [bodyData appendData:[tmpfileinfo dataUsingEncoding:NSUTF8StringEncoding]];
    [bodyData appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [bodyData appendData:[NSData dataWithData:videoData]];

}
vicky
  • 253
  • 2
  • 14
  • http://stackoverflow.com/questions/5015050/equivalent-for-stringbydeletingpathextension-for-an-array http://stackoverflow.com/questions/1098957/objective-c-extract-filename-from-path-string – vicky Jun 21 '16 at 14:39

1 Answers1

0

check out this library. Hope this can solve your problem. https://github.com/Darktt/DTVideoConverter

Pias
  • 218
  • 1
  • 2
  • 13