node-edge-tts

repository·master·Indexed 41 days ago

https://github.com/schneehertz/node-edge-tts

A Node.js module and CLI tool that leverages Microsoft Edge's online Text-to-Speech (TTS) service to convert text into audio files. Version 1.2.10 supports programmatic use via the EdgeTTS class and a command-line interface with options for voice, language, pitch, rate, and subtitle generation.

Tokens
2.1K
Snippets
11
Records
11
Agent score
95%

What's inside node-edge-tts

  1. Understand the subtitle JSON format

    master

    If saveSubtitles is enabled, the module generates a JSON file with the same name as the audio file. The JSON contains an array of objects representing word/part timing.

    Each object contains:

    • part: The text segment.
    • start: The start time in milliseconds.
    • end: The end time in milliseconds.
    [
      { "part": "node-edge-tts ", "start": 100, "end": 1287 },
      { "part": "is ", "start": 1287, "end": 1450 }
    ]
  2. Configure EdgeTTS options

    master

    When instantiating new EdgeTTS(), you can pass a configuration object to set default parameters for voice, language, format, and audio properties.

    Note: While many Azure Speech options are supported, not all are available due to limitations in the Microsoft Edge service. Supported values for voice, lang, and outputFormat can be referenced in Microsoft's documentation.

    const tts = new EdgeTTS({
      voice: 'en-US-AriaNeural',
      lang: 'en-US',
      outputFormat: 'audio-24khz-96kbitrate-mono-mp3',
      saveSubtitles: true,
      proxy: 'http://localhost:7890',
      pitch: '-10%',
      rate: '+10%',
      volume: '-50%',
      timeout: 10000
    })
  3. Use EdgeTTS in a Node.js module

    master

    Import the EdgeTTS class to programmatically convert text to speech. Use the ttsPromise method to perform the conversion and save it to a file.

    const { EdgeTTS } = require('node-edge-tts')
    // or
    import { EdgeTTS } from 'node-edge-tts'
    
    const tts = new EdgeTTS()
    await tts.ttsPromise('Hello world', 'path_to_audiofile_with_extension')
  4. Configure the EdgeTTS class

    master

    The EdgeTTS class can be instantiated with an optional configuration object to set default parameters for all subsequent text-to-speech requests. If no configuration is provided, it uses default values for voice, language, and audio format.

    Available configuration keys:

    • voice: The name of the voice to use (default: 'zh-CN-XiaoyiNeural').
    • lang: The language code (default: 'zh-CN').
    • outputFormat: The audio output format (default: 'audio-24khz-48kbitrate-mono-mp3').
    • saveSubtitles: Boolean indicating whether to save subtitle metadata to a .json file (default: false).
    • proxy: A proxy URL string for network requests.
    • rate: Prosody rate (default: 'default').
    • pitch: Prosody pitch (default: 'default').
    • volume: Prosody volume (default: 'default').
    • timeout: Request timeout in milliseconds (default: 10000).
    import { EdgeTTS } from 'node-edge-tts';
    
    const tts = new EdgeTTS({
      voice: 'en-US-GuyNeural',
      lang: 'en-US',
      saveSubtitles: true,
      rate: '+10%',
      timeout: 15000
    });
  5. Reference: node-edge-tts CLI options

    master

    The following options are available when using the CLI:

    FlagOptionDescription
    -t, --text(required)The text to be converted to speech
    -f, --filepath--filepathThe output file path (default: ./output.mp3)
    -v, --voice--voiceThe voice to be used (default: zh-CN-XiaoyiNeural)
    -l, --lang--langThe language to be used (default: zh-CN)
    -o, --outputFormat--outputFormatThe output format (default: audio-24khz-48kbitrate-mono-mp3)
    --pitch--pitchThe pitch of the voice (default: default)
    -r, --rate--rateThe rate of the voice (default: default)
    --volume--volumeThe volume of the voice (default: default)
    -s, --saveSubtitles--saveSubtitlesWhether to save subtitles (default: false)
    -p, --proxy--proxyProxy URL (e.g., http://localhost:7890)
    --timeout--timeoutThe timeout of the request in ms (default: 10000)
    npx node-edge-tts -t 'Hello world' -f './output.mp3'
  6. Convert text to speech with ttsPromise()

    master

    The ttsPromise(text, audioPath) method is the primary way to perform text-to-speech conversion. It connects to the Edge TTS service via WebSocket, streams the generated audio to the specified file path, and resolves when the process is complete.

    If saveSubtitles was enabled in the constructor, a .json file containing word-level timing metadata will be created at the same location as the audioPath (e.g., output.mp3.json).

    import { EdgeTTS } from 'node-edge-tts';
    
    const tts = new EdgeTTS();
    
    try {
      await tts.ttsPromise('Hello world', 'output.mp3');
      console.log('Audio saved successfully');
    } catch (err) {
      console.error('TTS failed:', err);
    }
  7. Subtitle data format

    master

    When saveSubtitles is set to true, the library generates a JSON file named {audioPath}.json. Each entry in the array represents a segment of text with timing information:

    • part: The string segment of the text.
    • start: The start time in deciseconds (1/10th of a second).
    • end: The end time in deciseconds.
    [
      {
        "part": "Hello",
        "start": 0,
        "end": 500
      },
      {
        "part": " world",
        "start": 500,
        "end": 1200
      }
    ]
  8. Reference CLI options for node-edge-tts

    master

    The following options are available when using the node-edge-tts command line interface:

    -t, --text <string>       (Required) The text to be converted to speech
    -f, --filepath <string>    The output file path (default: `./output.mp3`)
    -v, --voice <string>      The voice to be used (default: `zh-CN-XiaoyiNeural`)
    -l, --lang <string>        The language to be used (default: `zh-CN`)
    -o, --outputFormat <string> The output format (default: `audio-24khz-48kbitrate-mono-mp3`)
        --pitch <string>       The pitch of the voice (default: `default`)
    -r, --rate <string>       The rate of the voice (default: `default`)
        --volume <string>     The volume of the voice (default: `default`)
    -s, --saveSubtitles       Whether to save subtitles (default: `false`)
    -p, --proxy <string>      Proxy URL (e.g., `http://localhost:7890`)
        --timeout <number>     The timeout of the request (default: `10000`)
  9. Use the node-edge-tts CLI

    master

    The node-edge-tts CLI allows you to convert text to speech directly from your terminal. You can run it using npx.

    Basic usage requires the --text (or -t) flag. By default, the output is saved to ./output.mp3 using the zh-CN-XiaoyiNeural voice.

    npx node-edge-tts -t 'Hello world' -f './output.mp3'