我试图覆盖M4A文件中曲目编号元数据的值。 当我从现有轨道中读取值时:
if metadataIdentifier == AVMetadataIdentifier.iTunesMetadataTrackNumber{
print(track Number = \(itunesMetadata.value?.description)")
}
我得到了
track Number = <00000008 000E>
哪个结果是曲目编号 00000008 的十六进制表示以及总曲目 000E
通过做出这个假设,我试着像这样编写这个标签:
let trackNumber = String(format:"%08X", editedTrack?.trackNumber ?? 0)
let totalTrackNumber = String(format:"%04X", editedTrack?.totalTrackNumber ?? 0)
let trackNumberValue = "<\(dikNumber) \(totalDiskNumber)>"
let avMetaTrack = AVMutableMetadataItem()
avMetaTrack.keySpace = AVMetadataKeySpace.iTunes
avMetaTrack.key = AVMetadataIdentifier.iTunesMetadataTrackNumber as NSCopying & NSObjectProtocol
avMetaTrack.identifier = AVMetadataIdentifier.iTunesMetadataTrackNumber
avMetaTrack.value = trackNumberData as NSCopying & NSObjectProtocol
但它在itunes和外部标签编辑器中都不起作用。
编辑: 然后我通过
转换成新文件/* Export Metadata */
let urlAsset = AVURLAsset.init(url: trackFileManager.documentsUrl.appendingPathComponent(trackToEdit?.url ?? ""))
let assetExportSession: AVAssetExportSession! = AVAssetExportSession(asset: urlAsset, presetName: AVAssetExportPresetPassthrough)
assetExportSession.outputFileType = AVFileType.m4a
let stringUrlPath:[String] = (trackToEdit?.url?.components(separatedBy: "/"))!
assetExportSession.outputURL = trackFileManager.documentsUrl.appendingPathComponent("ex_\(stringUrlPath.last!)")
assetExportSession.metadata = newMetadata
print("\(logClassNameChild): output = \(assetExportSession.outputURL!)")
assetExportSession.exportAsynchronously(completionHandler: {
print("done")
//try! FileManager.default.removeItem(atPath: documentsUrl.appendingPathComponent(trackObject.url!).path)
//try! FileManager.default.moveItem(at: assetExportSession.outputURL!, to: documentsUrl.appendingPathComponent(trackObject.url!))
})
答案 0 :(得分:0)
事实证明,根据预期,数据需要像这样读取和写入
if metadataIdentifier == AVMetadataIdentifier.iTunesMetadataTrackNumber{
if let trackMetadataData = itunesMetadata.value as? Data{
let trackMetadataByteArray = [UInt8](trackMetadataData)
/*[0, 0, 0, 4, 0, 14, 0, 0]*/
if trackMetadataByteArray.count == 8{
print("\(logClassName): getItunesMetadata -> Track Number \(trackMetadataByteArray)")
/*Get track number -> Bytes 2,3*/
var trackArray = Array(trackMetadataByteArray[2...3])
let trackData = Data(bytes: &trackArray, count: 2)
tracKNumber = Int(Int16(bigEndian: trackData.withUnsafeBytes { $0.pointee }))
/*Get track total -> Bytes 4,5*/
var trackTotalArray = Array(trackMetadataByteArray[4...5])
let trackTotalData = Data(bytes: &trackTotalArray, count: 2)
totalTrack = Int(Int16(bigEndian: trackTotalData.withUnsafeBytes { $0.pointee }))
}
else{
print("\(logClassName): Error: Wrong track format")
}
}
else{
print("\(logClassName): Error: Wrong track format")
}
在另一个写作网站上:
var trackNumberByteArray = [UInt8]()
trackNumberByteArray.append(contentsOf: MetadataItem.int16ToByteArray(number: Int16(0)))
trackNumberByteArray.append(contentsOf: MetadataItem.int16ToByteArray(number: Int16(editedTrack?.trackNumber ?? 0)))
trackNumberByteArray.append(contentsOf: MetadataItem.int16ToByteArray(number: Int16(editedTrack?.totalTrackNumber ?? 0)))
trackNumberByteArray.append(contentsOf: MetadataItem.int16ToByteArray(number: Int16(0)))
let trackNumberData = Data(bytes: trackNumberByteArray)
print("MetadataItem: \(trackNumberData)")
let avMetaTrack = AVMutableMetadataItem()
avMetaTrack.keySpace = AVMetadataKeySpace.iTunes
avMetaTrack.key = AVMetadataIdentifier.iTunesMetadataTrackNumber as NSCopying & NSObjectProtocol
avMetaTrack.identifier = AVMetadataIdentifier.iTunesMetadataTrackNumber
avMetaTrack.value = trackNumberData as NSCopying & NSObjectProtocol