HOEKSTRA.CO.UK

MuseScore can import a variety of music files, and export them as a different type of file, e.g. you can manually load in a musicxml file and then export it as a MuseScore mscz file. When you have a have a couple of hundred of these files, then doing this manually becomes a tedious process. It is possible to batch process many files by using a single conversion job JSON file that contains the required conversions, and then invoking MuseScore with the -j parameter from the command line. The command is:

mscore -j conversion-job.json

Or, if you are running MuseScore as an AppImage:

/opt/MuseScore-Studio-4.7.0.260513133-x86_64.AppImage -j conversion-job.json 

JSON Jobfile Format

The format of the JSON job file is:

[
{"in":"file_1.musicxml","out":file_1.mscz},
{"in":"file_2.musicxml","out":file_2.mscz},
...
{"in":"file_n.musicxml","out":file_n.mscz}
]

This will convert all the listed files from musicxml format into a Musecore format file with the mscz-extension. An mscz-file is a compressed XML file, but with a far richer set of style definitions than musicxml

How to quickly create a JSON job file

Create the conversion-job.json JSON-file by listing the directory of muscixml files, manipulating the output a little using the sed stream editor, and redirecting it to the JSON file.

Start by creating the opening square bracket to initiate the JSON file, by writing this line to a new JSON file:

echo "[" > conversion-job.json

This echoes a "[" to standard output, but is then redirected to a file instead by using the ">" redirection operator. The single ">" redirection operator is used to create a new JSON file. Had the JSON file previously existed, it would have been completely overwritten with a line that contains the "[" character.

The next step is to list the musicxml files and pipe the filenames through the sed stream editor to to create the desired file conversion definition for each file. It does this by extracting the filename (without extension) and then reusing it (the \1 thingy) when making up the line. This is redirected - in the append mode, hence a double ">>" - to the JSON file, so as not to overwrite the work from the previous step:

ls *.musicxml | sed -e 's/\(.*\).musicxml/{"in":"\1.musicxml","out":"\1.mscz"},/' >> conversion-job.json 

The JSON format is not a forgiving data format and will not tolerate unnecessary trailing commas, so the trailing comma on the very last file item needs to be removed. To remove this last character from the last line from the JSON file, do this:

sed -i "$ 's/.$//'" conversion-job.json 

This makes sed go to the last line of the file (the first $ instructs sed to only operate on the last line), find the last character in the line (the /.$/ regular expression), and substitutes it (the s-command) with nothing (the //-bit). By specifying the -i parameter, sed performs this action in-sito on the actual file, rather than you having to pipe the file through sed to a temporary file and copying the resulting file back.

Finally, close the JSON file with a "]" character, by once again appending it, using the ">>" redirection operator, to the JSON file: 

$ echo "]" >> conversion-job.json 

When things go wrong

If the musicxml files contain valid XML, MuseScore will quickly process all these files. There is no error handling. The only way to determine where the process may have hanged is to list the output files (mscz files in this case) in chronological order:

lls -alrt *.mscz 

Determine where the process stopped by finding the next file what was due to be processed by looking in the conversion-job.json file, and correct the XML in the musicxml-file if it is faulty.