编辑 mp4 / m4a 文件的元标签

Editing metatags of mp4 / m4a files

我想用 iOS.

的 ObjC 编辑 mp4 / m4a 文件上的 TitleAuthor 标签

这可能吗?

可能有不止一种方法,但是AVAssetExportSession很简单而且有效。

N.B. 这将创建一个新文件。 AVFoundation 并没有真正 就地修改。

#import <AVFoundation/AVFoundation.h>
#import <CoreMedia/CoreMedia.h>

// ...

NSURL *outputURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/output.m4a", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]]];
[[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil];

NSURL *inputURL = [[NSBundle mainBundle] URLForResource:@"foo" withExtension:@"m4a"];
AVAsset *asset = [AVAsset assetWithURL:inputURL];

AVAssetExportSession *session = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetPassthrough];
session.outputURL = outputURL;
session.outputFileType = AVFileTypeAppleM4A;

AVMutableMetadataItem *metaTitle = [[AVMutableMetadataItem alloc] init];

metaTitle.identifier = AVMetadataCommonIdentifierTitle;                     // more in AVMetadataIdentifiers.h
metaTitle.dataType = (__bridge NSString *)kCMMetadataBaseDataType_UTF8;     // more in CoreMedia/CMMetadata.h
metaTitle.value = @"Choon!";

AVMutableMetadataItem *metaArtist = [[AVMutableMetadataItem alloc] init];
metaArtist.identifier = AVMetadataCommonIdentifierArtist;
metaArtist.dataType = (__bridge NSString *)kCMMetadataBaseDataType_UTF8;
metaArtist.value = @"Me, of course";

session.metadata = @[metaTitle, metaArtist];

[session exportAsynchronouslyWithCompletionHandler:^{
    if (session.status == AVAssetExportSessionStatusCompleted) {
        // hurray
    }
}];

此示例适用于 m4a 个文件,您需要将文件扩展名更改为 mp4,将 outputFileType 更改为 AVFileTypeMPEG4