Split any YouTube song into stems
TypeScript
feat: add compressed (AAC/.m4a) stem export
Anto Joel900cb3af7d25
diff --git a/src/main/index.ts b/src/main/index.tsindex 7a85de1..72fd03d 100644--- a/src/main/index.ts+++ b/src/main/index.ts@@ -1,6 +1,7 @@import { app, BrowserWindow, ipcMain, shell, dialog } from 'electron'import { join, normalize, extname } from 'path'import { existsSync, copyFileSync, mkdirSync, createReadStream, statSync } from 'fs'+import { spawn } from 'child_process'import { createServer, type Server } from 'http'import type { AddressInfo } from 'net'import type { AppSettings } from '../shared/types'@@ -80,6 +81,24 @@ function sanitizeName(name: string): string {return clean.length > 0 ? clean.slice(0, 120) : 'stems'}+function requireExportFfmpeg(): string {+ const ffmpeg = getStatus().ffmpeg.path+ if (!ffmpeg) {+ throw new Error('Something went wrong with the built-in audio tools. Try reinstalling StemKit.')+ }+ return ffmpeg+}++// the bundled ffmpeg is a minimal static build without libmp3lame, so mp3+// isn't an option — aac is a native encoder, no extra library needed+function transcodeToAac(ffmpeg: string, input: string, output: string): Promise<void> {+ return new Promise((resolve, reject) => {+ const child = spawn(ffmpeg, ['-y', '-i', input, '-c:a', 'aac', '-b:a', '256k', output])+ child.on('error', reject)+ child.on('close', (code) => (code === 0 ? resolve() : reject(new Error(`ffmpeg exited ${code}`))))+ })+}+async function createWindow(): Promise<void> {mainWindow = new BrowserWindow({width: 1360,@@ -194,13 +213,22 @@ app.whenReady().then(async () => {const song = loadSongs().find((s) => s.videoId === videoId)const file = join(stemsDir(videoId), `${stem}.wav`)if (!existsSync(file)) throw new Error(`Missing stem ${stem}`)+ const format = loadSettings().exportFormat+ const ext = format === 'aac' ? 'm4a' : 'wav'const result = await dialog.showSaveDialog({title: `Export ${stem}`,- defaultPath: join(app.getPath('downloads'), `${sanitizeName(song?.title ?? videoId)} - ${stem}.wav`),- filters: [{ name: 'WAV audio', extensions: ['wav'] }]+ defaultPath: join(+ app.getPath('downloads'),+ `${sanitizeName(song?.title ?? videoId)} - ${stem}.${ext}`+ ),+ filters: [{ name: format === 'aac' ? 'AAC audio' : 'WAV audio', extensions: [ext] }]})if (result.canceled || !result.filePath) return { saved: false }- copyFileSync(file, result.filePath)+ if (format === 'aac') {+ await transcodeToAac(requireExportFfmpeg(), file, result.filePath)+ } else {+ copyFileSync(file, result.filePath)+ }return { saved: true, path: result.filePath }})@@ -219,13 +247,21 @@ app.whenReady().then(async () => {if (result.canceled || !result.filePaths[0]) return { saved: false }const target = join(result.filePaths[0], sanitizeName(song?.title ?? videoId))mkdirSync(target, { recursive: true })+ const format = loadSettings().exportFormat+ const ext = format === 'aac' ? 'm4a' : 'wav'+ const ffmpeg = format === 'aac' ? requireExportFfmpeg() : nullfor (const name of list) {- copyFileSync(join(dir, `${name}.wav`), join(target, `${name}.wav`))+ const src = join(dir, `${name}.wav`)+ const dest = join(target, `${name}.${ext}`)+ if (ffmpeg) await transcodeToAac(ffmpeg, src, dest)+ else copyFileSync(src, dest)}let count = list.lengthconst mix = mixWavPath(videoId)if (existsSync(mix)) {- copyFileSync(mix, join(target, `${sanitizeName(song?.title ?? 'full track')}.wav`))+ const mixDest = join(target, `${sanitizeName(song?.title ?? 'full track')}.${ext}`)+ if (ffmpeg) await transcodeToAac(ffmpeg, mix, mixDest)+ else copyFileSync(mix, mixDest)count += 1}return { saved: true, path: target, count }diff --git a/src/main/settings.ts b/src/main/settings.tsindex 64b83a9..6ca8aee 100644--- a/src/main/settings.ts+++ b/src/main/settings.ts@@ -24,7 +24,8 @@ export function saveSettings(patch: Partial<AppSettings>): AppSettings {htdemucsFt: !!merged.htdemucsFt,roformerVocals: !!merged.roformerVocals,gpuSplit: !!merged.gpuSplit,- hideVideo: !!merged.hideVideo+ hideVideo: !!merged.hideVideo,+ exportFormat: merged.exportFormat === 'aac' ? 'aac' : 'wav'}writeFileSync(settingsFile(), JSON.stringify(next, null, 2))for (const win of BrowserWindow.getAllWindows()) {diff --git a/src/renderer/src/components/Settings.tsx b/src/renderer/src/components/Settings.tsxindex 853a74b..8ccfb81 100644--- a/src/renderer/src/components/Settings.tsx+++ b/src/renderer/src/components/Settings.tsx@@ -352,6 +352,37 @@ export function Settings({ settings, gpu, gpuVendor, onChange, onClose }: Props)/></div></section>++ <section className="pt-5 border-t border-white/[0.06] space-y-5">+ <SectionHeader label="Export" />+ <div className="flex items-start justify-between gap-4">+ <div className="min-w-0">+ <p className="text-[13px] font-medium">Export format</p>+ <p className="text-[11.5px] text-white/40 leading-relaxed mt-0.5">+ AAC (.m4a) is transcoded on export with the built-in ffmpeg — much smaller+ files, small quality loss.+ </p>+ </div>+ <div className="flex shrink-0 rounded-lg bg-white/[0.06] p-0.5 border border-white/[0.08]">+ {([+ { v: 'wav' as const, label: 'WAV' },+ { v: 'aac' as const, label: 'AAC' }+ ]).map((opt) => (+ <button+ key={opt.v}+ onClick={() => onChange({ exportFormat: opt.v })}+ className={`no-drag px-2.5 h-6 rounded-md text-[12px] font-semibold transition-colors ${+ settings.exportFormat === opt.v+ ? 'bg-white text-black'+ : 'text-white/45 hover:text-white/80'+ }`}+ >+ {opt.label}+ </button>+ ))}+ </div>+ </div>+ </section></div></div></div>diff --git a/src/renderer/src/components/StemLane.tsx b/src/renderer/src/components/StemLane.tsxindex c1d16d6..42374ba 100644--- a/src/renderer/src/components/StemLane.tsx+++ b/src/renderer/src/components/StemLane.tsx@@ -177,7 +177,7 @@ export function StemLane({{onExport && (<buttononClick={onExport}- title="Export stem as WAV"+ title="Export stem"className="no-drag w-6 h-6 rounded-md bg-white/5 hover:bg-white/10 text-white/40 hover:text-white flex items-center justify-center transition-colors shrink-0"><DownloadIcon className="w-3.5 h-3.5" />diff --git a/src/shared/types.ts b/src/shared/types.tsindex 961d728..f751ea7 100644--- a/src/shared/types.ts+++ b/src/shared/types.ts@@ -34,6 +34,10 @@ export interface AppSettings {// hide the YouTube video while playing: stems are always played locally,// this stops streaming the video and falls back to cached thumbnailshideVideo: boolean+ // format used when exporting stems: wav is the lossless source format,+ // aac (.m4a) is transcoded on export via the bundled ffmpeg. mp3 isn't an+ // option — the bundled ffmpeg is a minimal static build without libmp3lame+ exportFormat: 'wav' | 'aac'}export const DEFAULT_SETTINGS: AppSettings = {@@ -41,7 +45,8 @@ export const DEFAULT_SETTINGS: AppSettings = {htdemucsFt: false,roformerVocals: false,gpuSplit: false,- hideVideo: false+ hideVideo: false,+ exportFormat: 'wav'}export interface EngineStatus {