mirror of
https://gitlab.rlp.net/proj-wise2526-video2document/video2document.git
synced 2026-06-15 18:01:52 +02:00
Implemented local transcription solution with starting scripts
This commit is contained in:
@@ -3,32 +3,36 @@ import ffmpeg from 'fluent-ffmpeg';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import cliProgress from 'cli-progress';
|
||||
import { fileURLToPath } from 'url';
|
||||
|
||||
// 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
|
||||
// Test command: npx ts-node ./extract.ts /path/to/video.mp4
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
// Ensure ffmpeg binary is available
|
||||
if (!ffmpegPath) {
|
||||
throw new Error('FFmpeg binary not found!');
|
||||
throw new Error('FFmpeg binary not found!');
|
||||
}
|
||||
ffmpeg.setFfmpegPath(ffmpegPath);
|
||||
|
||||
// Ensure an input video path is provided via CLI
|
||||
if (process.argv.length < 3) {
|
||||
console.error('Usage: ts-node ffmpegExtractor.ts <input-video-path>');
|
||||
process.exit(1);
|
||||
console.error('Usage: ts-node ./extract.ts <input-video-path>');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Prepare output directory (always relative to project root)
|
||||
const outputDir = path.join(process.cwd(), 'storage', 'audio');
|
||||
// Resolve __dirname equivalent in ESM
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
|
||||
// Prepare output directory (always storage/audio under project root)
|
||||
const outputDir = path.join(__dirname, '..', '..', 'storage', 'audio');
|
||||
if (!fs.existsSync(outputDir)) {
|
||||
fs.mkdirSync(outputDir, { recursive: true });
|
||||
fs.mkdirSync(outputDir, { recursive: true });
|
||||
}
|
||||
|
||||
// Derive input and output paths
|
||||
@@ -38,10 +42,10 @@ const outputAudioPath = path.join(outputDir, `${inputVideoName}.wav`);
|
||||
|
||||
// Initialize CLI progress bar
|
||||
const progressBar = new cliProgress.SingleBar({
|
||||
format: 'Processing |{bar}| {percentage}% | {timemark}',
|
||||
barCompleteChar: '\u2588',
|
||||
barIncompleteChar: '\u2591',
|
||||
hideCursor: true
|
||||
format: 'Processing |{bar}| {percentage}% | {timemark}',
|
||||
barCompleteChar: '\u2588',
|
||||
barIncompleteChar: '\u2591',
|
||||
hideCursor: true
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -50,34 +54,37 @@ const progressBar = new cliProgress.SingleBar({
|
||||
* - Shows CLI progress bar
|
||||
* - Handles errors gracefully (without errors)
|
||||
*/
|
||||
function extractAudioFromVideo(videoFilePath: string, outputAudioPath: string): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
ffmpeg(videoFilePath)
|
||||
.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); // Save output file
|
||||
});
|
||||
export function extractAudioFromVideo(videoFilePath: string): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
ffmpeg(videoFilePath)
|
||||
.outputFormat('wav')
|
||||
.audioCodec('pcm_s16le')
|
||||
.audioChannels(1)
|
||||
.audioFrequency(16000)
|
||||
.on('progress', (progress) => {
|
||||
if (!progressBar.isActive) progressBar.start(100, 0, { timemark: '00:00:00' });
|
||||
if (progress.percent) {
|
||||
progressBar.update(progress.percent, { timemark: progress.timemark });
|
||||
}
|
||||
})
|
||||
.on('end', () => {
|
||||
progressBar.update(100, { timemark: 'done' });
|
||||
progressBar.stop();
|
||||
console.log(`Extraction completed: ${outputAudioPath}`);
|
||||
resolve();
|
||||
})
|
||||
.on('error', (err) => {
|
||||
progressBar.stop();
|
||||
console.error(`failed_audio_extraction: ${err.message}`);
|
||||
reject(err);
|
||||
})
|
||||
.save(outputAudioPath);
|
||||
});
|
||||
}
|
||||
|
||||
// Run extraction
|
||||
extractAudioFromVideo(inputVideoPath, outputAudioPath)
|
||||
// Run extraction if executed directly from CLI
|
||||
if (import.meta.url === `file://${process.argv[1]}`) {
|
||||
extractAudioFromVideo(inputVideoPath)
|
||||
.then(() => console.log('Audio extraction successful.'))
|
||||
.catch((err) => console.error(err));
|
||||
.catch((err) => console.error(err));
|
||||
}
|
||||
Reference in New Issue
Block a user