-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenai_tts_from_file.py
More file actions
executable file
·39 lines (33 loc) · 1.12 KB
/
openai_tts_from_file.py
File metadata and controls
executable file
·39 lines (33 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env -S uv run
# /// script
# dependencies = [
# "openai>=1.0.0",
# ]
# requires-python = ">=3.11"
# ///
import sys
import os
from openai import OpenAI
def main():
if len(sys.argv) == 1:
sys.stderr.write("Usage: {} <file1> [<file2> ...]\n".format(sys.argv[0]))
sys.exit(1)
client = OpenAI()
for file_path in sys.argv[1:]:
if os.path.isfile(file_path) and os.access(file_path, os.R_OK):
sys.stderr.write("Processing file: {}\n".format(file_path))
with open(file_path, 'r') as file:
file_contents = file.read()
response = client.audio.speech.create(
model="tts-1",
voice="alloy",
input=file_contents,
)
base_name, _ = os.path.splitext(file_path)
output_file = base_name + ".mp3"
response.stream_to_file(output_file)
sys.stderr.write("Output saved to: {}\n".format(output_file))
else:
sys.stderr.write("Error: File {} is not readable or does not exist.\n".format(file_path))
if __name__ == "__main__":
main()