【问题标题】:Powershell editing mp3 infosPowershell 编辑 mp3 信息
【发布时间】:2015-07-18 06:09:51
【问题描述】:

我正在寻找一种在 PowerShell 脚本中编辑 mp3 文件信息(如艺术家、专辑等)的方法。

我找到了一种获取 .mp3 文件所需信息的方法,但不知道如何修改它们。

$songs = Get-ChildItem $dir -Filter *.mp3;
$shell = new-object -com shell.application;

Foreach ($song in $songs) {
    $shellfolder = $shell.namespace($dir);
    $shellfile = $shellfolder.parsename($song);

    $title = $shell.namespace($dir).getdetailsof($shellfile,21);
}

使用 getDetailsOf (with 21) 我可以获取歌曲标题,但不存在 setDetailsOf,所以我不知道如何修改歌曲标题。

【问题讨论】:

  • 您是否尝试过使用 mp3 标签?这位谦虚的博主找到了一种使用 Powershell 的方法。 Todd Klindt's blog
  • 谢谢 Rubanov,这正是我所需要的。它就像一个魅力!

标签: powershell mp3


【解决方案1】:

如果您像我一样不喜欢使用库,这里有一个我编写的非常简单的 ID3v1 函数,可以满足您的需求:

    #Set the specified ID3v1 properties of a file by writing the last 128 bytes
    Function Set-ID3v1( #All parameters except path are optional, they will not change if not specified. 
      [string]$path, #Full path to the file to be updated - wildcards not supported because [] are so stinky and it's only supposed to work on one file at a time. 
      [string]$Title = "`0", #a string containing only 0 indicates a parameter not specified. 
      [string]$Artist  = "`0",
      [string]$Album = "`0",
      [string]$Year = "`0",
      [string]$Comment = "`0",
      [int]$Track = -1,
      [int]$Genre = -1, 
      [bool]$BackDate=$true){#Preserve modification date, but add a minute to indicate it's newer than duplicates
        $CurrentModified = (Get-ChildItem -LiteralPath $path).LastWriteTime #use literalpath here to get only one file, even if it has []
        Try{
            $enc = [System.Text.Encoding]::ASCII #Probably wrong, but works occasionally. See https://stackoverflow.com/questions/9857727/text-encoding-in-id3v2-3-tags
            $currentID3Bytes = New-Object byte[] (128)
            $strm = New-Object System.IO.FileStream ($path,[System.IO.FileMode]::Open,[System.IO.FileAccess]::ReadWrite,[System.IO.FileShare]::None)
            $strm.Seek(-128,'End') | Out-Null #Basic ID3v1 info is 128 bytes from EOF
            $strm.Read($currentID3Bytes,0,$currentID3Bytes.Length) | Out-Null
            Write-Host "$path `nCurrentID3: $($enc.GetString($currentID3Bytes))"
            $strm.Seek(-128,'End') | Out-Null #Basic ID3v1 info is 128 bytes from EOF
            If($enc.GetString($currentID3Bytes[0..2]) -ne  'TAG'){
                Write-Warning "No existing ID3v1 found - adding to end of file"
                $strm.Seek(0,'End') 
                $currentID3Bytes = $enc.GetBytes(('TAG' + (' ' * (30 + 30 + 30 + 4 + 30)))) #Add a blank tag to the end of the file
                $currentID3Bytes += 255 #empty Genre
                $strm.Write($currentID3Bytes,0,$currentID3Bytes.length)
                $strm.Flush()
                $Strm.Close()
                $strm = New-Object System.IO.FileStream ($path,[System.IO.FileMode]::Open,[System.IO.FileAccess]::Write,[System.IO.FileShare]::None)
                $strm.Seek(-128,'End') 
            } 
            $strm.Seek(3,'Current') | Out-Null #skip over 'TAG' to get to the good stuff
            If($Title -eq "`0"){ $strm.Seek(30,'Current') | Out-Null} #Skip over
             Else{ $strm.Write($enc.GetBytes($Title.PadRight(30,' ').Substring(0,30)),0,30)  } #if specified, write 30 space-padded bytes to the stream
            If($Artist -eq "`0"){ $strm.Seek(30,'Current') | Out-Null} 
             Else {$strm.Write($enc.GetBytes($Artist.PadRight(30,' ').Substring(0,30)),0,30) }
            If($Album -eq "`0"){ $strm.Seek(30,'Current') | Out-Null} 
             Else{$strm.Write($enc.GetBytes($Album.PadRight(30,' ').Substring(0,30)),0,30)  }
            If($Year -eq "`0"){ $strm.Seek(4,'Current') | Out-Null} 
             Else {$strm.Write($enc.GetBytes($Year.PadRight(4,' ').Substring(0,4)),0,4) }
            If(($Track -ne -1) -or ($currentID3Bytes[125] -eq 0)) {$CommentMaxLen = 28}Else{$CommentMaxLen = 30} #If a Track is specified or present in the file, Comment is 28 chars
            If($Comment -eq "`0"){ $strm.Seek($CommentMaxLen,'Current') | Out-Null} 
             Else {$strm.Write($enc.GetBytes($Comment.PadRight($CommentMaxLen,' ').Substring(0,$CommentMaxLen)),0,$CommentMaxLen)  }
            If($Track -eq -1 ){$strm.Seek(2,'Current') | Out-Null}
             Else{$strm.Write(@(0,$Track),0,2)} #Track, if present, is preceded by a 0-byte to form the last two bytes of Comment
            If($Genre -ne -1){$strm.Write($Genre,0,1) | Out-Null} 
        }Catch{
            Write-Error $_.Exception.Message
        }Finally{
            If($strm){
                $strm.Flush()
                $strm.Close()
            }
        }
        If($BackDate){(Get-ChildItem -LiteralPath $path).LastWriteTime = $CurrentModified.AddMinutes(1)}
    }

您可以使用 MP3 文件的完整路径以及您想要更改的任何 ID3V1 属性来调用它:

    Set-ID3v1 -path "c:\users\me\Desktop\Test.mp3" -Year 1996 -Title "This is a test" 

基本上它用标题、艺术家等写入文件的最后 128 个字节。 我可能会添加对 ID3v2.3 的支持,它更加灵活,但与旧设备和软件的兼容性较差。检查GitHub 以获取最新版本。

【讨论】:

  • 是否没有简单的方法可以访问给定文件类型(例如字体文件)的扩展属性(companyauthorcopyrightlegal trademark 等)?我不知道如何使用 powershell 脚本,但我打算在 nodejs 中使用 child_process 来访问 powershell 或 cmd 的实例
  • 上面的脚本仅适用于嵌入在文件最后 128 字节中的 MP3 ID3v1 信息。公司、作者等不属于ID3v1 spec。你看过this question吗?
  • 是的,我已经看过那个 Q 并且没有骰子。我提到的那些扩展属性对应于字体文件。你知道有什么方法可以得到吗?
  • [Shell.Application](docs.microsoft.com/en-us/previous-versions/windows/desktop/… 对象可能会帮助您找到字体属性。Here's a script I wrote 帮助您入门。
  • 非常感谢我如何运行节点中的 shell 脚本?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多