首页 > 代码库 > 相册--视频压缩

相册--视频压缩

iOS视频压缩AVAssetExportSession

字数55 阅读3 评论0 

AVAssetExportSession是系统自带的压缩
首先导入头文件

#import <AVFoundation/AVFoundation.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import <AssetsLibrary/AssetsLibrary.h>

设置代理

<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

 

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC"; color: #1d9421 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; min-height: 21.0px } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1d9421 } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #703daa } p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3d1d81 } p.p7 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC"; color: #c91b13 } p.p8 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #6122ae } p.p9 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c91b13 } span.s1 { font: 18.0px Menlo } span.s2 { } span.s3 { color: #c32275 } span.s4 { color: #000000 } span.s5 { font: 18.0px "PingFang SC" } span.s6 { color: #6122ae } span.s7 { color: #3d1d81 } span.s8 { font: 18.0px Menlo; color: #000000 } span.s9 { font: 18.0px Menlo; color: #3d1d81 } span.s10 { color: #703daa } span.s11 { color: #0435ff } span.s12 { color: #c91b13 } span.s13 { font: 18.0px "PingFang SC"; color: #c91b13 } span.s14 { color: #294c50 } span.s15 { color: #1d9421 } span.s16 { font: 18.0px "PingFang SC"; color: #1d9421 }</style>

//打开视频库

- (IBAction)openLibrary:(id)sender {

    

    //    申请打开视频库权限

    

    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];

    if (authStatus == AVAuthorizationStatusRestricted

        || authStatus == AVAuthorizationStatusDenied) {

        NSLog(@"摄像头已被禁用,您可在设置应用程序中进行开启");

        return;

    }

    

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {

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

        picker.delegate = self;

        picker.allowsEditing = YES;

        picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

        picker.mediaTypes = @[(NSString *)kUTTypeMovie];

        [self presentViewController:picker animated:YES completion:NULL];

    } else {

        NSLog(@"手机不支持摄像");

    }

    

    

}

 

 

 

//视频选择完成后调用

 

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {

    //获取选择的媒体类型

    NSString *mediaType=[info objectForKey:UIImagePickerControllerMediaType];

    if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {

        NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];

        NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%ld_compressedVideo.mp4",time(NULL)]];

        

        NSLog(@"compressedVideoSavePath : %@",path);

        NSLog(@"选中,压缩前大小 %f MB",[self fileSize:videoURL]);

 

        //压缩

        AVURLAsset *avAsset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];

        NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];

        if ([compatiblePresets containsObject:AVAssetExportPresetHighestQuality]) {

            

            AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName:AVAssetExportPreset640x480];  //AVAssetExportPreset640x480  压缩质量(可更改)

            //输出URL

            exportSession.outputURL = [NSURL fileURLWithPath:path];

            //优化网络

            exportSession.shouldOptimizeForNetworkUse = true;

            //转换后的格式

            exportSession.outputFileType = AVFileTypeMPEG4;

            //异步导出

            [exportSession exportAsynchronouslyWithCompletionHandler:^{

                // 如果导出的状态为完成

                if ([exportSession status] == AVAssetExportSessionStatusCompleted) {

                    [self saveVideo:[NSURL fileURLWithPath:path]];

                    NSLog(@"压缩完毕,压缩后大小 %f MB",[self fileSize:[NSURL fileURLWithPath:path]]);

                }else{

                    NSLog(@"当前压缩进度:%f",exportSession.progress);

                }

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

                

            }];

        }

    }

    

    [self dismissViewControllerAnimated:YES completion:nil];

}

 

 

//存入相册

- (void)saveVideo:(NSURL *)outputFileURL

{

    //ALAssetsLibrary提供了我们对iOS设备中的相片、视频的访问。

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

    [library writeVideoAtPathToSavedPhotosAlbum:outputFileURL completionBlock:^(NSURL *assetURL, NSError *error) {

        if (error) {

            NSLog(@"保存视频失败:%@",error);

        } else {

            NSLog(@"保存视频到相册成功");

        }

    }];

}

 

 

//获取压缩后的大小

- (CGFloat)fileSize:(NSURL *)path

{

    return [[NSData dataWithContentsOfURL:path] length]/1024.00 /1024.00;

}

 

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC"; color: #1d9421 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #6122ae } span.s1 { font: 18.0px Menlo } span.s2 { } span.s3 { color: #c32275 } span.s4 { color: #000000 } span.s5 { color: #3d1d81 } span.s6 { font: 18.0px "PingFang SC" }</style>

//清除沙盒文件中, 压缩后的视频所有

- (void)removeCompressedVideoFromDocuments {

    NSFileManager *manager = [NSFileManager defaultManager];

    if ([manager fileExistsAtPath:(沙盒中的路径)]) {

        [[NSFileManager defaultManager] removeItemAtPath:(沙盒中的路径) error:nil];

    }

}

 

相册--视频压缩