#import <MediaPlayer/MediaPlayer.h>

#import <MobileCoreServices/UTCoreTypes.h>

#import <AVFoundation/AVFoundation.h>


=============== header ==========

-(void)camaraTest

{

    if ([UIImagePickerController isSourceTypeAvailable:

         UIImagePickerControllerSourceTypeCamera] == YES){

        // Create image picker controller

        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

        imagePicker.mediaTypes =

        [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];

        // Set source to the camera

        imagePicker.sourceTypeUIImagePickerControllerSourceTypePhotoLibrary;

       // imagePicker.sourceType =  UIImagePickerControllerSourceTypeSavedPhotosAlbum;

        imagePicker.allowsEditing=TRUE;

        // Delegate is self

        

        imagePicker.delegate = self;

        //imageType=1;

        [self presentViewController:imagePicker animated:YES completion:nil];

    }

}

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

{

 

    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];

    //NSLog(@"type=%@",type);

    if ([type isEqualToString:(NSString *)kUTTypeVideo] ||

        [type isEqualToString:(NSString *)kUTTypeMovie])

    {// movie != video

        NSURL *urlvideo = [info objectForKey:UIImagePickerControllerMediaURL];

        NSString *tmpDir = NSTemporaryDirectory();

        tmpDir = [tmpDir  stringByAppendingString:@"jimin.mp4"];

        [self convertVideoQtimeToMpeg4:urlvideo withPath:tmpDir];

        

    }

    

    [picker dismissModalViewControllerAnimated:YES]; //dismiss 실행되면 dealloc자동 호출

}


- (void) convertVideoQtimeToMpeg4:(NSURL *) videoURL withPath:(NSString *)videoPath

{

    NSLog(@"======= convertVideoQtimeToMpeg4 ======");

    NSLog(@"videoURL :::: [%@]", videoURL);

    NSLog(@"videoPath :::: [%@]", videoPath);

    

    AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:videoURL options:nil];

    

    NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];

    

    if ([compatiblePresets containsObject:AVAssetExportPresetLowQuality])

    {

        AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:avAsset presetName:AVAssetExportPresetPassthrough];

        

        exportSession.outputURL = [NSURL fileURLWithPath:videoPath];

        

        exportSession.outputFileType = AVFileTypeMPEG4;

        

        CMTime start = CMTimeMakeWithSeconds(0.0, 600);

        

        CMTimeRange range = CMTimeRangeMake(start, [avAsset duration]);

        

        

        exportSession.timeRange = range;

        

        [exportSession exportAsynchronouslyWithCompletionHandler:^{

            

            switch ([exportSession status])

            {

                case AVAssetExportSessionStatusFailed:

                {

                    NSLog(@"Export failed: %@", [[exportSession error] localizedDescription]);

                }

                    break;

                    

                case AVAssetExportSessionStatusCompleted:

                {

                    NSLog(@"Export Success");

                }

                    break;

                    

                case AVAssetExportSessionStatusCancelled:

                {

                    NSLog(@"Export canceled");

                }

                    break;

                    

                default:

                    

                    break;

            }

        }];

    }

}

Posted by 모과이IT
,