ios - 以 Mp4 格式捕获视频

标签 ios objective-c

我正在使用 UIImagePickerController 录制视频,问题是它以 mov 格式录制视频以实现 android 兼容性。

我需要使用下面的代码将视频转换为 mp4 格式,问题是 6 秒的视频需要花费大约 30 到 35 秒的时间。
我可以直接以 mp4 格式或更快的方法录制视频的任何解决方案都会有很大帮助。提前致谢

   -(void)movToMp4:(NSURL *)videoURL{ // method for mov to mp4 conversion

    AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:videoURL options:nil];
    NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];
    if ([compatiblePresets containsObject:AVAssetExportPresetLowQuality])
    {
        AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:avAsset presetName:AVAssetExportPresetPassthrough];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString* videoPath = [NSString stringWithFormat:@"%@/xyz.mp4", [paths objectAtIndex:0]];
        exportSession.outputURL = [NSURL fileURLWithPath:videoPath];
        exportSession.outputFileType = AVFileTypeMPEG4;
        [exportSession exportAsynchronouslyWithCompletionHandler:^{
            switch ([exportSession status]) {  // switch case to get completion case where i put my delegate
                    return;
                    break;
                case AVAssetExportSessionStatusCompleted: {
                     [self.delegate mp4Response:videoPath];
                        break;

                    }

                }

            }
        }];
    }

}

最佳答案

是的,因为使用 AVURLAsset 从库/相册加载视频需要时间.

所以你需要使用block在这里从库中加载视频。

也行

[self.delegate mp4Response:videoPath];

在完成 block 中 - 它应该在主线程上。

遵循这种方法:
UIImagePickerController从库中获取视频的委托(delegate)方法。
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSURL *localUrl = (NSURL *)[info valueForKey:UIImagePickerControllerMediaURL];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* videoPath = [NSString stringWithFormat:@"%@/xyz.mp4", [paths objectAtIndex:0]];
    NSURL *outputURL = [NSURL fileURLWithPath:videoPath];

    [self convertVideoToLowQuailtyWithInputURL:localUrl outputURL:outputURL handler:^(AVAssetExportSession *exportSession)
     {
         if (exportSession.status == AVAssetExportSessionStatusCompleted) {
             NSLog(@"Capture video complete");
             [self performSelectorOnMainThread:@selector(doneCompressing) withObject:nil waitUntilDone:YES];
         }
     }];
    [self dismissViewControllerAnimated:YES completion:nil];
}


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

didFinishPickingMediaWithInfo方法观察到这条线:
[self performSelectorOnMainThread:@selector(doneCompressing) withObject:nil waitUntilDone:YES];

它将调用另一种方法doneCompressing在主线程中(在前台)。这样你就可以在 doneCompressing 中调用委托(delegate)方法.这将减少时间。
- (void) doneCompressing {
      [self.delegate mp4Response:videoPath];
}

关于ios - 以 Mp4 格式捕获视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27656069/

相关文章:

ios - 如何隐藏标签栏

ios - 为什么我不能用 swift 更改 if 语句中的变量?

ios - 在 IOS 上重新启动应用程序之前,Firebase 数据不会发送到服务器

objective-c - 在 NSString 中填充一个数字

iphone - 在后台无法获取位置更改通知

ios - NSMutableArray 即使在将数组插入其中后也是空的

ios - 如何在 Swift 中将新项目添加到我的多类型数组

ios - 在 segue 到子 uiviewcontroller ios 之后关闭自己的 uiviewcontroller?

c++ - Unsigned Long Long 超出范围?

iphone - "__workq_kernreturn"在 iOS 崩溃日志中或当您暂停应用程序执行时表示什么?