How to reduce the file size of a video recorded through my iOS app on a phone

笑着哭i 提交于 2019-12-12 03:04:31

问题


I am making an iOS app (Android soon) that involves users recording videos on their phones through the app. But I need the app to reduce the default size of those videos so that when they are uploaded to my server less bandwidth is used and it uploads faster. What is the best way to do that?


回答1:


Try this code hope your task done.

- (void)convertVideoToLowQuailty:(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:AVAssetExportPresetLowQuality];
        exportSession.outputURL = outputURL;
        exportSession.outputFileType = AVFileTypeQuickTimeMovie;
        [exportSession exportAsynchronouslyWithCompletionHandler:^(void) 
        {
            handler(exportSession);
            [exportSession release];
        }];
    }

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

    {   
        NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
        NSURL *outputURL = [NSURL fileURLWithPath:@"/Users/Hems/Desktop/output.mov"];
        [self convertVideoToLowQuailty:videoURL outputURL:outputURL handler:^(AVAssetExportSession *exportSession)
         {
             if (exportSession.status == AVAssetExportSessionStatusCompleted)
             {
                 printf("completed\n");
             }
             else
             {
                 printf("error\n");

             }
         }];

    }


来源:https://stackoverflow.com/questions/40012758/how-to-reduce-the-file-size-of-a-video-recorded-through-my-ios-app-on-a-phone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!