Solved

iOS copying/saving video using AVAssetExportSession and writeVideoAtPathToSavedPhotosAlbum

Posted on 2013-11-13
2
5,913 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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…
There is a security feature on iOS devices that is nearly impenetrable when it has been activated.  This article will provide some possible solutions as well as necessary steps to take to ensure you do not end up with a locked device.
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…
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…

832 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