echogarden-project / echogarden

Easy-to-use speech toolset. Written in TypeScript. Includes tools for synthesis, recognition, alignment, speech translation, language detection, source separation and more.
GNU General Public License v3.0
190 stars 19 forks source link

Aligment: is that a method to generate srt file in JS code? #37

Closed yhown589 closed 8 months ago

yhown589 commented 8 months ago

I try align() method, it return a promise object, it only can output the json string in the terminal. I haven't found a feature that can generate srt file according to my alignmentOptions. Although I know that can manually generated srt file through fs module using json string, but this json string generated by align() is fixed, I want to generate srt file through the configuration I set in advance. does feature exist?

import * as alignment from 'echogarden'
import * as fs from "fs"

let audioPath = "E:\\Programming Tools\\1_News One.mp3"
let audioUint8Array = fs.readFileSync(audioPath)
let transcriptPath = "E:\\Programming Tools\\1_News One.txt"
let transcriptStr = fs.readFileSync(transcriptPath).toString()
let alignmentOptions = {
    "engine": "dtw",
    "overwrite": true,
    "subtitles": {
        "format": "srt",
        "mode": "line"
    },
}

alignment.align(audioUint8Array, transcriptStr, alignmentOptions).then(
    (result) => {
        console.log(result)
    }
)
rotemdan commented 8 months ago

There is a method to convert the returned timeline JSON object to subtitles. It produces a string output, which you can then write to a file. It supports both SRT and. Here's the part from the API documentation about it:

Subtitles

timelineToSubtitles(timeline, options)

Converts a timeline to subtitles.

Returns:

Subtitle file content, as a string.

yhown589 commented 8 months ago

thank u very much much, it works. But I have an other problem: If I want to achieve the same configuration as the command line in js code and I want such an option to be set when generating json, how should I write the js option configuration? subtitles.maxLineCount=1 --subtitles.mode="line"

rotemdan commented 8 months ago

The options object mentioned in the API documentation, can accept these options, and many more, like { maxLineCount: 1, mode: "line" }, etc.

Here is the TypeScript definition of the options object, directly from the source code (if you are using TypeScript in VSCode it could autocomplete these options for you while editing your code):

export type SubtitlesMode = 'line' | 'segment' | 'sentence' | 'word' | 'phone' | 'word+phone'

export interface SubtitlesConfig {
    format?: 'srt' | 'webvtt'
    language?: string
    mode?: SubtitlesMode

    maxLineCount?: number
    maxLineWidth?: number
    minWordsInLine?: number
    separatePhrases?: boolean
    maxAddedDuration?: number

    decimalSeparator?: ',' | '.'
    includeCueIndexes?: boolean
    includeHours?: boolean
    lineBreakString?: '\n' | '\r\n'

    originalText?: string
    totalDuration?: number
}
yhown589 commented 8 months ago

I looked through your source and found this: import { defaultSubtitlesBaseConfig } from "../subtitles/Subtitles.js"; and then, found the expectation of subtitles options I want thank u