修改 iTunesMetadataTrackNumber 音频文件 M4A swift

Posted

技术标签:

【中文标题】修改 iTunesMetadataTrackNumber 音频文件 M4A swift【英文标题】:modifying iTunesMetadataTrackNumber Audio file M4A swift 【发布时间】:2018-02-09 10:21:40 【问题描述】:

我正在尝试覆盖 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!))
        )

【问题讨论】:

如何将avMetaDisk 链接回您的文件? avMetaDisk 是如何与newMetadata 关联的? 【参考方案1】:

事实证明,按照预期,Data 需要像这样读写

        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

【讨论】:

以上是关于修改 iTunesMetadataTrackNumber 音频文件 M4A swift的主要内容,如果未能解决你的问题,请参考以下文章

怎么修改文件修改日期

电脑文件修改时间和实际修改时间不一样

ce修改器使用方法

Sql Server 增加字段修改字段修改类型修改默认值(转)

网卡MAC怎么修改

关于windows 2003域控账户修改密码问题,客户端账号过期的时候修改密码提示“您无权修改密码”。