Last active
May 26, 2025 15:45
-
-
Save milnak/812c7b2ac2ac584fb3aa5f438003514a to your computer and use it in GitHub Desktop.
Extract MuseScore score and parts to PDF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Convert MuseScore file to PDF. Files are output to current folder. | |
.EXAMPLE | |
mkdir PDF; cd PDF | |
Get-ChildItem -File -LiteralPath '..' -Filter '*.mscz' | ForEach-Object { ConvertFrom-MuseScore -Extract Parts -File $_.FullName } | |
#> | |
function ConvertFrom-MuseScore { | |
param( | |
# Path to .mscz file | |
[Parameter(Mandatory)][string]$File, | |
# What to extract | |
[ValidateSet('Score', 'ScoreAudio', 'Parts', IgnoreCase)] | |
[string[]]$Extract = @('Score', 'Parts'), | |
# Path to MuseScore. Defaults to standard install location. | |
[string]$MuseScorePath = (Join-Path "$env:ProgramFiles" 'MuseScore 4\bin\MuseScore4.exe') | |
) | |
# $out_leafbase = Split-Path $File -LeafBase | |
# $musescore_args = ` | |
# '--export-to', ('"{0}.pdf"' -f $out_leafbase), ` | |
# '--export-score-parts', | |
# ('"{0}"' -f $File) | |
# Start-Process -FilePath ('"{0}"' -f $MuseScorePath) -ArgumentList $musescore_args -NoNewWindow -Wait | |
$path = Resolve-Path -LiteralPath $File | |
Write-Host ('Processing "{0}"...' -f (Split-Path -Path $path -Leaf)) | |
Write-Host '* Extracting JSON' | |
$musescoreJsonFile = 'musescore-score-parts.json' | |
Start-Process -Wait -NoNewWindow ` | |
-WorkingDirectory (Get-Location).Path ` | |
-FilePath """$MuseScorePath""" ` | |
-ArgumentList '--score-parts-pdf', """$path""", '--export-to', """$musescoreJsonFile""" | |
$musescoreJson = Get-Content $musescoreJsonFile | ConvertFrom-Json | |
$name = Split-Path -LeafBase $musescoreJson.score | |
if ('Score' -in $Extract) { | |
Write-Host "* Extracting score ""$name""" | |
$filename = $name + ' [Score].pdf' # $musescoreJson.scoreFullPostfix | |
Set-Content -LiteralPath $filename -AsByteStream -Value ([Convert]::FromBase64String($musescoreJson.scoreBin)) | |
} | |
if ('Parts' -in $Extract) { | |
for ($i = 0; $i -lt $musescoreJson.parts.Count; $i++) { | |
$partname = $musescoreJson.parts[$i] | |
Write-Host "* Extracting part ""$partname""" | |
$filename = '{0} [{1}].pdf' -f (Split-Path -LeafBase $name), $partname | |
Set-Content -LiteralPath $filename -AsByteStream -Value ([Convert]::FromBase64String($musescoreJson.partsBin[$i])) | |
} | |
} | |
Remove-Item -LiteralPath $musescoreJsonFile | |
if ('ScoreAudio' -in $Extract) { | |
$filename = $name + '.mp3' | |
Write-Host "* Extracting audio ""$filename""..." | |
Start-Process -Wait -NoNewWindow ` | |
-WorkingDirectory (Get-Location).Path ` | |
-FilePath """$MuseScorePath""" ` | |
-ArgumentList """$path""", '--export-to', """$filename""" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment