Skip to content

Commit

Permalink
Simplify and improve placeholder path parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
rotemdan committed Dec 5, 2024
1 parent 8940a5f commit ec98d33
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 44 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "echogarden",
"version": "2.0.8",
"version": "2.0.9",
"description": "An easy-to-use speech toolset. Includes tools for synthesis, recognition, alignment, speech translation, language detection, source separation and more.",
"author": "Rotem Dan",
"license": "GPL-3.0",
Expand Down
62 changes: 21 additions & 41 deletions src/cli/CLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { removePackage } from '../utilities/PackageManager.js'
import { appName } from '../api/Common.js'
import { ServerOptions, startServer } from '../server/Server.js'
import { OpenPromise } from '../utilities/OpenPromise.js'
import { getLowercaseFileExtension, joinPath, parsePath, resolveToModuleRootDir } from '../utilities/PathUtilities.js'
import { getDirName, getFileNameWithoutExtension, getLowercaseFileExtension, joinPath, parsePath, resolveToModuleRootDir } from '../utilities/PathUtilities.js'
import { CLIOptions, CLIOptionsKeys } from './CLIOptions.js'
import { convertHtmlToText, formatIntegerWithLeadingZeros, formatListWithQuotedElements } from '../utilities/StringUtilities.js'

Expand Down Expand Up @@ -517,9 +517,7 @@ export async function speak(operationData: CLIOperationData) {
}

for (const outputFilename of outputFilenames) {
const placeholderPatternMatch = outputFilename.match(filenamePlaceholderPattern)

if (placeholderPatternMatch) {
if (isPlaceholderFilePath(outputFilename)) {
continue
}

Expand Down Expand Up @@ -562,9 +560,7 @@ export async function transcribe(operationData: CLIOperationData) {
}

for (const outputFilename of outputFilenames) {
const partPatternMatch = outputFilename.match(filenamePlaceholderPattern)

if (partPatternMatch) {
if (isPlaceholderFilePath(outputFilename)) {
continue
}

Expand Down Expand Up @@ -658,9 +654,7 @@ export async function align(operationData: CLIOperationData) {
}

for (const outputFilename of outputFilenames) {
const partPatternMatch = outputFilename.match(filenamePlaceholderPattern)

if (partPatternMatch) {
if (isPlaceholderFilePath(outputFilename)) {
continue
}

Expand Down Expand Up @@ -763,9 +757,7 @@ export async function alignTranslation(operationData: CLIOperationData) {
}

for (const outputFilename of outputFilenames) {
const partPatternMatch = outputFilename.match(filenamePlaceholderPattern)

if (partPatternMatch) {
if (isPlaceholderFilePath(outputFilename)) {
continue
}

Expand Down Expand Up @@ -892,9 +884,7 @@ export async function alignTranscriptAndTranslation(operationData: CLIOperationD
}

for (const outputFilename of outputFilenames) {
const partPatternMatch = outputFilename.match(filenamePlaceholderPattern)

if (partPatternMatch) {
if (isPlaceholderFilePath(outputFilename)) {
continue
}

Expand Down Expand Up @@ -994,9 +984,7 @@ export async function alignTimelineTranslation(operationData: CLIOperationData)
const allowOverwrite = getWithDefault(cliOptions.overwrite, overwriteByDefault)

for (const outputFilename of outputFilenames) {
const partPatternMatch = outputFilename.match(filenamePlaceholderPattern)

if (partPatternMatch) {
if (isPlaceholderFilePath(outputFilename)) {
continue
}

Expand Down Expand Up @@ -1071,9 +1059,7 @@ export async function translateText(operationData: CLIOperationData) {
logger.start('\nWrite output files')

for (const outputFilename of outputFilenames) {
const partPatternMatch = outputFilename.match(filenamePlaceholderPattern)

if (partPatternMatch) {
if (isPlaceholderFilePath(outputFilename)) {
continue
}

Expand Down Expand Up @@ -1134,9 +1120,7 @@ export async function translateSpeech(operationData: CLIOperationData) {
}

for (const outputFilename of outputFilenames) {
const partPatternMatch = outputFilename.match(filenamePlaceholderPattern)

if (partPatternMatch) {
if (isPlaceholderFilePath(outputFilename)) {
continue
}

Expand Down Expand Up @@ -1298,9 +1282,7 @@ export async function detectVoiceActivity(operationData: CLIOperationData) {
}

for (const outputFilename of outputFilenames) {
const partPatternMatch = outputFilename.match(filenamePlaceholderPattern)

if (partPatternMatch) {
if (isPlaceholderFilePath(outputFilename)) {
continue
}

Expand Down Expand Up @@ -1879,13 +1861,7 @@ async function checkOutputFilenames(outputFilenames: string[], acceptMediaOutput
throw new Error(errorText)
}

const placeholderPatternMatch = outputFilename.match(filenamePlaceholderPattern)

if (placeholderPatternMatch && placeholderPatternMatch[1] === 'segment') {
//if (placeholderPatternMatch[1] != 'segment') {
// throw new Error(`Invalid placeholder pattern: '${placeholderPatternMatch[1]}'. Placeholder output filename pattern currently only supports 'segment'. For example: '/out/[segment].wav'`)
//}

if (isPlaceholderFilePath(outputFilename)) {
includesPlaceholderPattern = true
}
}
Expand All @@ -1911,13 +1887,13 @@ async function writeOutputFilesForSegment(outputFilenames: string[], index: numb
}

for (const outputFilename of outputFilenames) {
const placeholderPatternMatch = outputFilename.match(filenamePlaceholderPattern)

if (!placeholderPatternMatch) {
if (!isPlaceholderFilePath(outputFilename)) {
continue
}

const segmentFilename = outputFilename.replace(filenamePlaceholderPattern, `${formatIntegerWithLeadingZeros(index + 1, digitCount)} - ${initialText}.$2`)
const fileDir = getDirName(outputFilename)
const fileExtension = getLowercaseFileExtension(outputFilename)
const segmentFilename = joinPath(fileDir, `${formatIntegerWithLeadingZeros(index + 1, digitCount)} - ${initialText}.${fileExtension}`)

const fileSaver = getFileSaver(segmentFilename, allowOverwrite)
await fileSaver(audio, timeline, text)
Expand Down Expand Up @@ -2016,12 +1992,16 @@ function getFileSaver(outputFilePath: string, allowOverwrite: boolean): FileSave
return fileSaver
}

function isPlaceholderFilePath(filePath: string) {
const filenameWithoutExtension = getFileNameWithoutExtension(filePath)

return filenameWithoutExtension === '[segment]'
}

const supportedMetadataFileExtensions = ['txt', 'json']
const supportedSubtitleFileExtensions = ['srt', 'vtt']
const supportedOutputMediaFileExtensions = ['wav', 'mp3', 'opus', 'm4a', 'ogg', 'flac']

const filenamePlaceholderPattern = /^\[([^\]]+)\]\.(.+)$/

const overwriteByDefault = false

startIfInWorkerThread()

0 comments on commit ec98d33

Please sign in to comment.