From 029f40057a72ef11e113dba0c94bc9747fc449d6 Mon Sep 17 00:00:00 2001 From: MikeHughes-BIN Date: Sun, 2 Nov 2025 17:46:36 +0100 Subject: [PATCH] Added some improved commentary --- .../modules/extraction/ffmpegExtractor.ts | 43 +++++++++++-------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/services/modules/extraction/ffmpegExtractor.ts b/services/modules/extraction/ffmpegExtractor.ts index 6b06bfd..27c280b 100644 --- a/services/modules/extraction/ffmpegExtractor.ts +++ b/services/modules/extraction/ffmpegExtractor.ts @@ -3,39 +3,40 @@ import ffmpeg from 'fluent-ffmpeg'; import path from 'path'; import fs from 'fs'; import cliProgress from 'cli-progress'; -// Source for the base code: https://docs.yemreak.com/arsiv/programming/extract-audio-from-video-with-typescript-and-ffmpeg -// Testfile command: npx ts-node /Users/mikehughes/PROJ/video2document/services/modules/extraction/ffmpegExtractor.ts /Users/mikehughes/Downloads/sweetHomeAlabama.mp4 + +// Base code reference: https://docs.yemreak.com/arsiv/programming/extract-audio-from-video-with-typescript-and-ffmpeg +// Test command: npx ts-node ffmpegExtractor.ts /path/to/video.mp4 + /** - * Extracts audio from a video file and saves it as an wav file. - * @param videoFilePath - The path to the video file. - * @param outputAudioPath - The path where the extracted audio file should be saved. + * Extracts audio from a video file and saves it as WAV. + * @param videoFilePath Path to the input video file. + * @param outputAudioPath Path where the output WAV audio will be saved. */ -// If the ffmpeg binary is not found, throw an error + +// Ensure ffmpeg binary is available if (!ffmpegPath) { throw new Error('FFmpeg binary not found!'); } ffmpeg.setFfmpegPath(ffmpegPath); -// If the input video path is not provided, exit the script +// Ensure an input video path is provided via CLI if (process.argv.length < 3) { console.error('Usage: ts-node ffmpegExtractor.ts '); process.exit(1); } -// Ensure the output directory exists (./storage/audio - always start command from project root) +// Prepare output directory (always relative to project root) const outputDir = path.join(process.cwd(), 'storage', 'audio'); if (!fs.existsSync(outputDir)) { fs.mkdirSync(outputDir, { recursive: true }); } -// Get input video path from command line arguments +// Derive input and output paths const inputVideoPath = process.argv[2]; -// Derive output audio path from input video path const inputVideoName = path.basename(inputVideoPath, path.extname(inputVideoPath)); -// Define output audio path const outputAudioPath = path.join(outputDir, `${inputVideoName}.wav`); -// Initialize progress bar +// Initialize CLI progress bar const progressBar = new cliProgress.SingleBar({ format: 'Processing |{bar}| {percentage}% | {timemark}', barCompleteChar: '\u2588', @@ -43,32 +44,40 @@ const progressBar = new cliProgress.SingleBar({ hideCursor: true }); -// Function to extract audio from video. Possible parameters are videoFilePath and outputAudioPath. Ouptput format is wav. +/** + * Extracts audio from a video using ffmpeg. + * - Converts video to WAV (16 kHz, Mono, PCM optional if needed) + * - Shows CLI progress bar + * - Handles errors gracefully (without errors) + */ function extractAudioFromVideo(videoFilePath: string, outputAudioPath: string): Promise { return new Promise((resolve, reject) => { ffmpeg(videoFilePath) - .outputFormat('wav') // Set the output format + .outputFormat('wav') // Set output format to WAV .on('progress', (progress) => { + // Start progress bar if not already active if (!progressBar.isActive) progressBar.start(100, 0, { timemark: '00:00:00' }); if (progress.percent) { progressBar.update(progress.percent, { timemark: progress.timemark }); } }) .on('end', () => { + // Finish progress bar progressBar.update(100, { timemark: 'done' }); progressBar.stop(); console.log(`Extraction completed: ${outputAudioPath}`); resolve(); }) .on('error', (err) => { + // Show extraction errors in a clear format console.error(`failed_audio_extraction: ${err.message}`); reject(err); }) - .save(outputAudioPath); // Specify the output file path + .save(outputAudioPath); // Save output file }); } -// Call the function to extract audio. +// Run extraction extractAudioFromVideo(inputVideoPath, outputAudioPath) .then(() => console.log('Audio extraction successful.')) - .catch((err) => console.error(err)); + .catch((err) => console.error(err)); \ No newline at end of file