Solved

iOS copying/saving video using AVAssetExportSession and writeVideoAtPathToSavedPhotosAlbum

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

What's a UDID? If you're involved in developing, testing, or even reviewing an iOS application that's in beta, then at some point you may need to know the UDID for any iOS devices that you'll be testing on. What's the UDID? It stands for Unique Dev…
Are you looking to clear some space on your phone for the latest iOS 8 update? Did you switch to Spotify so you no longer need to keep music native on your phone? Run out of space for taking photos while in the middle of vacation? Sometimes the quic…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
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.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now