AVAssetExportSession:trackID 不持久

问题描述 投票:0回答:1

我正在向现有视频添加画外音轨道,其中可能包括音轨。我正在使用 AVAssetExportSession 将视频渲染为 .mov。

我想跟踪哪个音轨是画外音,因此如果用户稍后想要编辑画外音,我知道要更新哪个音轨。我想我会使用一个唯一的 trackID。

所以我打电话:

let compositionAudioTrack = composition.addMutableTrack(withMediaType: .audio, preferredTrackID:  myAudioTrackID)!

其中 myAudioTrackID 是我生成的随机 Int32。我已经确认返回的compositionAudioTrack的trackID确实是= myAudioTrackID。

但是当我使用 AVAssetExportSession 渲染视频时,trackID 被重新分配给其他数字。

我如何跟踪哪个音轨是我的?

ios avfoundation avassetexportsession avkit
1个回答
0
投票

确实

AVAssetExportSession
会丢弃您选择的那些 trackID。

但是私人使用RFC 4646语言标签允许您将自己的数据嵌入到轨道中。您可以使用它们来识别您的轨迹。

此示例中有一堆已弃用的 API 警告,我可能稍后会修复。

func doExport() {
    var composition = AVMutableComposition()
    
    let asset1 = AVURLAsset(url: inputAudioTrack1Url)
    let track1 = asset1.tracks(withMediaType: .audio).first!
    
    let asset2 = AVURLAsset(url: inputAudioTrack2Url)
    let track2 = asset2.tracks(withMediaType: .audio).first!

    // these IDs do not end up in the exported file
    let cTrack1 = composition.addMutableTrack(withMediaType: .audio, preferredTrackID: 10)!
    let cTrack2 = composition.addMutableTrack(withMediaType: .audio, preferredTrackID: 11)!

    // https://datatracker.ietf.org/doc/html/rfc4646
    // private use tags language tags to
    cTrack1.extendedLanguageTag = "x-my-tag-\(123)"
    cTrack2.extendedLanguageTag = "x-my-tag-\(456)"

    let range = CMTimeRange(start: .zero, end: CMTimeMake(value: 3, timescale: 1))
    try! cTrack1.insertTimeRange(range, of: track1, at: .zero)
    try! cTrack2.insertTimeRange(range, of: track2, at: .zero)

    try? FileManager.default.removeItem(at: outputUrl)
    
    let session = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality)!
    session.outputURL = outputUrl
    session.outputFileType = .mov
    
    session.exportAsynchronously {
        print("finiiiished: \(session.status.rawValue), \(session.error)")
        self.doimport()
    }
}

func doImport() {
    let asset1 = AVURLAsset(url: outputUrl)
    let tracks = asset1.tracks(withMediaType: .audio)
    
    var track: AVAssetTrack
    for track in tracks {
        print("track language tag \(track.extendedLanguageTag)")
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.