Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing metadata to ALAsset

I am developing a video app for iPhone. I am recording a video and saving it to iPhone Camera Roll using AssetsLibrary framework. The API that I have used is:

- (void)writeVideoAtPathToSavedPhotosAlbum:(NSURL *)videoPathURL 
  completionBlock:(ALAssetsLibraryWriteVideoCompletionBlock)completionBlock

Is there any way to save custom metadata of the video to the Camera Roll using ALAsset. If this is not possible using AssetsLibrary framework, can this be done using some other method. Basically I am interested in writing details about my app as a part of the video metadata.

like image 784
Sahana Kamath Avatar asked Nov 14 '22 00:11

Sahana Kamath


1 Answers

Since iOS 4+ there is the AVFoundation framework, which also lets you read/write metadata from/to video files. There are only specific keys that you can use to add metadata using this option, but I don't believe it would be a problem.

Here's a small example that you can use to add a title to your videos (however, in this example all older metadata is removed):

    // prepare metadata (add title "title")
NSMutableArray *metadata = [NSMutableArray array];
AVMutableMetadataItem *mi = [AVMutableMetadataItem metadataItem];
mi.key = AVMetadataCommonKeyTitle;
mi.keySpace = AVMetadataKeySpaceCommon;
mi.value = @"title";
[metadata addObject:mi];

    // prepare video asset (SOME_URL can be an ALAsset url)
AVURLAsset *videoAsset = [[AVURLAsset alloc] initWithURL:SOME_URL options:nil];

    // prepare to export, without transcoding if possible
AVAssetExportSession *_videoExportSession = [[AVAssetExportSession alloc] initWithAsset:videoAsset presetName:AVAssetExportPresetPassthrough];
[videoAsset release];
_videoExportSession.outputURL = [NSURL fileURLWithPath:_outputPath];
_videoExportSession.outputFileType = AVFileTypeQuickTimeMovie;
_videoExportSession.metadata = metadata;
[_videoExportSession exportAsynchronouslyWithCompletionHandler:^{
    switch ([_videoExportSession status]) { 
        case AVAssetExportSessionStatusFailed:
            NSLog(@"Export failed: %@", [[_videoExportSession error] localizedDescription]);                
        case AVAssetExportSessionStatusCancelled:
            NSLog(@"Export canceled");
        default:
            break;
    }
    [_videoExportSession release]; _videoExportSession = nil;
    [self finishExport];  //in finishExport you can for example call writeVideoAtPathToSavedPhotosAlbum:completionBlock: to save the video from _videoExportSession.outputURL
}];

This also shows some examples: avmetadataeditor

like image 130
alex-i Avatar answered Dec 21 '22 08:12

alex-i