将视频转换成MP4

来源:互联网 发布:美工小刀片 编辑:程序博客网 时间:2024/06/10 01:09

- (NSURL *)convert2Mp4:(NSURL *)movUrl {

   NSURL *mp4Url = nil;

   AVURLAsset *avAsset = [AVURLAssetURLAssetWithURL:movUrl options:nil];

   NSArray *compatiblePresets = [AVAssetExportSessionexportPresetsCompatibleWithAsset:avAsset];

    

    if ([compatiblePresetscontainsObject:AVAssetExportPresetHighestQuality]) {

        AVAssetExportSession *exportSession = [[AVAssetExportSessionalloc]initWithAsset:avAsset

                                                                             presetName:AVAssetExportPresetHighestQuality];

        mp4Url = [movUrlcopy];

        mp4Url = [mp4UrlURLByDeletingPathExtension];

        mp4Url = [mp4UrlURLByAppendingPathExtension:@"mp4"];

        exportSession.outputURL = mp4Url;

        exportSession.shouldOptimizeForNetworkUse =YES;

        exportSession.outputFileType =AVFileTypeMPEG4;

        dispatch_semaphore_t wait =dispatch_semaphore_create(0l);

        [exportSession exportAsynchronouslyWithCompletionHandler:^{

           switch ([exportSession status]) {

                caseAVAssetExportSessionStatusFailed: {

                   NSLog(@"failed, error:%@.", exportSession.error);

                }break;

                caseAVAssetExportSessionStatusCancelled: {

                   NSLog(@"cancelled.");

                }break;

                caseAVAssetExportSessionStatusCompleted: {

                   NSLog(@"completed.");

                }break;

               default: {

                   NSLog(@"others.");

                }break;

            }

            dispatch_semaphore_signal(wait);

        }];

       long timeout = dispatch_semaphore_wait(wait,DISPATCH_TIME_FOREVER);

       if (timeout) {

           NSLog(@"timeout.");

        }

       if (wait) {

            //dispatch_release(wait);

            wait =nil;

        }

    }

    

   return mp4Url;

}

0 0
原创粉丝点击