-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_quote_audio.py
39 lines (34 loc) · 1.4 KB
/
generate_quote_audio.py
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
import csv
from pathlib import Path
from gtts import gTTS
from pydub import AudioSegment
from io import BytesIO
filename = input("Enter data filename (default: contacts.csv): ") or "contacts.csv"
with open("data/{}".format(filename), encoding="utf-8") as scheduleFile:
graduates = [
{k: v for k, v in row.items()}
for row in csv.DictReader(scheduleFile, skipinitialspace=True)
]
audioFileNameToQuote = {}
audioFileNameToName = {}
for grad in graduates:
audioFileNameToQuote[grad["Audio File Name"]] = grad["Senior quote?"]
audioFileNameToName[grad["Audio File Name"]] = grad["Your Full Name"]
files = list(Path("audio").glob("*.mp3"))
for file in files:
audioFileName = file.stem
quote = audioFileNameToQuote[audioFileName]
if quote != "":
speech = gTTS(text=quote, lang="en")
speech.save("audio/{} tts.mp3".format(audioFileName))
nameAudio = AudioSegment.from_mp3(file)
ttsAudio = AudioSegment.from_mp3("audio/{} tts.mp3".format(audioFileName))
combinedAudio = nameAudio + ttsAudio
finalAudio = combinedAudio[: min(29500, len(combinedAudio))]
finalAudio.export("audio/{}.mp3".format(audioFileName), format="mp3")
Path("audio/{} tts.mp3".format(audioFileName)).unlink()
print(
"Generated for {} ({})".format(
audioFileNameToName[audioFileName], audioFileName
)
)