UNPKG

ffmpeg-toolkit

Version:

A modern FFmpeg toolkit for Node.js

65 lines 2.3 kB
import fs from 'fs/promises'; export class SubtitleModule { constructor(base) { this.base = base; } /** * Normalize path for FFmpeg */ normalizePath(path) { let normalized = path.replace(/\\/g, '/'); if (normalized.startsWith('/')) { normalized = normalized.substring(1); } normalized = normalized.replace(/:/g, '\\:').replace(/'/g, "'\\''"); return normalized; } /** * Check if directory exists and is accessible */ async checkDirectoryAccess(dir) { try { await fs.access(dir, fs.constants.R_OK); return true; } catch { return false; } } async burnSubtitles(options) { try { const { inputPath, pathOutput, subtitlesPath, fontDirectory } = options; if (!(await this.checkDirectoryAccess(inputPath))) { throw new Error(`Input video not accessible: ${inputPath}`); } if (!(await this.checkDirectoryAccess(subtitlesPath))) { throw new Error(`Subtitle file not accessible: ${subtitlesPath}`); } if (fontDirectory && !(await this.checkDirectoryAccess(fontDirectory))) { throw new Error(`Font directory not accessible: ${fontDirectory}`); } const normalizedSubtitlePath = this.normalizePath(subtitlesPath); let subtitleFilter = `subtitles='${normalizedSubtitlePath}'`; if (fontDirectory) { const normalizedFontDir = this.normalizePath(fontDirectory); subtitleFilter += `:fontsdir='${normalizedFontDir}'`; } const command = this.base.ffmpeg(inputPath); return this.base.process({ callback: () => command .videoFilters(subtitleFilter) .videoCodec('libx264') .audioCodec('copy') .output(pathOutput), data: { pathOutput, }, inputPath, }); } catch (error) { this.base.handleModuleError(error, 'Failed to burn subtitles'); } } } //# sourceMappingURL=subtitle.js.map