能否仅用PowerShell批量提取FLAC文件的LYRICS元数据至文本文件?
纯PowerShell提取FLAC同步歌词方案
一、FLAC文件LYRICS对应的Shell属性编号
Windows Shell中,FLAC的同步歌词(带时间戳)对应的属性编号通常是147(属性名System.Music.Lyrics)。你可以通过以下PowerShell命令验证实际编号:
$shell = New-Object -ComObject Shell.Application $folder = $shell.Namespace("C:\你的FLAC文件所在文件夹路径") # 遍历属性编号查找Lyrics相关项 for ($i = 0; $i -lt 255; $i++) { $propName = $folder.GetDetailsOf($folder.Items().Item(0), $i) if ($propName -match "Lyrics") { Write-Host "属性编号: $i,属性名: $propName" } }
执行后会输出Lyrics对应的具体编号,若与147不同,以实际输出为准。
二、修改后的PowerShell提取代码(基于Shell.Application)
以下代码会遍历指定目录下的FLAC文件,提取歌词并保存为同名.txt文件:
$targetDir = "C:\你的FLAC文件目录" $shell = New-Object -ComObject Shell.Application $folder = $shell.Namespace($targetDir) # 替换为你实际查到的Lyrics属性编号,示例用147 $lyricsPropId = 147 foreach ($file in Get-ChildItem $targetDir -Filter *.flac) { $shellFile = $folder.ParseName($file.Name) $lyrics = $folder.GetDetailsOf($shellFile, $lyricsPropId) if (-not [string]::IsNullOrEmpty($lyrics)) { $outputPath = Join-Path $targetDir "$($file.BaseName).txt" $lyrics | Out-File -FilePath $outputPath -Encoding UTF8 Write-Host "已提取歌词至: $outputPath" } else { Write-Host "$($file.Name) 未找到歌词元数据" } } # 释放COM对象 [System.Runtime.Interopservices.Marshal]::ReleaseComObject($shell) | Out-Null
三、替代纯PowerShell方案(无需Shell.Application)
如果Shell属性无法读取到同步歌词,可直接解析FLAC的Vorbis Comment标签(同步歌词通常存于LYRICS或SYLT标签),以下是基于TagLibSharp的方案:
- 下载
TagLibSharp.dll(从NuGet包提取后放到脚本同目录) - 执行以下代码:
Add-Type -Path ".\TagLibSharp.dll" $targetDir = "C:\你的FLAC文件目录" foreach ($file in Get-ChildItem $targetDir -Filter *.flac) { $tagFile = [TagLib.File]::Create($file.FullName) # 读取普通歌词标签 $lyrics = $tagFile.Tag.Lyrics if (-not [string]::IsNullOrEmpty($lyrics)) { $outputPath = Join-Path $targetDir "$($file.BaseName).txt" $lyrics | Out-File -FilePath $outputPath -Encoding UTF8 Write-Host "已提取歌词至: $outputPath" } else { # 尝试读取SYLT同步歌词标签 $syltTags = $tagFile.Tag.GetTag([TagLib.TagTypes]::VorbisComment).GetField("SYLT") if ($syltTags) { $syltContent = $syltTags -join "`n" $outputPath = Join-Path $targetDir "$($file.BaseName)_sync.txt" $syltContent | Out-File -FilePath $outputPath -Encoding UTF8 Write-Host "已提取同步歌词至: $outputPath" } else { Write-Host "$($file.Name) 未找到任何歌词标签" } } }
该方案直接操作FLAC原生标签,兼容性更强,能准确抓取同步歌词。
内容的提问来源于stack exchange,提问作者Calysmin38
相关产品推荐
相关产品推荐

