Skip to content

Commit c9e7f54

Browse files
committed
add cmd flags
1 parent b75a017 commit c9e7f54

File tree

2 files changed

+59
-11
lines changed

2 files changed

+59
-11
lines changed

README.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,31 @@ The project is written in Go and executables are provided for Windows, MacOS, an
77
## Usage
88

99
1. Get an API key from [Deepgram](https://www.deepgram.com/) and add it to your environment variables as `DEEPGRAM_API_KEY`.
10-
2. Create a CSV file with the following columns:
10+
2. Create a CSV file with the following columns (use the provided `sample-scripts.csv` as a template)
1111
- `label`: The label for the script. This will be used as the file name.
1212
- `script`: The text to be converted to speech.
13-
3. Run the executable. It will try and read the csv locally from `./scripts.csv`. If it doesn't exist, it will ask for the path to the CSV file.
14-
4. The audio files will be saved in the `audio/` directory.
13+
3. Download the executable for your OS from the [releases](https://github.com/aashish-joshi/tts-bulk/releases) page.
14+
4. The tool will try and read the csv locally from `scripts.csv`. If it doesn't exist, it will ask for the path to the CSV file.
1515

16+
### Commandline flags
17+
18+
The following flags are supported at the moment.
19+
20+
- `-format`: The format of the audio file. Supported formats are `wav` and `mp3`. Default is `mp3`.
21+
- `-output`: The output directory where the audio files will be saved. Default is `audio/`.
22+
- `-csv`: The path to the CSV file. Default is `scripts.csv`.
23+
- `-output`: The output directory where the audio files will be saved. Default is `audio/`.
24+
25+
### Example
26+
27+
1. Generate mp3 files in the default location.
28+
29+
```bash
30+
./tts-bulk
31+
```
32+
33+
2. Generate wav files in a custom location.
34+
35+
```bash
36+
./tts-bulk -format=wav -output=/path/to/output
37+
```

tts-bulk.go

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package main
33
import (
44
"context"
55
"encoding/csv"
6+
"flag"
67
"fmt"
78
"os"
89
"path/filepath"
10+
"strings"
911
"sync"
1012
"time"
1113

@@ -24,6 +26,29 @@ func checkDeepgramKey() error {
2426

2527
func main() {
2628

29+
var dgContainer, dgEncoding string
30+
31+
// Define the commandline flags
32+
dgModelName := flag.String("model", "aura-asteria-en", "Deepgram model name. Defaults to aura-asteria-en")
33+
dgFileFormat := flag.String("format", "mp3", "File format for the generated audio files. Defaults to mp3")
34+
outputFolder := flag.String("output", "audio", "Output folder for the generated audio files.")
35+
csvLocation := flag.String("csv", "scripts.csv", "Location of the CSV file containing the scripts to convert to audio.")
36+
// parse the commandline flags
37+
flag.Parse()
38+
39+
// If the DG file format is neither wav nor mp3, raise error.
40+
if strings.ToLower(*dgFileFormat) != "wav" && strings.ToLower(*dgFileFormat) != "mp3" {
41+
fmt.Println("Invalid file format. Only wav and mp3 are supported.")
42+
return
43+
}
44+
45+
if strings.ToLower(*dgFileFormat) == "wav" {
46+
dgContainer = "wav"
47+
dgEncoding = "linear16"
48+
} else {
49+
dgContainer = ""
50+
dgEncoding = "mp3"
51+
}
2752
// First check if the Deepgram API key is set
2853
if dgErr := checkDeepgramKey(); dgErr != nil {
2954
fmt.Println(dgErr)
@@ -38,22 +63,23 @@ func main() {
3863

3964
// set the Transcription options
4065
options := &interfaces.SpeakOptions{
41-
Model: "aura-asteria-en",
66+
Model: strings.ToLower(*dgModelName),
67+
Container: dgContainer,
68+
Encoding: dgEncoding,
4269
}
4370

4471
// create a Deepgram client
4572
c := client.NewRESTWithDefaults()
4673
dg := api.New(c)
4774

4875
// Check if the file exists
49-
fileName := "scripts.csv"
50-
if _, err := os.Stat(fileName); os.IsNotExist(err) {
51-
fmt.Print("Enter the name of the CSV file: ")
52-
fmt.Scanln(&fileName)
76+
if _, err := os.Stat(*csvLocation); os.IsNotExist(err) {
77+
fmt.Print("The file does not exist. Please enter the correct file path.")
78+
return
5379
}
5480

5581
// Open the CSV file
56-
file, err := os.Open(fileName)
82+
file, err := os.Open(*csvLocation)
5783
if err != nil {
5884
fmt.Printf("Error opening CSV file: %s\n", err)
5985
return
@@ -71,7 +97,7 @@ func main() {
7197
}
7298

7399
// Create the "audio" directory if it doesn't exist
74-
audioDir := "audio"
100+
audioDir := strings.ToLower(*outputFolder)
75101
if _, err := os.Stat(audioDir); os.IsNotExist(err) {
76102
err := os.Mkdir(audioDir, 0755)
77103
if err != nil {
@@ -101,7 +127,7 @@ func main() {
101127
defer wg.Done()
102128

103129
// Perform TTS and save to disk
104-
audioPath := filepath.Join(audioDir, fmt.Sprintf("%s.mp3", label))
130+
audioPath := filepath.Join(audioDir, fmt.Sprintf("%s.%s", label, *dgFileFormat))
105131
err := generateTTSAndSave(ctx, dg, script, options, audioPath)
106132
if err != nil {
107133
fmt.Printf("Could not generate TTS for row %v - %v: %v\n", i, label, err)

0 commit comments

Comments
 (0)