You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用TagLib-Sharp在PowerShell中为.m4b文件添加自定义标签的技术求助

使用TagLib-Sharp在PowerShell中为.m4b文件添加自定义标签的技术求助

大家好,我目前正在写一个PowerShell脚本,用TagLib-Sharp批量更新我的有声书.m4b文件标签——数据来源是Audiobookshelf生成的metadata.json。现在脚本已经能正常读取JSON数据,更新.m4b里的默认标签(比如标题、作者、流派这些),但卡在了添加自定义标签这一步:比如我想给文件加上ISBN、ASIN、系列名称、系列编号这些自定义字段,看了不少C#/VB的TagLib-Sharp例子,却没法转换成能运行的PowerShell语法。

先跟大家同步下我的进度:

Update: 我已经搞定了自定义标签的创建和写入逻辑,等把剩下的功能完成测试后,会把最终脚本更新上来。

下面是我目前的WIP脚本(结构还比较乱,但核心功能能跑),重点看末尾#Write Tags to File开始的部分:

# Load the assembly that will handle the MP3 tagging.
[Reflection.Assembly]::LoadFrom("F:\taglib-sharp\TagLibSharp.2.3.0\lib\net462\taglibsharp.dll")

Function GetSeriesNumber($InputString) {
    $regExResult = $InputString | Select-String -Pattern '(?:[^#](?!(#)))+$'
    $regExResult.Matches[0].Value
}

Function GetSeriesName($InputString) {
    $regExResult = $InputString | Select-String -Pattern '.*(#)'
    $regExResult.Matches[0].Value
}

#Get parent path for metadata updates
$pathName = Read-Host "Enter the starting path (ex. d:\test\)"
Write-Host "Path=$pathName"

#Variables
$abFileExt = "*.m4b"
$jsonFileName = "\metadata.json"
$directories = Get-ChildItem -Path $pathName -Directory -Recurse
$directories += $pathName

foreach ($directory in $directories) {
    Write-Host $directory
    ## Get relevant files
    $files = Get-ChildItem -Path $directory -Filter $abFileExt

    ## Loop through the files
    foreach ($filename in $files) {
        # Read the JSON file
        # Check if the file exists
        if (-Not (Test-Path -Path $directory$jsonFileName)) {
            Write-Host "File $jsonFileName at $directory does not exist. Exiting script." -ForegroundColor Red
            Exit
        }
        # Continue with the rest of the script if the file exists
        Write-Host "File exists. Proceeding with the script." -ForegroundColor Green

        $jsonContent = Get-Content -Path $directory$jsonFileName -Raw
        # Convert JSON content to a PowerShell object
        $jsonObject = $jsonContent | ConvertFrom-Json

        # Access properties of the JSON object
        $seriesFullName = $jsonObject.series
        Write-Output $seriesFullName
        $seriesPattern = "/^.*#/"
        $title = $jsonObject.title
        $authors = $jsonObject.authors
        $tags = $jsonObject.tags
        $chapters = $jsonObject.chapters
        $subtitle = $jsonObject.subtitle
        $narrators = $jsonObject.narrators
        $genres = $jsonObject.genres
        $publishedYear = $jsonObject.publishedYear
        $publishedDate = $jsonObject.publishedDate
        $publisher = $jsonObject.publisher
        $descrption = $jsonObject.description
        $isbn = $jsonObject.isbn
        $asin = $jsonObject.asin
        $language = $jsonObject.language
        $explicit = $jsonObject.explicit
        $abridged = $jsonObject.abridged

        Write-Host "title: "$title " authors: "$authors" narrators: "$narrators

        if (0 -eq $tags.Count) {
            $abtagsJoined = "Unknown"
        } else {
            $abtagsJoined = [system.String]::Join(",", $tags)
        }

        if (0 -eq $authors.Count) {
            $authorsJoined = "Unknown"
        } else {
            ($authorsJoined = [system.String]::Join(",", $authors))
        }

        if (0 -eq $narrators.Count) {
            $narratorsJoined = "Unknown"
        } else {
            $narratorsJoined = [system.String]::Join(",", $narrators)
        }

        if (0 -eq $genres.Count) {
            $genresJoined = "Unknown"
        } else {
            $genresJoined = [system.String]::Join("`r`n", $genres)
        }

        if (0 -eq $chapters.Count) {
            $chaptersJoined = "0"
        } else {
            $chaptersJoined = [system.String]::Join(",", $chapters)
        }

        #parse series and number
        $seriesNumber = GetSeriesNumber -InputString $seriesFullName
        Write-Host "Number Result = '$($seriesNumber)'"
        $seriesName = (GetSeriesName -InputString $seriesFullName) -replace ".{2}$"
        Write-Host "Name Result = '$($seriesName)'"

        #Write Tags to File
        Write-Host "Editing $fileName" -ForegroundColor Blue
        $updateFile = [TagLib.File]::Create($fileName)

        # change existing tags in the file
        $updateFile.Tag.Title = $title
        $updateFile.Tag.AlbumArtists = $authors
        $updateFile.Tag.Album = $title
        $updateFile.Tag.Composers = $narratorsJoined
        $updateFile.Tag.Genres = $genresJoined

        Write-Host "Saving $fileName" -ForegroundColor Blue
        $updateFile.Save()
    }
}

我需要的是PowerShell专属的例子,演示怎么用TagLib-Sharp给.m4b(MPEG4容器)文件添加自定义标签——注意我看到的例子大多是针对ID3v2(MP3)的,而.m4b用的是MPEG4的标签体系,虽然TagLib-Sharp支持这个功能,但我找不到对应的PowerShell实现。

有没有大佬能给我指个方向,或者直接给一段能嵌入到我现有脚本里的代码片段?谢谢大家!

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.07 07:53:09