5

是否可以保存视频并将其添加到从 UIImagePicker 以 mp4 格式捕获的自定义 ALAsset?或者我必须将它保存在 .mov 中并通过 AVAssetExportSession 进行压缩?

4

2 回答 2

2

是的,您可以使用AVAssetExportSession. 在这里您可以指定压缩视频的视频类型、质量和输出 url。

见以下方法:

- (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);
    }];
}

在这里,我将压缩视频保存到应用程序的文档目录中。您可以在下面的示例代码中检查此操作的详细信息:

示例演示:

于 2014-11-26T09:35:00.753 回答
0
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)")
                    }
                }
            }

        }
}
于 2018-07-04T06:21:10.270 回答