2

On a device running iOS 13, [exportSession exportAsynchronouslyWithCompletionHandler: always fails with message "The operation could not be completed" while converting .MOV video to mp4. However, the same code runs fine on iOS prior to 13 i.e 12. I am pasting below my complete method

- (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!");
    }
}
Ali Jawad
  • 21
  • 2
  • Have been trying different parameters there. It makes no difference when I put AVAssetExportPresetLowQuality in if condition. – Ali Jawad Oct 16 '19 at 12:26
  • Have you tried this solution? https://stackoverflow.com/a/58278117/3241041 – alxlives Oct 16 '19 at 12:39
  • @alxlives yes I have already tried this solution before, but it did not work in my case. Thank you for mentioning though. – Ali Jawad Oct 16 '19 at 12:42

1 Answers1

0

1.

Create Folder -

let 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()
  1. Start Mov to MP4 now it is working.