2016-07-09 65 views
0

當我從圖庫中選擇一個視頻時,我將選定的文件擴展名爲.MOV,因此想將其轉換爲.MP4​​研究但在這種情況下沒有得到如何做到這一點。如果你有任何想法,請幫助。刪除文件路徑擴展名不工作,當我們必須將視頻格式從.MOV轉換爲.MP4​​

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

     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"]; 

      } 
     }]; 
    }]; 
} 

else 

{ 
    NSURL *videoURL=info[UIImagePickerControllerReferenceURL]; 
    NSLog(@" valid is%@",[info objectForKey:UIImagePickerControllerReferenceURL]); 
    //else this is valid : [info objectForKey:UIImagePickerControllerReferenceURL]]; 
    PHFetchResult<PHAsset*> *assets = [PHAsset fetchAssetsWithALAssetURLs:@[videoURL] 
                    options:nil]; 


    PHAsset *asset1 = assets.firstObject; 


    AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:videoURL options:nil]; 
    NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset]; 

    // Check if video is supported for conversion or not 
    if ([compatiblePresets containsObject:AVAssetExportPresetLowQuality]) 
    { 
     //Create Export session 
     AVAssetExportSession *exportSession = [[AVAssetExportSession  alloc]initWithAsset:avAsset presetName:AVAssetExportPresetLowQuality]; 

     //Creating temp path to save the converted video 
     NSString* documentsDirectory=  [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
     NSString* myDocumentPath= [documentsDirectory stringByAppendingPathComponent:@"temp.mp4"]; 
     NSURL *url = [[NSURL alloc] initFileURLWithPath:myDocumentPath]; 

     NSData* data = [myDocumentPath dataUsingEncoding:NSUTF8StringEncoding]; 

     //Check if the file already exists then remove the previous file 
     if ([[NSFileManager defaultManager]fileExistsAtPath:myDocumentPath]) 
     { 
      [[NSFileManager defaultManager]removeItemAtPath:myDocumentPath error:nil]; 
     } 
     exportSession.outputURL = url; 
     //set the output file format if you want to make it in other file format (ex .3gp) 
     exportSession.outputFileType = AVFileTypeMPEG4; 
     exportSession.shouldOptimizeForNetworkUse = YES; 

     [exportSession exportAsynchronouslyWithCompletionHandler:^{ 
      switch ([exportSession status]) 
      { 
       case AVAssetExportSessionStatusFailed: 
        NSLog(@"Export session failed"); 
        break; 
       case AVAssetExportSessionStatusCancelled: 
        NSLog(@"Export canceled"); 
        break; 
       case AVAssetExportSessionStatusCompleted: 
       { 
        //Video conversion finished 
        NSLog(@"Successful!"); 
       } 
        break; 
       default: 
        break; 
      } 
     }]; 
    } 
    else 
    { 
     NSLog(@"Video file not supported!"); 
    } 

    [[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 phassetis %f",[size floatValue]/(1024.0*1024.0)); //size is 43.703005 
      NSData *data = [NSData dataWithContentsOfURL:urlAsset.URL]; 
      NSLog(@"length %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"]; 

     } 
    }]; 

} 

[picker dismissViewControllerAnimated:YES completion:NULL]; 
_importView.hidden=YES; 

} 

我已經刪除的文件擴展名from.MOV到.MP4試圖通過以下方式上載,但din't worked.So我認爲需要將其轉換在乞討只有當從畫廊或選擇的camera.But得不到怎麼辦that.Any幫助表示讚賞

-(void)afnetworking{ 

NSURL *videoURL = [NSURL fileURLWithPath:myDocumentPath]; 
NSData *data = [NSData dataWithContentsOfURL:videoURL]; 
if(data) 

{ 

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

} 

回答

0

1)你所選擇照相館視頻,你已經做的第一。

2)之後,你必須導出選定的視頻文件目錄。

3)然後,您必須在上傳到服務器時從文檔目錄路徑中選擇視頻。

// Create the asset url with the video file 
     AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:videoURL options:nil]; 
     NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset]; 

     // Check if video is supported for conversion or not 
     if ([compatiblePresets containsObject:AVAssetExportPresetLowQuality]) 
     { 
     //Create Export session 
      AVAssetExportSession *exportSession = [[AVAssetExportSession  alloc]initWithAsset:avAsset presetName:AVAssetExportPresetLowQuality]; 

     //Creating temp path to save the converted video 
      NSString* documentsDirectory=  [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
      NSString* myDocumentPath= [documentsDirectory stringByAppendingPathComponent:@"temp.mp4"]; 
      NSURL *url = [[NSURL alloc] initFileURLWithPath:myDocumentPath]; 

     //Check if the file already exists then remove the previous file 
      if ([[NSFileManager defaultManager]fileExistsAtPath:myDocumentPath]) 
      { 
        [[NSFileManager defaultManager]removeItemAtPath:myDocumentPath error:nil]; 
      } 
      exportSession.outputURL = url; 
      //set the output file format if you want to make it in other file format (ex .3gp) 
      exportSession.outputFileType = AVFileTypeMPEG4; 
      exportSession.shouldOptimizeForNetworkUse = YES; 

      [exportSession exportAsynchronouslyWithCompletionHandler:^{ 
      switch ([exportSession status]) 
      { 
        case AVAssetExportSessionStatusFailed: 
         NSLog(@"Export session failed"); 
         break; 
        case AVAssetExportSessionStatusCancelled: 
         NSLog(@"Export canceled"); 
         break; 
        case AVAssetExportSessionStatusCompleted: 
        { 
         //Video conversion finished 
         NSLog(@"Successful!"); 
        } 
         break; 
        default: 
         break; 
       } 
      }]; 
     } 
     else 
     { 
       NSLog(@"Video file not supported!"); 
     } 

編輯

-(void)afnetworking{ 
NSUrl *videoURL = [NSURL fileURLWithPath:myDocumentPath]; 
NSData *data = [NSData dataWithContentsOfURL:videoURL]; 
    if(data) 
{ 
    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]]; 

} 
+0

我們在做什麼水道線的NSString * myDocumentPath = [documentsDirectory stringByAppendingPathComponent:@ 「temp.mp4的」];我們不想添加temp.mp4,我們只需要filename.mp4,你可以向我解釋它。 – vicky

+0

@vicky該行是輸出視頻路徑,你可以給你任何你想要的名字。我們將視頻存儲在Document目錄中。 –

+0

意味着它只是用於存儲目的的原始文件名(我們從圖庫中拾取的文件)不會改變?對。 @Bhadresh Mulsaniya – vicky

相關問題