我通常使用 MP3Gain 应用程序来设置mp3文件的重播增益。
应用程序可以在mp3文件的 APEv2 标记中创建这些字段:
(截图取自 WinAmp 播放器)
使用 TagLibSharp 库我写了 ID3v1 和 ID3v2 解析器,现在,我想知道我是否可以读写提到使用此库的 APEv2 字段?。
我认为 MP3Gain 应用程序使用字段的唯一名称,因此 TagLibsharp 可能不支持它们,但 TagLibsharp 库有{我认为我需要使用的{1}},ReadBlock()
,Removeblock()
和Find()
方法,但我不确切知道如何将它们结合使用...
这是我唯一拥有的:
RFind()
这是一个用Vb.Net编写的伪代码,只是为了说明预期的抽象或行为。
Dim file As New TagLib.Mpeg.AudioFile("C:\input.mp3")
Dim data As Byte() = Encoding.ASCII.GetBytes("MP3GAIN_MINMAX")
Dim vector As New ByteVector(data)
Dim offset As Long = file.Find(vector)
我认为我最终自己完成了“阅读”部分,但是我不知道如何编写块,因为如果字段不存在肯定我可以覆盖/破坏文件......
Imports TagLib
Public NotInheritable Class Mp3File
Private tagFile As Global.TagLib.Mpeg.AudioFile
Public ReadOnly Property APEv2 As APEv2Tag
Get
Return Me.apeTagB
End Get
End Property
Private ReadOnly apeTagB As APEv2Tag
Public Sub New(ByVal file As FileInfo)
Me.tagFile = New Global.TagLib.Mpeg.AudioFile(file.FullName)
Me.apeTagB = New APEv2Tag(Me.tagFile)
End Sub
End Class
''' <summary>
''' Represents the APEv2 tag for a MP3 file.
''' </summary>
Public Class APEv2Tag
Protected ReadOnly mp3File As Global.TagLib.Mpeg.AudioFile
Public Sub New(ByVal mp3File As Global.TagLib.Mpeg.AudioFile)
Me.mp3File = mp3File
End Sub
Public Overridable Property MP3GAIN_MINMAX As Double
Get
If field exists then...
Return TheValue...
End If
End Get
Set(ByVal value As Double)
...
End Set
End Property
' More properties here...
End Class
答案 0 :(得分:3)
从TagLib#文件获取APEv2特定信息的过程类似于this answer中描述的ID3v2标记的过程。
以下是阅读值的方法:
// Get the APEv2 tag if it exists.
TagLib.Ape.Tag ape_tag = (TagLib.Ape.Tag)file.GetTag(TagLib.TagTypes.Ape, false);
if(ape_tag != null) {
// Get the item.
TagLib.Ape.Item item = ape_tag.GetItem("MP3GAIN_MINMAX");
if (item != null) {
Console.Log(item.ToStringArray());
}
}
我不确定屏幕截图中的字段是单个字符串还是两个用逗号连接的字符串。
保存将是相反的方向,但更简单:
// Get the APEv2 tag if it exists.
TagLib.Ape.Tag ape_tag = (TagLib.Ape.Tag)file.GetTag(TagLib.TagTypes.Ape, true);
if(ape_tag != null) {
ape_tag.SetValue("MP3GAIN_MINMAX", value);
}
file.Save();
一个例子,对于Vb.Net:
字段:
ReadOnly mp3File As Global.TagLib.Mpeg.AudioFile = ...
属性(MP3Gain相关):
Property MP3GainMinMax As String
Get
Return Me.GetField("MP3GAIN_MINMAX")
End Get
Set(ByVal value As String)
Me.SetField("MP3GAIN_MINMAX", value)
End Set
End Property
Property MP3GainUndo As String
Get
Return Me.GetField("MP3GAIN_UNDO")
End Get
Set(ByVal value As String)
Me.SetField("MP3GAIN_UNDO", value)
End Set
End Property
Property ReplayGainTrackGain As String
Get
Return Me.GetField("REPLAYGAIN_TRACK_GAIN")
End Get
Set(ByVal value As String)
Me.SetField("REPLAYGAIN_TRACK_GAIN", value)
End Set
End Property
Property ReplayGainTrackPeak As String
Get
Return Me.GetField("REPLAYGAIN_TRACK_PEAK")
End Get
Set(ByVal value As String)
Me.SetField("REPLAYGAIN_TRACK_PEAK", value)
End Set
End Property
功能'获取':
Function GetField(ByVal fieldName As String) As String
Dim apeTag As TagLib.Ape.Tag =
DirectCast(Me.mp3File.GetTag(TagTypes.Ape, create:=False), TagLib.Ape.Tag)
If (apeTag IsNot Nothing) Then
Dim item As TagLib.Ape.Item = apeTag.GetItem(fieldName)
If (item IsNot Nothing) Then
Return item.ToString()
End If
End If
Return String.Empty
End Function
方法'设置':
Sub SetField(ByVal fieldName As String, ByVal value As String)
Dim apeTag As TagLib.Ape.Tag =
DirectCast(Me.mp3File.GetTag(TagTypes.Ape, create:=True), TagLib.Ape.Tag)
apeTag.SetValue(fieldName, value)
End Sub