ios - 无法在iOS 13中转换为mp4

标签 ios objective-c ios13 format-conversion

在运行iOS 13的设备上,将.MOV视频转换为mp4时,[exportSession exportAsynchronouslyWithCompletionHandler:总是失败,并显示消息“操作无法完成”。但是,相同的代码可以在iOS上早于13(即12)运行。

- (void)encodeVideo:(NSString *)videoURL
{
   // Create the asset url with the video file
    AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:videoURL] options:nil];
    NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];

    // Check if video is supported for conversion or not
    if ([compatiblePresets containsObject: AVAssetExportPresetLowQuality])
    {
    //Create Export session
         AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:avAsset presetName:AVAssetExportPresetLowQuality];

    //Creating temp path to save the converted video
         NSString* documentsDirectory=     [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
         NSString* myDocumentPath= [documentsDirectory stringByAppendingPathComponent:@"temp.mp4"];
         NSURL *url = [[NSURL alloc] initFileURLWithPath:myDocumentPath];

    //Check if the file already exists then remove the previous file
         if ([[NSFileManager defaultManager]fileExistsAtPath:myDocumentPath])
         {
              [[NSFileManager defaultManager]removeItemAtPath:myDocumentPath error:nil];
         }
         exportSession.outputURL = url;
         //set the output file format if you want to make it in other file format (ex .3gp)
         exportSession.outputFileType = AVFileTypeMPEG4;
         exportSession.shouldOptimizeForNetworkUse = YES;

         [exportSession exportAsynchronouslyWithCompletionHandler:^{
         switch ([exportSession status])
         {
              case AVAssetExportSessionStatusFailed:
                   NSLog(@"Export session failed");
                   break;
              case AVAssetExportSessionStatusCancelled:
                   NSLog(@"Export canceled");
                   break;
              case AVAssetExportSessionStatusCompleted:
              {
                   //Video conversion finished
                   NSLog(@"Successful!");
              }
                   break;
              default:
                   break;
          }
         }];
    }
    else
    {
           NSLog(@"Video file not supported!");
    }
}

最佳答案

1。

创建文件夹 -

让filePath = documentDirectory.appendingPathComponent(“FolderName”)

    if !fileManager.fileExists(atPath: filePath.path) {

        do {

            try fileManager.createDirectory(atPath: filePath.path, withIntermediateDirectories: true, attributes: nil)

        } catch {

            print(error.localizedDescription)

            return nil

        }

    }

2。
Let url =  videoURL

    destinationURL = filePath.appendingPathComponent("filename.mp4")

    url.startAccessingSecurityScopedResource()

    do {

        try  FileManager.default.copyItem(at: url, to: destinationURL)

    } catch  {

        Logging.Log.error("EncodeVideo failed \(error.localizedDescription)")

    }

    url.startAccessingSecurityScopedResource()
  • 现在开始将Mov播放到MP4。
  • 关于ios - 无法在iOS 13中转换为mp4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58413218/

    相关文章:

    ios - SwiftUI 将触摸事件从 UIViewControllerRepresentable 传递给 View 后面

    iphone - 将 NSDate 拆分为年月日

    ios - 如何在当前队列中dispatch_after?

    iOS 13 语音控制辅助功能 - 有没有办法执行更多自定义操作(而不仅仅是链接到辅助功能标签)?

    iphone - 线程 1 : EXC_BAD_ACCESS (code=1, 地址=0x48) - iOS13.1 损坏(开发版本 2)

    ios - 为什么 Swift5 上的 CryptoSwift 文本加密失败?

    ios - NSUserDefaults。检索不正确的值

    ios - 迁移到 Twilio Voice 5.0(iOS 13、Xcode 11): Custom incoming call view controller, 拒绝来电问题

    ios - SwiftUI 设置状态栏样式

    ios - 映射关注者/关注与 Restkit 的关系