以 mp4 格式录制、保存和/或转换视频? [英] Record, save and/or convert video in mp4 format?

查看:23
本文介绍了以 mp4 格式录制、保存和/或转换视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下问题 - 我正在尝试创建一个记录视频的应用程序,然后将其保存到相机胶卷,然后将该视频上传到网络.问题是唯一支持的格式是mp4",但我的视频是mov".

I have the following problem - I am trying to create an app that records video, then save it to the camera roll and after that I am uploading that video to the web. The problem is that the only supported format is "mp4", but my videos are "mov".

所以我的问题是如何以mp4"格式保存来自相机的视频,或将其保存为mov"然后将其转换为mp4".

So my question is how to save video from camera in "mp4" format, or save it in "mov" and then convert it to "mp4".

这是我的代码:

  • 这就是我打开相机的方式:

  • this is how I open the camera:

picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
picker.showsCameraControls = YES;
picker.allowsEditing = YES;
picker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
[self presentViewController:picker animated:YES completion:nil];

  • 这是我保存视频的方式:

  • this is how I save the video:

    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
    
    if (CFStringCompare ((__bridge_retained CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo)
    {
        NSString *moviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
        videoURL = info[UIImagePickerControllerMediaURL];
    
        if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(moviePath))
        {
            UISaveVideoAtPathToSavedPhotosAlbum(moviePath, self, nil, nil);
        }
    }
    [nextScreenButton setTitle:@"ПРОДЪЛЖИ" forState:UIControlStateNormal];
    [self dismissViewControllerAnimated:YES completion:nil];
    

  • 提前致谢!

    推荐答案

    你做得对.. 现在你需要将这个 mov 文件转换为 mp4,如下所示.

    You are doing right thing.. Now you need to convert this mov file to mp4 as below.

    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
    NSString *videoPath1 = @"";
    if (CFStringCompare ((__bridge_retained CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo)
     {
       if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(moviePath))
       {
             NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
             NSString *moviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
             videoPath1 =[NSString stringWithFormat:@"%@/xyz.mov",docDir];
             NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
             NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
             [videoData writeToFile:videoPath1 atomically:NO];
           //  UISaveVideoAtPathToSavedPhotosAlbum(moviePath, self, nil, nil);
       }
     }
    
        AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:videoPath1] 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);
            videoPath = [NSString stringWithFormat:@"%@/xyz.mp4", [paths objectAtIndex:0]];
            exportSession.outputURL = [NSURL fileURLWithPath:videoPath];
            NSLog(@"videopath of your mp4 file = %@",videoPath);  // PATH OF YOUR .mp4 FILE
            exportSession.outputFileType = AVFileTypeMPEG4;
    
          //  CMTime start = CMTimeMakeWithSeconds(1.0, 600);
          //  CMTime duration = CMTimeMakeWithSeconds(3.0, 600);           
          //  CMTimeRange range = CMTimeRangeMake(start, duration);            
          //   exportSession.timeRange = range;        
          //  UNCOMMENT ABOVE LINES FOR CROP VIDEO   
            [exportSession exportAsynchronouslyWithCompletionHandler:^{
    
                switch ([exportSession status]) {
    
                    case AVAssetExportSessionStatusFailed:
                        NSLog(@"Export failed: %@", [[exportSession error] localizedDescription]);
    
                        break;
    
                    case AVAssetExportSessionStatusCancelled:
    
                        NSLog(@"Export canceled");
    
                        break;
    
                    default:
    
                        break;
    
                }
                 UISaveVideoAtPathToSavedPhotosAlbum(videoPath, self, nil, nil);
                [exportSession release];
    
            }];
    
        }
    [nextScreenButton setTitle:@"ПРОДЪЛЖИ" forState:UIControlStateNormal];
    [self dismissViewControllerAnimated:YES completion:nil];
    

    这篇关于以 mp4 格式录制、保存和/或转换视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆