美文网首页首页投稿(暂停使用,暂停投稿)
选择本地的图片和视频,对视频进行压缩

选择本地的图片和视频,对视频进行压缩

作者: iOS乐乐 | 来源:发表于2017-06-23 13:21 被阅读0次
TZImagePickerController这是个可以选择的图片和视频选择的第三方的库
视频格式的压缩
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    //如果是视频返回的是URL
    NSURL*url=info[UIImagePickerControllerMediaURL];
    
    AVURLAsset * asset = [AVURLAsset assetWithURL:url];
    CMTime   time = [asset duration];
    int seconds = ceil(time.value/time.timescale);
    NSLog(@"seconds--->>%d",seconds);

    if (seconds < 5 ||seconds >60*5)
    {
        [MBProgressHUD showError:@"上传的视频时长最少5秒"];
        [self.obj dismissViewControllerAnimated:YES completion:nil];
        return;
    }
    else if (seconds >60*5)
    {
        [MBProgressHUD showError:@"上传的视频时长最大5分钟"];
        [self.obj dismissViewControllerAnimated:YES completion:nil];
        return;
    }
    //使用媒体工具 压缩
    [self compressVideo:url];
    [self.obj dismissViewControllerAnimated:YES completion:nil];
    
}

//压缩

-(void)compressVideo:(NSURL*)url
{
    //使用媒体工具(AVFoundation框架下的)
    //Asset 资源 可以是图片音频视频
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];

    //设置压缩的格式
    AVAssetExportSession *session=[AVAssetExportSession exportSessionWithAsset:asset presetName:AVAssetExportPreset1280x720];//mediumquality:中等质量
    
    //AVAssetExportPreset1280x720
    //设置导出路径
    NSString *path=[NSTemporaryDirectory() stringByAppendingPathComponent:@"needAV1.mp4"];

    session.shouldOptimizeForNetworkUse = YES;
    //设置到处路径
    session.outputURL=[NSURL fileURLWithPath:path];
    
    //设置输出文件的类型
    session.outputFileType=AVFileTypeMPEG4;
    
    //开辟子线程处理耗时操作
    [session exportAsynchronouslyWithCompletionHandler:^
    {
        NSLog(@"导出完成!路径:%@",path);
        int exportStatus = session.status;
        switch (exportStatus)
        {
            case AVAssetExportSessionStatusFailed:
            {
                DLog(@"失败");
            }
            case AVAssetExportSessionStatusCompleted:
            {
                DLog(@"完成");
                [self uploadVideoFromnetworking];
            }
        }
    }];
    
}


向网络上传视频
-(void)uploadVideoFromnetworkingWithFilePath:(NSString*)filePath
{
    MBProgressHUD*hud = [MBProgressHUD showHUDAddedTo:KWINDOW animated:YES];
    hud.mode=MBProgressHUDModeDeterminateHorizontalBar;
    hud.labelText =@"正在上传视频";
    
    NSString*urlStr=[NSString stringWithFormat:@"%@news/video_uploade",YuMing];
    
    NSDictionary*params=@{@"token":[USER objectForKey:@"token"]};
    
    [HTTPTools POST:urlStr parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
     {
         /**明见命名**/
         NSDateFormatter*formatter=[[NSDateFormatter alloc]init];
         formatter.dateFormat=@"yyyMMddHHmmss";
         NSString*str=[formatter stringFromDate:[NSDate date]];
         
         /**从本地读取数据**/
//         NSString *outfilePath=[NSTemporaryDirectory() stringByAppendingPathComponent:@"needAV1.mp4"];
//         NSURL * filePathURL = [NSURL fileURLWithPath:outfilePath];
         
         
         NSURL * filePathURL = [NSURL fileURLWithPath:filePath];
         
         NSData *videoData = [NSData dataWithContentsOfURL:filePathURL];
         
         
         
         NSString*fileName=[NSString stringWithFormat:@"%@.mp4",str];
         
         /**上传文件**/
         [formData appendPartWithFileData:videoData name:@"video" fileName: fileName mimeType:@"video/mp4"];
         
     } success:^(id responseObject)
     {
         if (responseObject)
         {
             DLog(@"----->>%@",responseObject)
             [hud removeFromSuperview];
         }
         
     } progress:^(NSProgress *progress)
     {
         NSString *str=[progress.localizedDescription componentsSeparatedByString:@"%"][0];
         CGFloat abc=[str floatValue];
         CGFloat X=abc/100;
         hud.progress=X;
         NSLog(@"上传的进度--->>%@",progress.localizedDescription);
         
     } failure:^(NSError *error)
     {
         [MBProgressHUD showError:Failed];
         [hud removeFromSuperview];
         DLog(@"%@",error);
         
     }];
    
    
}

相关文章

网友评论

    本文标题:选择本地的图片和视频,对视频进行压缩

    本文链接:https://www.haomeiwen.com/subject/vfrccxtx.html