Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6717
  • Last Modified:

iOS copying/saving video using AVAssetExportSession and writeVideoAtPathToSavedPhotosAlbum

I have some simple code that takes a video from the photo album and makes a copy of it.
The original video was created using the camera on the device (iPod Touch 5th Gen).

The code uses AVAssetExportSession to make a temporary copy of the video and then uses
writeVideoAtPathToSavedPhotosAlbum to save the copy back to the album.

The code works fine and makes a perfect copy except for the file type.

I am setting the OutputFileType as AVFileTypeMPEG4 and giving the temp file a name with a .mp4 extension, but the copy always ends up getting a new name with a .MOV extension.

Is that normal behavior for the writeVideoAtPathToSavedPhotosAlbum?

Here is the chunk of code that does the copying/saving -

asset = [AVURLAsset assetWithURL: [representation url]];
    NSURL *outputURL;
    NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:asset];
    if ([compatiblePresets containsObject:AVAssetExportPresetHighestQuality]) {
        AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]
                                               initWithAsset:asset presetName:AVAssetExportPresetHighestQuality];
        // Implementation continues.
        _success = false;
        NSString *filePath = nil;
        filePath = NSTemporaryDirectory();
            filePath = [filePath stringByAppendingPathComponent:@"test128"];
            filePath = [filePath stringByAppendingPathExtension:@".mp4"];
       
        outputURL = [NSURL fileURLWithPath:filePath];
        exportSession.outputURL = outputURL;
        exportSession.outputFileType = AVFileTypeMPEG4;
       
        [exportSession exportAsynchronouslyWithCompletionHandler:^{
           
            switch ([exportSession status]) {
                case AVAssetExportSessionStatusCompleted:
                    _success = true;
                    NSLog(@"Export Completed");
                    break;
                case AVAssetExportSessionStatusWaiting:
                    NSLog(@"Export Waiting");
                    break;
                case AVAssetExportSessionStatusExporting:
                    NSLog(@"Export Exporting");
                    break;
                case AVAssetExportSessionStatusFailed:
                {
                    NSError *error = [exportSession error];
                    NSLog(@"Export failed: %@", [error localizedDescription]);
                   
                    break;
                }
                case AVAssetExportSessionStatusCancelled:
                    NSLog(@"Export canceled");
                   
                    break;
                default:
                    break;
            }
           
            if (_success == true) {
               
                ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
                [assetLibrary writeVideoAtPathToSavedPhotosAlbum:outputURL completionBlock:^(NSURL *assetURL, NSError *error){
                    NSError *removeError = nil;
                    [[NSFileManager defaultManager] removeItemAtURL:outputURL error:&removeError];
                }];
               
            }
        }];
0
kevinvw1
Asked:
kevinvw1
1 Solution
 
EZFragCommented:
Hi There,

Yes, It seems to be the norm. Some people are complaining that the process of copying a movie file also takes a while. But I believe that it is Apple converting the video to the mov format before saving it to the media folder.

If this is the case, it might be better to set the output file type to mov from the beginning, and save some processing time.

I might be wrong, I might be right. But this is my take on it.

Kind Regards,
EZFrag
0
 
kevinvw1Author Commented:
Excellent, thanks for the confirmation.  

My clips are all super small (less than 5 seconds) so conversion time so far is negligible.

Thanks!

Kevin.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now