【发布时间】:2016-04-07 07:54:56
【问题描述】:
我想使用适用于 iOS 的 ObjC 编辑 mp4 / m4a 文件上的 Title 和 Author 标签。
这可能吗?
【问题讨论】:
标签: ios objective-c tags m4a
我想使用适用于 iOS 的 ObjC 编辑 mp4 / m4a 文件上的 Title 和 Author 标签。
这可能吗?
【问题讨论】:
标签: ios objective-c tags m4a
可能有不止一种方法,但AVAssetExportSession 很简单且有效。
注意这会创建一个新文件。 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。
【讨论】: