2014-12-26 3 views
1

UIImagePickerController를 사용하여 비디오를 녹화하고 있는데 문제는 안드로이드 호환성을위한 mov 형식의 비디오를 녹화하는 것입니다.Mp4 형식의 비디오 캡처

다음 코드를 사용하여 비디오를 mp4 형식으로 변환해야하는데 문제는 약 30 초에서 35 초 정도 소요되는 6 초의 비디오 시간입니다. 직접 mp4 형식 또는 더 빠른 방법으로 비디오를 녹화 할 수있는 솔루션은 큰 도움이 될 것입니다. 당신이 AVURLAsset를 사용하여 라이브러리/앨범에서 비디오를로드 할 때 시간이 걸립니다

-(void)movToMp4:(NSURL *)videoURL{ // method for mov to mp4 conversion 

    AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:videoURL options:nil]; 
    NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset]; 
    if ([compatiblePresets containsObject:AVAssetExportPresetLowQuality]) 
    { 
     AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:avAsset presetName:AVAssetExportPresetPassthrough]; 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString* videoPath = [NSString stringWithFormat:@"%@/xyz.mp4", [paths objectAtIndex:0]]; 
     exportSession.outputURL = [NSURL fileURLWithPath:videoPath]; 
     exportSession.outputFileType = AVFileTypeMPEG4; 
     [exportSession exportAsynchronouslyWithCompletionHandler:^{ 
      switch ([exportSession status]) { // switch case to get completion case where i put my delegate 
        return; 
        break; 
       case AVAssetExportSessionStatusCompleted: { 
        [self.delegate mp4Response:videoPath]; 
         break; 

        } 

       } 

      } 
     }]; 
    } 

} 
+0

들여 쓰기가 쉬운 코드 들여 쓰기를 수정하고 게시물 편집 도구 모음에서 "코드"버튼을 사용하여 읽기 쉽게하십시오. – AFract

+0

코드를 포함하여 처음으로 죄송합니다. :) –

답변

2

예 있기 때문에 미리 감사드립니다.

여기에서 block을 사용하여 라이브러리에서 비디오를로드해야합니다. 그것은 메인 쓰레드에 있어야 -

또한 완료 블록

[self.delegate mp4Response:videoPath]; 

라인. 라이브러리에서 비디오를 얻을 수

UIImagePickerController 위임 방법 :

이 방법을 따르십시오.

[self performSelectorOnMainThread:@selector(doneCompressing) withObject:nil waitUntilDone:YES]; 

그것은 (전경) 메인 쓰레드의 하나 이상의 방법 doneCompressing를 호출한다 : didFinishPickingMediaWithInfo 방법에

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
    NSURL *localUrl = (NSURL *)[info valueForKey:UIImagePickerControllerMediaURL]; 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString* videoPath = [NSString stringWithFormat:@"%@/xyz.mp4", [paths objectAtIndex:0]]; 
    NSURL *outputURL = [NSURL fileURLWithPath:videoPath]; 

    [self convertVideoToLowQuailtyWithInputURL:localUrl outputURL:outputURL handler:^(AVAssetExportSession *exportSession) 
    { 
     if (exportSession.status == AVAssetExportSessionStatusCompleted) { 
      NSLog(@"Capture video complete"); 
      [self performSelectorOnMainThread:@selector(doneCompressing) withObject:nil waitUntilDone:YES]; 
     } 
    }]; 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 


- (void)convertVideoToLowQuailtyWithInputURL:(NSURL*)inputURL outputURL:(NSURL*)outputURL handler:(void (^)(AVAssetExportSession*))handler { 
    [[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil]; 
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil]; 
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetPassthrough]; 
    exportSession.outputURL = outputURL; 
    exportSession.outputFileType = AVFileTypeMPEG4; 
    [exportSession exportAsynchronouslyWithCompletionHandler:^(void) { 
     handler(exportSession); 
    }]; 
} 

이 광고를 관찰했다. 그래서 doneCompressing에 대리자 메서드를 호출 할 수 있습니다. 그리고 이것은 시간을 줄일 것입니다.

- (void) doneCompressing { 
     [self.delegate mp4Response:videoPath]; 
} 
3
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 

    NSLog(@"file info = %@", info); 
    NSString *pickedType = [info objectForKey:@"UIImagePickerControllerMediaType"]; 
    NSData *videoData; 
    if ([pickedType isEqualToString:@"public.movie"]){ 

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

    } 

    [self dismissViewControllerAnimated:YES completion:^{ 

     // 
     [self writeFileData:videoData]; 
    }]; 

} 

// to get path of document directory 
- (NSString *)applicationDocumentsDirectory 
{ 
    // return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    return documentsDirectory; 
} 

- (void) writeFileData:(NSData *)fileData{ 



    float size = fileData.length/(1024 * 1024); 

    NSString *fileName = nil; 
    NSString *strPath = nil; 

    NSString *documentsDirectory = [self applicationDocumentsDirectory]; 

    double CurrentTime = CACurrentMediaTime(); 


     fileName = [NSString stringWithFormat:@"%d.mp4",(int)CurrentTime]; 


    strPath = [documentsDirectory stringByAppendingPathComponent:fileName]; 
    NSFileManager *filemanager=[[NSFileManager alloc] init]; 
    NSError *er; 

    if (![filemanager fileExistsAtPath:documentsDirectory]) { 
     [filemanager createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:&er]; 
     NSLog(@"error in folder creation = %@", er); 

    } 

    NSLog(@"size of data = %lu", (unsigned long)[fileData length]); 
    BOOL saved = [fileData writeToFile:strPath atomically:YES]; 
    if (saved) { 
     NSURL *videoURL = [NSURL URLWithString:strPath]; 
     // now u can handle mp4 video from videoURL 
     } 
    else 
     return; 


} 
+0

OP가 원하는 이유를 설명해주세요. – Unheilig

+0

이렇게하면 비디오를 녹화하고 mp4 형식의 문서 디렉토리에 저장할 수 있습니다. –

+0

덕분에 많은 도움이되었습니다. –

관련 문제