5

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.

Stu Thompson
  • 38,370
  • 19
  • 110
  • 156
Sahana Kamath
  • 201
  • 2
  • 8

3 Answers3

1

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

alex-i
  • 5,406
  • 2
  • 36
  • 56
0

You can also set the metadata in the videoWriter so something like =>

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

videoWriter.metadata = metadata;

where videoWriter is of type AVAssetWriter

and then when you stop recording you call =>

[videoWriter endSessionAtSourceTime:CMTimeMake(durationInMs, 1000)];
[videoWriter finishWritingWithCompletionHandler:^() {
    ALAssetsLibrary *assetsLib = [[ALAssetsLibrary alloc] init];
    [assetsLib writeVideoAtPathToSavedPhotosAlbum:videoUrl
   completionBlock:^(NSURL* assetURL, NSError* error) {
         if (error != nil) {
             NSLog( @"Video not saved");
         }
     }];
 }];
Adam Freeman
  • 1,271
  • 12
  • 20
0

There is no officially supported way to do this.

What you may do: Store the info you want to save in a separate database. The downside however is that such information is then only available in your app.

What exactly are you trying to accomplish?

gnat
  • 6,213
  • 108
  • 53
  • 73
holtmann
  • 6,043
  • 32
  • 44
  • The videos recorded from my app are saved in the Camera Roll. When I fetch all the videos from Camera Roll, I want to identify the videos saved from my app. How can this be done? – Sahana Kamath May 09 '11 at 03:51
  • You can use the url method of ALAssetRepresentation to identify the asset. – holtmann Jul 05 '11 at 06:34
  • The url of each asset is unique and persistent (According to Apple documentation). But I have observed that the URL's are not persistent when there is an iPhone OS upgrade. Different url's were getting generated for the same assets on upgrading the iPhone OS from 4.2.1 to 4.3.2. – Sahana Kamath Aug 25 '11 at 14:02
  • Yes, I observed that as well. At the moment there is no reliable way to identify an asset under 4.x. – holtmann Sep 07 '11 at 07:40