2016-07-09 5 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; 

} 

나는 그것을 업로드하는 동안 .MP4에 파일 확장자 from.MOV을 삭제하여 다음과 같은 방법을 시도했지만 갤러리 또는에서 선택 동안에 만 내가 구걸로 변환 할 필요가 있다고 생각 worked.So을 din't 한)

-(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을 평가하는 방법을 저런거 도움을 수행하는 방법 못하고 camera.But 당신이 이미하고있는 포토 갤러리에서 비디오를 선택해야합니다 우선.

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

EDIT 우리가 선있는 NSString * myDocumentPath = [documentsDirectory stringByAppendingPathComponent "temp.mp4"@] GAT 무엇을하고 있는지

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

; 우리는 temp.mp4 파일을 추가하고 싶지 않습니다. 우리가 원하는 filename.mp4는 나에게 설명 할 수 있습니다. – vicky

+0

@vicky 그 라인은 출력 비디오 경로를위한 것이므로 원하는 이름을 줄 수 있습니다. 우리는 Document 디렉토리 안에 비디오를 저장하고 있습니다. –

+0

은 원래의 파일 이름 (갤러리에서 가져온 파일)을 저장하는 것이 아니라는 것을 의미합니다. 권리. @ 바닷물 Mulsaniya – vicky