Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

Cancel a Suno import's audio extraction when the import is cancelled

Claude4d19ccd748ff
diff --git a/server/services/trackSunoImport.js b/server/services/trackSunoImport.js
index b994575e89..e4eb7843ee 100644
--- a/server/services/trackSunoImport.js
+++ b/server/services/trackSunoImport.js
@@ -57,12 +57,12 @@ async function fetchSongAudio(urls) {
}
// Copy the video's audio track out (Suno's is AAC); transcode only if a copy fails.
-async function extractAudio(videoPath, outPath) {
+async function extractAudio(videoPath, outPath, signal) {
const bin = await findFfmpeg();
if (!bin) throw new Error('Suno only offers this song as a video, and ffmpeg is needed to take its audio out');
for (const codec of [['-c:a', 'copy'], ['-c:a', 'aac', '-b:a', '256k']]) {
- const res = await runFfmpegProcess({ bin, args: ['-y', '-i', videoPath, '-vn', '-map', '0:a:0', ...codec, outPath] });
- if (res.ok) return;
+ const res = await runFfmpegProcess({ bin, signal, args: ['-y', '-i', videoPath, '-vn', '-map', '0:a:0', ...codec, outPath] });
+ if (res.ok || signal?.aborted) return;
console.warn(`⚠️ Suno audio extract (${codec[1]}) failed: ${res.reason}`);
}
throw new Error('Could not take the audio out of the video Suno offers for this song');
@@ -80,6 +80,7 @@ export function cancelSunoImport(jobId) {
const job = importJobs.get(jobId);
if (!job || job.canceled || job.status !== 'running') return false;
job.canceled = true;
+ job.abort.abort(); // stops an audio extraction in flight
return true;
}
@@ -103,7 +104,7 @@ export async function startSunoImport(url) {
if (!isSunoSongUrl(url)) throw new ServerError(SUNO_URL_INVALID_MESSAGE, { status: 400, code: 'SUNO_URL_INVALID' });
const jobId = randomUUID();
- const job = { id: jobId, status: 'running', clients: [], canceled: false };
+ const job = { id: jobId, status: 'running', clients: [], canceled: false, abort: new AbortController() };
importJobs.set(jobId, job);
console.log(`🎶 Suno import ${shortId(jobId)} — ${url}`);
@@ -147,7 +148,7 @@ export async function startSunoImport(url) {
const videoPath = join(dir, 'song.mp4');
await writeFile(videoPath, video.buffer);
tempPath = join(dir, 'song.m4a');
- await extractAudio(videoPath, tempPath);
+ await extractAudio(videoPath, tempPath, job.abort.signal);
if (abortIfCanceled()) return;
}
diff --git a/server/services/trackSunoImport.test.js b/server/services/trackSunoImport.test.js
index 2dcad70af6..05a9d42c09 100644
--- a/server/services/trackSunoImport.test.js
+++ b/server/services/trackSunoImport.test.js
@@ -106,7 +106,7 @@ describe('startSunoImport', () => {
expect(fetchPublicBinary.mock.calls.map(([url]) => url)).toEqual([
`https://cdn1.suno.ai/${ID}.mp3`, `https://cdn1.suno.ai/${ID}.mp4`,
]);
- expect(runFfmpegProcess).toHaveBeenCalledWith({ bin: '/usr/local/bin/ffmpeg', args: expect.arrayContaining(['-vn', '-c:a', 'copy']) });
+ expect(runFfmpegProcess).toHaveBeenCalledWith({ bin: '/usr/local/bin/ffmpeg', signal: expect.any(AbortSignal), args: expect.arrayContaining(['-vn', '-c:a', 'copy']) });
expect(importUploadedTrack).toHaveBeenCalledWith(expect.stringMatching(/song\.m4a$/), 'Airplane Mode.m4a');
expect(createTrack).toHaveBeenCalledWith(expect.objectContaining({ title: 'Airplane Mode', lyrics: '[Verse]\nno signal' }));
});
@@ -127,6 +127,20 @@ describe('startSunoImport', () => {
expect(createTrack).not.toHaveBeenCalled();
});
+ it('stops the audio extraction when cancelled during it', async () => {
+ fetchPublicText.mockResolvedValue(null);
+ fetchPublicBinary.mockImplementation(async (url) => (url.endsWith('.mp4') ? { buffer: Buffer.from('v'), contentType: 'video/mp4' } : null));
+ runFfmpegProcess.mockImplementation(({ signal }) => new Promise((resolve) => {
+ signal.addEventListener('abort', () => resolve({ ok: false, reason: 'cancelled (SIGTERM)' }));
+ }));
+ const { jobId } = await startSunoImport(`https://suno.com/song/${ID}`);
+ await vi.waitFor(() => expect(runFfmpegProcess).toHaveBeenCalled());
+ expect(cancelSunoImport(jobId)).toBe(true);
+ expect(await terminal()).toEqual({ type: 'canceled' });
+ expect(runFfmpegProcess).toHaveBeenCalledTimes(1); // no transcode retry after a cancel
+ expect(importUploadedTrack).not.toHaveBeenCalled();
+ });
+
it('ends as cancelled, creating no track, when cancelled mid-download', async () => {
fetchPublicText.mockResolvedValue(null);
let release;