10

我正在尝试使用与我的应用相关的自定义元数据保存视频,并在用户从库中选择该视频时尝试检索它。我不确定我是否正确保存了元数据,因为当我尝试检索元数据时看不到任何内容。我也不确定我是否正确检索元数据。我是iOS新手,感谢任何帮助。我搜索了许多线程和开发人员库,但无法使其正常工作。

我正在尝试将元数据保存在 recordingDidFinishToOutputFileURL 委托函数中。视频正在库中保存。

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

    NSLog(@"Output saving:%@",outputFileURL);
    AVAsset *video = [AVAsset assetWithURL:outputFileURL];
    AVAssetExportSession *exportSession = [AVAssetExportSession      exportSessionWithAsset:video presetName:AVAssetExportPresetPassthrough];
    exportSession.shouldOptimizeForNetworkUse = YES;
    exportSession.outputFileType = AVFileTypeMPEG4;
    exportSession.metadata = metadata;
    exportSession.outputURL = outputFileURL;
    [exportSession exportAsynchronouslyWithCompletionHandler:^{
        NSLog(@"done processing video!");
        UISaveVideoAtPathToSavedPhotosAlbum(outputFileURL.path, self, @selector(video:didFinishSavingWithError: contextInfo:), nil); 
    }];

我正在尝试在 didFinishPickingMediaWithInfo 委托函数中检索视频以检查元数据,但在 completionhandler 函数中看不到任何内容

        if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {
        video_selected = TRUE;
        NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];

        NSLog(@"video has %@", videoURL.path);
        AVAsset *videoAsset = [AVAsset assetWithURL:videoURL];
        NSLog(@"Loading metadata...");
        NSArray *keys = [[NSArray alloc] initWithObjects:@"commonMetadata", nil];
        NSMutableArray *metadata = [[NSMutableArray alloc] init];
        [videoAsset loadValuesAsynchronouslyForKeys:keys completionHandler:^{

            [metadata removeAllObjects];
            for (NSString *format in [videoAsset availableMetadataFormats])
            {
                [metadata addObjectsFromArray:[videoAsset metadataForFormat:format]];
                NSLog(@"Printing metadata-%@",metadata);
            }


        }];
4

2 回答 2

0

check your errors. You're exporting to where the file already exists so it's exporting into itself which won't work. Just export to a different location

于 2014-02-15T16:28:31.270 回答
0

这些键工作正常:

  • AVMetadataCommonKeyAuthor
  • AVMetadataCommonKeyDescription
  • AVMetadataCommonKey版权所有

MP4 文件支持 ISO 用户数据键空间和 iTunes 元数据键空间。上面的键没有 ISO 用户数据映射,所以这就是它们不起作用的原因。同样,您不能将 AVMetadataQuickTimeMetadataKey* 放在 MP4 文件中,因为它不支持该键空间。

var metadata: [AVMetadataItem] = []
let mi = AVMutableMetadataItem()
mi.identifier = AVMetadataIdentifier.commonIdentifierDescription
mi.keySpace = .common
mi.value = "Milan Description" as (NSCopying & NSObjectProtocol)?
metadata.append(mi)

let mi1 = AVMutableMetadataItem()
mi1.identifier = AVMetadataIdentifier.commonIdentifierCopyrights
mi1.keySpace = .common
mi1.value = "Milan copy" as (NSCopying & NSObjectProtocol)?
metadata.append(mi1)

print("Output saving:\(trackObj.getTrackUrl())")
let video = AVAsset(url: trackObj.getTrackUrl())
let exportSession = AVAssetExportSession(asset: video, presetName: AVAssetExportPresetPassthrough)
exportSession?.metadata = metadata

exportSession?.outputURL = volumeBoosterViewModel.audioResultRootFolderURL.appendingPathComponent(trackObj.filePathName)
exportSession?.outputFileType = AVFileType.mp3

exportSession?.exportAsynchronously(completionHandler: {[weak self]
    () -> Void in
    if exportSession!.status == AVAssetExportSession.Status.completed  {
        // All is working fine!!
        print("success")
        //get metadata
        let asset = AVAsset(url: Your url)
        if asset.commonMetadata.count > 0 {
            for item in asset.metadata {
                //get metadata
                print(item.value)
            }
        }
    }
})
于 2021-07-28T09:14:04.070 回答