Solved

iOS copying/saving video using AVAssetExportSession and writeVideoAtPathToSavedPhotosAlbum

Posted on 2013-11-13
2
5,985 Views
Last Modified: 2013-11-14
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
Comment
Question by:kevinvw1
2 Comments
 
LVL 9

Accepted Solution

by:
EZFrag earned 500 total points
ID: 39647325
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
 

Author Closing Comment

by:kevinvw1
ID: 39648044
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

     Intro      In this article, I will show you how to parse a JSON in Swift using an open source library called SwiftyJSON (https://github.com/SwiftyJSON/SwiftyJSON). If you haven't heard of a JSON before, it stands for JavaScript Object Notation…
Set up iPhone and iPad email signatures to always send in high-quality HTML with this step-by step guide.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question