Skip to content

Commit

Permalink
Added ffmpeg termination on cancel
Browse files Browse the repository at this point in the history
  • Loading branch information
ablomer committed Feb 3, 2025
1 parent f03bc21 commit 4a75a52
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 22 deletions.
46 changes: 28 additions & 18 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,15 @@ export default function App() {
const startTime = Date.now();

try {
// Initialize a new FFmpeg instance for this conversion
const newFfmpeg = new FFmpeg();
const baseURL = 'https://unpkg.com/@ffmpeg/[email protected]/dist/esm';
await newFfmpeg.load({
coreURL: await toBlobURL(`${baseURL}/ffmpeg-core.js`, 'text/javascript'),
wasmURL: await toBlobURL(`${baseURL}/ffmpeg-core.wasm`, 'application/wasm'),
});
setFFmpegInstance(newFfmpeg);

console.log(`Starting conversion of ${file.name} to ${targetFormat}`);
const blob = await convertMedia(file, targetFormat, setProgress, handleLog);

Expand Down Expand Up @@ -234,31 +243,32 @@ export default function App() {
? `Conversion failed: ${error.message}`
: 'Failed to convert file. Check console for details.',
color: 'red',
autoClose: false,
autoClose: 5000,
});
} finally {
setConverting(false);
setProgress(0);
}
};

const handleCancel = () => {
cancelConversion();

// Log cancellation
if (file && targetFormat) {
logConversionEvent('media_conversion_cancelled', {
from_format: file.name.split('.').pop()?.toLowerCase() || 'unknown',
to_format: targetFormat,
file_size: file.size
});
const handleCancel = async () => {
try {
await cancelConversion();

// Log cancellation
if (file && targetFormat) {
logConversionEvent('media_conversion_cancelled', {
from_format: file.name.split('.').pop()?.toLowerCase() || 'unknown',
to_format: targetFormat,
file_size: file.size
});
}
} catch (error) {
console.error('Error during cancellation:', error);
} finally {
setConverting(false);
setProgress(0);
}

notifications.show({
title: 'Cancelled',
message: 'Conversion cancelled. Please wait for cleanup to complete...',
color: 'yellow',
});
};

return (
Expand Down Expand Up @@ -365,7 +375,7 @@ export default function App() {
)}
</Stack>
</Paper>
<Text c="dimmed" size="xs" ta="center" ff="monospace" fw={500}>0.9.1 beta</Text>
<Text c="dimmed" size="xs" ta="center" ff="monospace" fw={500}>0.9.2 beta</Text>
</Stack>
</Container>
</MantineProvider>
Expand Down
23 changes: 19 additions & 4 deletions src/utils/mediaConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,19 @@ export function setFFmpegInstance(instance: FFmpeg) {
let isCancelled = false;

/**
* Cancel the current conversion process
* Cancel the current conversion process by terminating FFmpeg instance
*/
export function cancelConversion() {
export async function cancelConversion() {
isCancelled = true;
if (ffmpeg) {
// Terminate the current FFmpeg instance
try {
await ffmpeg.terminate();
} catch (error) {
console.error('Error terminating FFmpeg:', error);
}
ffmpeg = null;
}
}

/**
Expand Down Expand Up @@ -463,10 +472,16 @@ async function convertWithFFmpeg(

// Step 7) Run FFmpeg
onLog('Running FFmpeg with args: ' + ffmpegArgs.join(' '));
await ffmpeg.exec(ffmpegArgs);
try {
await ffmpeg.exec(ffmpegArgs);
} catch (error) {
if (isCancelled) {
throw new Error('Conversion cancelled');
}
throw error;
}

if (isCancelled) {
await cleanupTempFiles(ffmpeg, [ffmpegInputName, outputName], onLog);
throw new Error('Conversion cancelled');
}

Expand Down

0 comments on commit 4a75a52

Please sign in to comment.