IOS UIImagePicker with mp4 format

痞子三分冷 提交于 2019-12-07 11:02:41

问题


Is it possible to save video and add it to custom ALAsset, captured from UIImagePicker in mp4 format? Or I have to save it in .mov and make compression by AVAssetExportSession?


回答1:


Yes, you can compress video using AVAssetExportSession. Here you can specify video type, quality and output url for compress video.

See below methods:

- (void) saveVideoToLocal:(NSURL *)videoURL {

    @try {
        NSArray *documentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *docPath = [documentsDirectory objectAtIndex:0];

        NSString *videoName = [NSString stringWithFormat:@"sampleVideo.mp4"];
        NSString *videoPath = [docPath stringByAppendingPathComponent:videoName];

        NSURL *outputURL = [NSURL fileURLWithPath:videoPath];

        NSLog(@"Loading video");

        [self convertVideoToLowQuailtyWithInputURL:videoURL outputURL:outputURL handler:^(AVAssetExportSession *exportSession) {

             if (exportSession.status == AVAssetExportSessionStatusCompleted) {
                 NSLog(@"Compression is done");
             }
             [self performSelectorOnMainThread:@selector(doneCompressing) withObject:nil waitUntilDone:YES];
         }];
    }
    @catch (NSException *exception) {
        NSLog(@"Exception :%@",exception.description);
        [self performSelectorOnMainThread:@selector(doneCompressing) withObject:nil waitUntilDone:YES];
    }
}


//---------------------------------------------------------------

- (void)convertVideoToLowQuailtyWithInputURL:(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:AVAssetExportPresetPassthrough];
    exportSession.outputURL = outputURL;
    exportSession.outputFileType = AVFileTypeMPEG4;
    [exportSession exportAsynchronouslyWithCompletionHandler:^(void) {
        handler(exportSession);
    }];
}

Here I saved compress video to document directory of application. You can check detail working of this in below sample code:

Sample demo:




回答2:


func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
    {
        picker.dismiss(animated: true, completion: nil)
        guard let mediaType = info[UIImagePickerControllerMediaType] as? String else
        {
            return
        }

        if mediaType == "public.movie"
        {
            if let  videoURL = info[UIImagePickerControllerMediaURL] as? URL
            {
                var videoData:Data!
                do {
                    videoData   = try Data(contentsOf: videoURL, options: [Data.ReadingOptions.alwaysMapped])
                }
                catch
                {
                    print(error.localizedDescription)
                    return
                }
                if videoData != nil
                {
                    let writePath = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("vid1.mp4")
                    print("writePath - \(writePath)")
                    do{
                        try videoData.write(to: writePath)
                    }catch{
                        print("Error - \(error.localizedDescription)")
                    }
                }
            }

        }
}


来源:https://stackoverflow.com/questions/13912139/ios-uiimagepicker-with-mp4-format

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