【问题标题】:Manipulate UserDefined tag (TXXX frame) with Taglib-Sharp使用 Taglib-Sharp 操作用户定义的标签(TXXX 帧)
【发布时间】:2015-08-23 16:29:33
【问题描述】:

情况与任务

我有大量音乐收藏,我想使用 PowerShell 和 taglip-sharp 清理他们的 ID3V2 tags。一些标签如commentencoding 应删除,而其他标签如artisttitle 不应删除。

Usually you manipulate ID3 tags this way (Simplified version)

# Add taglib dll 
[Void][System.Reflection.Assembly]::LoadFrom("$PSScriptRoot\taglib-sharp.dll")

# Load example mp3 into memory as [taglib.file]
$media = [TagLib.File]::Create("C:\path\to\musicFile.mp3")

# Change comment tag 
$media.tag.tags[0].Comment = "Hello World"

# Save tags back to mp3 file
$Media.Save()

问题

许多音乐文件在 called TXXX 框架中存储自定义信息,例如 URLShop Name。不幸的是,使用上面显示的方法无法访问此框架。或者我还没有找到方法。

改为使用

# Read UserTextInformationFrame
$media.GetTag([TagLib.TagTypes]::Id3v2).GetFrames("TXXX")

这个User defined text information frame 可以包含多个值。还有一些很有用,因为 Foobar 之类的音乐播放器在 TXXX 中存储 PERFORMERDATEreplay_track_gain 标签。

上述行的示例输出可能是:

Description  : replaygain_track_gain
Text         : {-5.00 dB}
FieldList    : {-5.00 dB}
TextEncoding : Latin1
FrameId      : {84, 88, 88, 88}
Size         : 32
Flags        : None
GroupId      : -1
EncryptionId : -1

Description  : URL
Text         : {www.amazon.com}
FieldList    : {www.amazon.com}
TextEncoding : UTF16
FrameId      : {84, 88, 88, 88}
Size         : 43
Flags        : None
GroupId      : -1
EncryptionId : -1

在此之后,我能够过滤掉所有不必要的 TXXX 值

# Create a whitelist of TXXX frames
$goodTXXX  = 'performer','replaygain_track_gain','date'

# Read UserTextInformationFrame AND filter it
$newTXXX = $Media.GetTag([TagLib.TagTypes]::Id3v2).GetFrames("TXXX") | 
  where { $goodTXXX -contains $_.Description }   

问题:如何将多个值写入TXXX

所以我的问题是,如何将过滤后的结果保存回 mp3 文件?
我失败的尝试是:

$media.GetTag([TagLib.TagTypes]::Id3v2).RemoveFrames("TXXX")
$media.GetTag([TagLib.TagTypes]::Id3v2).SetTextFrame("TXXX",$newTXXX)
# Removes old values, but does not show anything in Foobar

#$media.GetTag([TagLib.TagTypes]::Id3v2).GetFrames("TXXX").SetText("Hello World")
# Shows garbage in Foobar. And it's not usable for multiple values

Taglib-Sharp documentation for SetTextFrame


额外问题: taglib-sharp 是否能够在将新标签保存为 ID3v2.3 标签的同时去除 Id3v1 和 ID3v2.4 标签? (Related SO answer, but doesn't distinguish between v2.3 and v2.4)

【问题讨论】:

    标签: powershell taglib-sharp


    【解决方案1】:

    我找到了一种尝试错误的方法。这并不优雅,因为如果您只想更改一个值,则必须删除所有 TXXX 值并将它们添加回来

    # Add taglib dll 
    [Void][System.Reflection.Assembly]::LoadFrom("$PSScriptRoot\taglib-sharp.dll")
    
    # Load example mp3 into memory as [taglib.file]
    $media = [TagLib.File]::Create("C:\path\to\musicFile.mp3")
    
    # Get or create the ID3v2 tag.
    [TagLib.Id3v2.Tag]$id3v2tag = $media.GetTag([TagLib.TagTypes]::Id3v2, 1)
    
    # Create new 'TXXX frame' object
    $TXXXFrame = [TagLib.Id3v2.UserTextInformationFrame]("WWW")
    
    # Delete complete TXXX frame first, or else all values are just appended
    $id3v2tag.RemoveFrames("TXXX")
    
    # Set the value/text in the newly created TXXX frame, default Text encoding is UTF8
    # Use curly brackets instead of single quotation marks
    $TXXXFrame.Text = {www.myurl.com}
    
    # Add TXXX frame to tag
    $id3v2tag.AddFrame($TXXXFrame)
    
    # Write all changed tags back to file
    $media.Save()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-31
      • 1970-01-01
      • 2016-04-03
      • 2019-01-27
      • 1970-01-01
      • 2018-07-27
      相关资源
      最近更新 更多