IOS如何编写和读取元数据到视频?

时间:2017-08-31 16:34:29

标签: ios objective-c video

我正在尝试在我的应用内录制的视频中写入元数据信息。在视频拍摄结束时我有:

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

        AVMutableMetadataItem *mi2 = [AVMutableMetadataItem metadataItem];
        mi2.key = AVMetadataCommonKeyIdentifier;
        mi2.keySpace = AVMetadataKeySpaceCommon;
        mi2.value = @"testmetadata";
        [metadata addObject:mi2];

AVURLAsset *asset = [AVURLAsset URLAssetWithURL:self.outputURL options:nil];
        AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality];

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *outputURL_nuovo = paths[0];
        NSFileManager *manager = [NSFileManager defaultManager];
        [manager createDirectoryAtPath:outputURL_nuovo withIntermediateDirectories:YES attributes:nil error:nil];
        outputURL_nuovo = [outputURL_nuovo stringByAppendingPathComponent:@"output.mp4"];
        // Remove Existing File
        [manager removeItemAtPath:outputURL_nuovo error:nil];


        exportSession.outputURL = [NSURL fileURLWithPath:outputURL_nuovo];
        exportSession.shouldOptimizeForNetworkUse = YES;
        exportSession.outputFileType = AVFileTypeQuickTimeMovie;
        exportSession.metadata=metadata;
        CMTime start = CMTimeMakeWithSeconds(1.0, 600);
        CMTime duration = CMTimeMakeWithSeconds(3.0, 600);
        CMTimeRange range = CMTimeRangeMake(start, duration);
        exportSession.timeRange = range;
        [exportSession exportAsynchronouslyWithCompletionHandler:^(void)
         {
             switch (exportSession.status) {
                 case AVAssetExportSessionStatusCompleted:
                     [self writeVideoToPhotoLibrary:[NSURL fileURLWithPath:outputURL_nuovo]];
                     NSLog(@"Export Complete %d %@", exportSession.status, exportSession.error);
                     break;
                 case AVAssetExportSessionStatusFailed:
                     NSLog(@"Failed:%@",exportSession.error);
                     break;
                 case AVAssetExportSessionStatusCancelled:
                     NSLog(@"Canceled:%@",exportSession.error);
                     break;
                 default:
                     break;
             }


         }];

如果导出成功,我有另外一种方法:

-(void)writeVideoToPhotoLibrary: (NSURL*)url{

            ALAssetsLibrary* assetsLibrary = [[ALAssetsLibrary alloc] init];
            [assetsLibrary writeVideoAtPathToSavedPhotosAlbum:url
                                              completionBlock:^(NSURL *assetURL, NSError *error)
             {
                 [assetsLibrary assetForURL:assetURL resultBlock:^(ALAsset *asset) {
                     ALAssetRepresentation *assetRep = [asset defaultRepresentation];
                     NSLog(@"Image filename: %@", [assetRep filename]);
                     NSDate *date = [asset valueForProperty:ALAssetPropertyDate];
                     NSLog(@"DATA prima %@",date);
                     NSLog(@"TIMESTAMP  %f",[date timeIntervalSince1970]);

                     //////////get info
                     [assetsLibrary assetForURL:url resultBlock:^(ALAsset *asset) {
                         ALAssetRepresentation *assetRep = [asset defaultRepresentation];
                         NSLog(@"Image filename dopp: %@", [assetRep filename]);


                         NSLog(@"METADATA %@",[assetRep metadata]);

                     } failureBlock:^(NSError *error) {


                         NSLog(@"Error: %@", error.localizedDescription);
                     }];
                     ///////////////get info

                 } failureBlock:^(NSError *error) {
                     NSLog(@"Error: %@", error.localizedDescription);
                 }];

             }];
}

使用此代码,我试图保存带有一些元数据的视频,如标题,然后读取我刚刚保存的元数据。日志返回一个空值METADATA (null),我做错了什么?

0 个答案:

没有答案