是否可以使用TagLibSharp从MP3文件中删除Lyrics3v2标签? [英] Is it possible, using TagLibSharp, to remove a Lyrics3v2 tag from a MP3 file?

查看:121
本文介绍了是否可以使用TagLibSharp从MP3文件中删除Lyrics3v2标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能使用 Lyrics3v2 标签类型. "nofollow"> TagLibSharp 库.

I wonder if it's possible to remove a Lyrics3v2 tag type from a MP3 file using TagLibSharp library.

文档说,该块条目以单词" LYRICSBEGIN"开头"并以" LYRICS200 "结尾,它还说应该显示ID3标签以使 Lyrics3v2 标签存在...但是没有指定是否引用 ID3v1 ID3v2 标记或其中的任何一个,无论如何我都不理解该部分,因为 Lyrics3v2 标记是单个标记标记类型,它不是 ID3v1/ID3v2 标记类型的一部分,它在mp3标头上有其自己的条目,因此...我不理解关于 ID3v1/ID3v2的含义依赖关系".

This documentation says that the block entry starts with word "LYRICSBEGIN" and ends with "LYRICS200", also it says that the ID3 tag should be present to let exists the Lyrics3v2 tag ...but it doesn't specifies if reffers to ID3v1 or ID3v2 tag, or any of them, anyways I don't understand that part, because Lyrics3v2 tag is a single tag type, is not part of an ID3v1/ID3v2 tag type, it has its own entry on the mp3 header so... I don't understand what it means about the ID3v1/ID3v2 "dependancy".

无论如何,假设信息是正确的,那么我应该能够使用 TagLibSharp 从mp3文件中删除 ID3v1 ID3v2 标签包含 Lyrics3v2 标签,那么该标签也会被删除吗?但是,该标签仍然存在.

Anyways assuming that info is correct, then I should be able to use TagLibSharp to remove the ID3v1 and ID3v2 tags from the mp3 file containing the Lyrics3v2 tag then that tag will be removed too?, however, the tag still exists.

此外,公开 TagLibSharp 类的Lyrics属性似乎并不影响 Lyrics3v2 标记,这一切都很令人困惑.

Also, the Lyrics property that exposes TagLibSharp's classes seems doesn't affect to the Lyrics3v2 tag, all this is very confussing.

推荐答案

我使用taglibsharp编写了该解决方案:

I wrote this solution using taglibsharp:

' *************************************************************
' THIS CLASS IS PARTIALLY DEFINED FOR THIS STACKOVERFLOW ANSWER
' *************************************************************

Imports System.IO
Imports System.Text
Imports TagLib

''' <summary>
''' Represents the <c>Lyrics3</c> tag for a MP3 file.
''' </summary>
Public Class Lyrics3Tag

    Protected ReadOnly mp3File As Mpeg.AudioFile

    ''' <summary>
    ''' The maximum length for the <c>Lyrics3</c> block to prevent issues like removing a false-positive block of data.
    ''' <para></para>
    ''' Note that this is a personal attempt to prevent catastrophes, not based on any official info.
    ''' </summary>
    Private ReadOnly maxLength As Integer = 512 ' bytes

    Private Sub New()
    End Sub

    Public Sub New(ByVal mp3File As Mpeg.AudioFile)
        Me.mp3File = mp3File
    End Sub

    ''' <summary>
    ''' Entirely removes the <c>Lyrics3</c> tag.
    ''' </summary>
    <DebuggerStepThrough>
    Public Overridable Sub Remove()

        Dim initVector As New ByteVector(Encoding.UTF8.GetBytes("LYRICSBEGIN"))
        Dim initOffset As Long = Me.mp3File.Find(initVector, startPosition:=0)

        If (initOffset <> -1) Then

            ' The Lyrics3 block can end with one of these two markups, so we need to evaluate both.
            For Each str As String In {"LYRICS200", "LYRICSEND"}

                Dim endVector As New ByteVector(Encoding.UTF8.GetBytes(str))
                Dim endOffset As Long = Me.mp3File.Find(endVector, startPosition:=initOffset)

                If (endOffset <> -1) Then

                    Dim length As Integer = CInt(endOffset - initOffset) + (str.Length)
                    If (length < Me.maxLength) Then
                        Try
                            Me.mp3File.Seek(initOffset, SeekOrigin.Begin)
                            ' Dim raw As String = Me.mp3File.ReadBlock(length).ToString()
                            Me.mp3File.RemoveBlock(initOffset, length)
                            Exit Sub

                        Catch ex As Exception
                            Throw

                        Finally
                            Me.mp3File.Seek(0, SeekOrigin.Begin)

                        End Try

                    Else ' Length exceeds the max length.
                         ' We can handle it or continue...
                        Continue For

                    End If

                End If

            Next str

        End If

    End Sub

End Class

示例用法:

Dim mp3File As New Taglib.Mpeg.AudioFile("filepath")

Using lyrics As New Lyrics3Tag(mp3File)
    lyrics.Remove()
End Using

这篇关于是否可以使用TagLibSharp从MP3文件中删除Lyrics3v2标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆