シナリオ
私は通常、MP3Gainアプリケーションを使用して、mp3 ファイルの再生ゲインを設定します。
アプリケーションは、mp3 ファイルのAPEv2タグに次のフィールドを作成できます。
(スクリーンショットはWinAmpプレーヤーから取得したものです)
質問
TagLibSharpライブラリを使用してID3v1およびID3v2パーサーを作成しましたが、このライブラリを使用して前述のAPEv2フィールドを読み書きできるかどうか疑問に思っています。
リサーチ
MP3Gainアプリはフィールドに一意の名前を使用しているため、おそらくTagLibsharpはそれらをサポートしていないと思いますが、TagLibsharpライブラリにはReadBlock()
、Removeblock()
、Find()
およびRFind()
メソッドがあり、そのために使用する必要があると思いますが、正確な使用方法はわかりませんそれらを組み合わせて...
これは私が持っている唯一のものです:
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)
これは、予想される抽象化または動作を示すためだけに Vb.Net で記述された疑似コードです。
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
アップデート:
ようやく自分で「読み取り」部分を完成させたと思いますが、フィールドが存在しないとファイルを上書き/破損する可能性があるため、ブロックの書き込み方法がわかりません...
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Gets the <c>MP3GAIN_MINMAX</c> metatada field of the audio file.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <returns>
''' The <c>MP3GAIN_MINMAX</c> field value.
''' </returns>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
Private Function GetFieldMP3GainMinMax() As String
Dim data As Byte() = Encoding.UTF8.GetBytes("MP3GAIN_MINMAX")
Dim vector As New ByteVector(data)
Dim offset As Long = Me.mp3File.Find(vector)
Dim result As String
If (offset = -1) Then
Return String.Empty
Else
Try
offset += ("MP3GAIN_MINMAX".Length + 1)
Me.mp3File.Seek(offset, SeekOrigin.Begin)
result = Me.mp3File.ReadBlock(8).ToString.TrimEnd()
Return result
Catch ex As Exception
Throw
Finally
Me.mp3File.Seek(0, SeekOrigin.Begin)
End Try
End If
End Function
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Gets the <c>MP3GAIN_UNDO</c> metatada field of the audio file.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <returns>
''' The <c>MP3GAIN_UNDO</c> field value.
''' </returns>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
Private Function GetFieldMP3GainUndo() As String
Dim data As Byte() = Encoding.UTF8.GetBytes("MP3GAIN_UNDO")
Dim vector As New ByteVector(data)
Dim offset As Long = Me.mp3File.Find(vector)
Dim result As String
If (offset = -1) Then
Return String.Empty
Else
Try
offset += ("MP3GAIN_UNDO".Length + 1)
Me.mp3File.Seek(offset, SeekOrigin.Begin)
result = Me.mp3File.ReadBlock(12).ToString.TrimEnd()
Return result
Catch ex As Exception
Throw
Finally
Me.mp3File.Seek(0, SeekOrigin.Begin)
End Try
End If
End Function
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Gets the <c>REPLAYGAIN_TRACK_GAIN</c> metatada field of the audio file.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <returns>
''' The <c>REPLAYGAIN_TRACK_GAIN</c> field value.
''' </returns>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
Private Function GetFieldReplayGainTrackGain() As String
Dim data As Byte() = Encoding.UTF8.GetBytes("REPLAYGAIN_TRACK_GAIN")
Dim vector As New ByteVector(data)
Dim offset As Long = Me.mp3File.Find(vector)
Dim result As String
If (offset = -1) Then
Return String.Empty
Else
Try
offset += ("REPLAYGAIN_TRACK_GAIN".Length + 1)
Me.mp3File.Seek(offset, SeekOrigin.Begin)
result = Me.mp3File.ReadBlock(12).ToString.TrimEnd()
Return result
Catch ex As Exception
Throw
Finally
Me.mp3File.Seek(0, SeekOrigin.Begin)
End Try
End If
End Function
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Gets the <c>REPLAYGAIN_TRACK_PEAK</c> metatada field of the audio file.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <returns>
''' The <c>REPLAYGAIN_TRACK_PEAK</c> field value.
''' </returns>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
Private Function GetFieldReplayGainTrackPeak() As String
Dim data As Byte() = Encoding.UTF8.GetBytes("REPLAYGAIN_TRACK_PEAK")
Dim vector As New ByteVector(data)
Dim offset As Long = Me.mp3File.Find(vector)
Dim result As String
If (offset = -1) Then
Return String.Empty
Else
Try
offset += ("REPLAYGAIN_TRACK_PEAK".Length + 1)
Me.mp3File.Seek(offset, SeekOrigin.Begin)
result = Me.mp3File.ReadBlock(8).ToString.TrimEnd()
Return result
Catch ex As Exception
Throw
Finally
Me.mp3File.Seek(0, SeekOrigin.Begin)
End Try
End If
End Function