【问题标题】:Writing metadata to ALAsset将元数据写入 ALAsset
【发布时间】:2011-08-10 20:34:42
【问题描述】:

我正在为 iPhone 开发一个视频应用程序。我正在录制视频并使用 AssetsLibrary 框架将其保存到 iPhone 相机胶卷。我使用的API是:

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

有没有办法使用 ALAsset 将视频的自定义元数据保存到相机胶卷。如果使用 AssetsLibrary 框架无法做到这一点,可以使用其他方法来完成。基本上,我有兴趣将我的应用程序的详细信息作为视频元数据的一部分。

【问题讨论】:

    标签: iphone video metadata alasset assetslibrary


    【解决方案1】:

    从 iOS 4+ 开始,有了 AVFoundation 框架,它还允许您从视频文件读取/写入元数据。只有特定的键可用于使用此选项添加元数据,但我认为这不会有问题。

    这是一个小示例,您可以使用它为您的视频添加标题(但是,在此示例中,所有较旧的元数据都已删除):

        // 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
    }];
    

    这也显示了一些例子:avmetadataeditor

    【讨论】:

      【解决方案2】:

      没有官方支持的方式来做到这一点。

      您可以做什么:将要保存的信息存储在单独的数据库中。然而,缺点是此类信息仅在您的应用中可用。

      你到底想完成什么?

      【讨论】:

      • 从我的应用程序录制的视频保存在相机胶卷中。当我从相机胶卷中获取所有视频时,我想识别从我的应用程序中保存的视频。如何做到这一点?
      • 您可以使用 ALAssetRepresentation 的 url 方法来识别资产。
      • 每个资产的 url 都是唯一且持久的(根据 Apple 文档)。但我观察到,当 iPhone 操作系统升级时,URL 不是持久的。将 iPhone OS 从 4.2.1 升级到 4.3.2 时,会为相同的资产生成不同的 url。
      • 是的,我也观察到了这一点。目前没有可靠的方法来识别低于 4.x 的资产。
      【解决方案3】:

      您还可以在 videoWriter 中设置元数据,例如 =>

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

      videoWriter 的类型为 AVAssetWriter

      然后当您停止录制时,您会调用 =>

      [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");
               }
           }];
       }];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-15
        • 2013-12-28
        • 2017-01-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-11
        • 1970-01-01
        相关资源
        最近更新 更多