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. An mscz-file is a compressed XML file, but with a far richer set of style definitions than musicxml, BTW. When you have a have a couple hundred of these files that need to be converted, doing this manually on a one-by-one basis becomes tedious. Luckily, It is possible to batch process many files by using a JSON conversion jobfile that lists all 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
The content of the JSON jobfile is very simple:
JSON Jobfile Format
Each file needs to be listed as a data tuple in the JSON jobfile, indicating the input and the output file names. 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}]
Each line will convert the indicated file from musicxml format into a Musecore format file that will have the mscz-extension. MuseScore uses the file extension to figure out the formats of each incoming and outgoing file, without you having to specify it sperately.
The usual formatting requirements of JSON apply here too: the JSON file needs to begin with a "[" and end with a "]".
How to quickly create a JSON job file
Let's say you have a directory full of musicxml files that need to be converted to MusceScore mscz files, then you can create the conversion-job.json jobfile 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 this line that contains the "[" character.
The next step is to list the musicxml files:
ls *.musicxml
file_1.musicxml
file_2.musicxml
etc.
Each line need to be converted into something like this: {"in":"file_1.musicxml","out":file_1.mscz},
You can see that the base filename (the file_1 bit) is used twice. To get to this result, the output of the file listing is piped in the sed stream editor, that operates on the input on a line-by-line basis. By using the \(.*\).musicxml regular expression, can identify the base filename (without the extension, so only the file_1, file_2,... bits) on each incoming line and to implicitly assign a placeholder to it - that is what the escaped parenthesis \(...\) are for. Placeholders start at 1, and are referred to with a leading backslash when reused, e.g. \1. In our case, we used the \1 placeholder to help make up the rest of the line, with all the other bits of required text:
ls *.musicxml | sed -e 's/\(.*\).musicxml/{"in":"\1.musicxml","out":"\1.mscz"},/'
This output is redirected to our target JSON jobfile, such that it is appended and will not destroy whatever already exists in the file. Therefore, we use a double ">>" to redirect 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
Ready to use
You should now have a JSON jobfile that MuseScore can process, be specifying th -j parameter:
mscore -j conversion-job.json
When things go wrong
If the musicxml files contain valid XML, MuseScore will quickly process all these files. There is progress output nor is there any 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:
ls -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.