mp3标签乱码
0 1
[1 楼] 八大行星 [资深泡菜]
10-23 07:01
我的车机在播放mp3是大多数中文名都显示为乱码

乱码的样子和下面这里的差不多。

而这些文件在Windows英文版里看到的效果也是一样的。但在控制面版中的语言高级设置中将non-unicode编码默认改为中文就没有这个问题了。

我推测原编码可能是GB2312,我想是不是改为UTF-8能解决问题? 让ChatGPT写了一段Powershell脚本。它处理之后,在Windows中显示成这个样子:


我觉得按理说改成UTF-8在Windows里应该显示没问题,这是哪里出了问题吗?

# ==========================================
# PowerShell 7 脚本:
# 检测 MP3 标签编码(仅当是 GB2312 时才转为 UTF-8)
# 递归处理当前目录及子目录
# 需要 taglib-sharp.dll 支持 (https://github.com/mono/taglib-sharp/releases)
# ==========================================
Add-Type -Path ".\taglib-sharp.dll"
$encGB2312 = [System.Text.Encoding]::GetEncoding("GB2312")
$encUTF8   = [System.Text.Encoding]::UTF8
# 检测文本是否是 GB2312 编码的函数
function Test-IsGB2312([string]$text) {
    if ([string]::IsNullOrWhiteSpace($text)) { return $false }
    try {
        # 获取字节后重新按 GB2312 解码,看是否出现非法替换字符
        $bytes = [System.Text.Encoding]:: default.GetBytes($text)
        $decoded = $encGB2312.GetString($bytes)
        # 如果包含大量问号或替换符,则可能不是 GB2312
        $ratio = ($decoded.ToCharArray() | Where-Object { $_ -eq '?' }).Count / [math]::Max(1, $decoded.Length)
        return ($ratio -lt 0.2)  # 问号比例小于20%视为可解析
    } catch {
        return $false
    }
}
# GB2312 → UTF-8 转换函数
function Convert-IfGB2312([string]$text) {
    if ([string]::IsNullOrWhiteSpace($text)) { return $text }
    if (Test-IsGB2312 $text) {
        try {
            $bytes = $encGB2312.GetBytes($text)
            return $encUTF8.GetString($bytes)
        } catch {
            return $text
        }
    } else {
        return $text
    }
}
# 查找所有 mp3 文件
$mp3Files = Get-ChildItem -Path . -Filter *.mp3 -Recurse -File
Write-Host "🔍 共找到 $($mp3Files.Count) 个 MP3 文件。开始检测与转换..." -ForegroundColor Cyan
foreach ($fileItem in $mp3Files) {
    try {
        $mp3 = [TagLib.File]::Create($fileItem.FullName)
        $originalTitle   = $mp3.Tag.Title
        $originalAlbum   = $mp3.Tag.Album
        $originalComment = $mp3.Tag.Comment
        $originalArtist  = ($mp3.Tag.Performers -join ", ")
        $newTitle   = Convert-IfGB2312 $originalTitle
        $newAlbum   = Convert-IfGB2312 $originalAlbum
        $newComment = Convert-IfGB2312 $originalComment
        $newArtist  = Convert-IfGB2312 $originalArtist
        # 仅当内容发生变化才写回
        if ($newTitle -ne $originalTitle -or
            $newAlbum -ne $originalAlbum -or
            $newComment -ne $originalComment -or
            $newArtist -ne $originalArtist) {
            $mp3.Tag.Title      = $newTitle
            $mp3.Tag.Album      = $newAlbum
            $mp3.Tag.Comment    = $newComment
            $mp3.Tag.Performers = @($newArtist)
            $mp3.Save()
            Write-Host "✅ 已转换: $($fileItem.FullName)" -ForegroundColor Green
        } else {
            Write-Host "⏭️ 跳过: $($fileItem.FullName)(无GB2312编码)" -ForegroundColor DarkGray
        }
    }
    catch {
        Write-Warning "⚠️ 无法处理文件: $($fileItem.FullName) —— $($_.Exception.Message)"
    }
}
Write-Host "🎉 处理完成!" -ForegroundColor Yellow
[2 楼] Trompette [泡菜]
10-24 20:13
直觉是命令行里不能混编码传参,很容易出问题

解决车机显示乱码,最简单的办法是用 MP3tag 里的 文件名->Tag 的转换功能