← Back

split an audio file into tracks with audacity

2026-01-05

Overview

This script helps you take one long recorded audio track in Audacity, and split and export it into multiple audio tracks.

This script assumes you have:

  • A long track in Audacity that represents multiple audio clips playing one after the other
  • A list of timestamps where you want to split the long audio track you have

Note that the list of timestamps needs to be in Audacity's .txt format. This script includes some really basic conversion code to transform a list of line-separated <minutes>:<seconds> clip lengths into the format Audacity needs.

It'd probably be better and more precise to use a time format with partial seconds, like the <seconds>.<microseconds> in Audacity's .txt format ... but for now I was going the manual route of typing in song lengths, so I didn't do that.

Process

Broadely, I'm following the process at https://support.audacityteam.org/audio-editing/splitting-a-recording-into-separate-tracks. Step by step...

  1. Record your big long audio track into Audacity. Save the file. Ideally this should be recorded from a lossless source, such as .flac.
  2. Write up your list of timestamps. Convert it into Audacity's .txt labels format. Save it as a .txt file. If you're having trouble with this step, try creating some labels manually in Audacity's GUI, and then exporting those and using the exported file as a guide for the expected format.
  3. In Audacity, choose the menu item File → Import → Labels...
  4. Choose your exported .txt file. This should successfully import all labels.
  5. Manually adjust your labels as needed, tweaking their position so it falls exactly where it needs to. Ideally this shouldn't be needed, but you'll probably want to double-check the split positions anyways to save yourself headaches down the road.
  6. Split the track at each label boundary. I think the quickest way to do this is 1) select the label spot, in the upper track that you're going to split, then 2) execute the split using the ⌘ + I shortcut.
  7. Export the track as multiple files. In Audacity, choose the menu item File → Export Audio, or shortcut ⌘ + Shift + E. In the lower part of the resulting dialog box, under Export Range, select Multiple Files, and Split files based on: Tracks. For naming, I recommend Using Label/Track Name, and setting your labels programmatically in the script you use to converting your list of timestamps to Audacity's .txt labels format.
  8. Future: use an exported .xspf playlist file in tandem with the numbered tracks to write music metadata from the .xspf file into the numbered tracks, matching track files to playlist files through alphabetical file name ordering and track sequence respectively.

Script to convert minutes-seconds clip lengths to Audacity labels

import fs from "fs";
import path from "path";
import os from "os";

// Example file string
const fileString = `1:30
2:55
3:33
2:55
3:33
2:55
3:33
2:55
3:33
2:55
3:33
2:55
`;
// Example reading from file, likely more useful in real world
// const filePath = "./scripts/audacity-generate-labels/clip-lengths.txt"
// const filePathFull = path.join(process.cwd(),filePath);
// const fileString = fs.readFileSync(filePathFull, "utf8");

main(fileString);

/**
 * TODO: write a description, for now I'm feeling lazy
 */
function main(fileString) {
	const lines = fileString.split("\n");
	const clipLengthsSeconds = [];
	for (const line of lines) {
		if (typeof line !== "string") continue;
		if (line.trim() === "") continue;
		const parts = line.split(":");
		if (parts.length < 2) continue;
		const [minutesStr, secondsStr] = line.split(":");
		const parsedParts = [minutesStr, secondsStr]
			.map((str) => {
				return parseInt(str);
			})
			.filter((e) => {
				return !isNaN(e);
			});
		if (parsedParts.length < 2) continue;
		const [minutes, seconds] = parsedParts;
		const secondsTotal = minutes * 60 + seconds;
		clipLengthsSeconds.push(secondsTotal);
	}
	console.log("");
	console.log("Clip lengths in seconds:");
	console.log(clipLengthsSeconds.map((n) => n.toFixed(6)).join("\n"));
	console.log("");
	const clipCount = clipLengthsSeconds.length;
	const clipCountExponent = Math.floor(Math.log10(Math.abs(clipCount)));
	const clipCountPadStart = clipCountExponent + 1;
	console.log({ clipCount, clipCountExponent });
	const audacityLabelLines = [];
	let clipsLengthRunningTotal = 0;
	for (let i = 0; i < clipCount; i++) {
		const clipLength = clipLengthsSeconds[i];
		const labelStart = clipsLengthRunningTotal;
		clipsLengthRunningTotal += clipLength;
		const labelEnd = clipsLengthRunningTotal;
		const audacityLabel = `${labelStart.toFixed(6)}\t${labelEnd.toFixed(
			6
		)}\tlabel${(i + 1).toFixed(0).padStart(clipCountPadStart, "0")}`;
		audacityLabelLines.push(audacityLabel);
	}

	const audacityLabelsFileString = audacityLabelLines.join("\n");
	const audacityLabelsFilePath = path.join(
		os.homedir(),
		"Desktop",
		"audacity-label-test.txt"
	);
	console.log("");
	console.log("Audacity Label Import Text:");
	console.log(audacityLabelsFileString);
	console.log("Writing out to:");
	console.log(audacityLabelsFilePath);
	console.log("");
	fs.writeFileSync(audacityLabelsFilePath, audacityLabelsFileString, "utf8");
}