Repository navigation
Expand file tree
/
Copy pathgetdialogsamples.py
More file actions
115 lines (96 loc) · 3.84 KB
/
Copy pathgetdialogsamples.py
File metadata and controls
115 lines (96 loc) · 3.84 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import requests
import os
from dotenv import load_dotenv
import json
import shutil
from mutagen.mp3 import MP3
import subprocess
from pydub import AudioSegment
load_dotenv()
ELEVENLABS_API_KEY=str(os.getenv("ELEVENLABS_API_KEY"))
CHUNK_SIZE = 1024
PAUSE_BETWEEN_SEGMENTS=0.3
dialogsegmentid=0
dialogdetails =[]
def generate_sample(name, dialog):
global dialogsegmentid
global identifier
global dialogdetails
url=""
rachelurl = "https://api.elevenlabs.io/v1/text-to-speech/21m00Tcm4TlvDq8ikWAM"
billurl = "https://api.elevenlabs.io/v1/text-to-speech/pqHfZKP75CvOlQylNhV4"
if 'Eliza' in name:
print("Eliza says " + dialog)
url=rachelurl
elif 'Alan' in name:
print("Alan says " + dialog)
url=billurl
else:
return
headers = {
"Accept": "audio/mpeg",
"Content-Type": "application/json",
"xi-api-key": ELEVENLABS_API_KEY
}
outputfilename="./draft/"+identifier+"/"+str(dialogsegmentid)+"_"+name+".mp3"
data = {
"text": dialog,
"model_id": "eleven_multilingual_v2",
"voice_settings": {
"stability": .5,
"similarity_boost": .75,
"use_speaker_boost": True
}
}
response = requests.post(url, json=data, headers=headers)
with open(outputfilename, 'wb') as f:
for chunk in response.iter_content(chunk_size=CHUNK_SIZE):
if chunk:
f.write(chunk)
print(outputfilename)
wavfilename = outputfilename.replace('.mp3','.wav')
subprocess.run(["ffmpeg","-i",outputfilename, wavfilename])
sample = MP3(outputfilename)
print(sample.info.length)
dialogsampledetails = {
"dialogsegmentid": dialogsegmentid,
"name": name,
"length": sample.info.length,
"filename": wavfilename
}
dialogdetails.append(dialogsampledetails)
dialogsegmentid=dialogsegmentid+1
def combine_samples():
if len(dialogdetails)<2:
print("Too few samples")
exit()
path="./draft/"+identifier+"/"
for index, dialogsampledetails in enumerate(dialogdetails):
if (index==0):
print("Creating base snippet 0 and 1")
shutil.copy(dialogsampledetails['filename'],path+dialogsampledetails['name']+"_dialog.wav")
#######LOOK AT THIS LINE TO PREPAD ALAN SAMPLE
subprocess.run(["ffmpeg","-i",dialogdetails[1]['filename'],'-af','areverse,apad=pad_dur='+str(dialogdetails[0]['length']+PAUSE_BETWEEN_SEGMENTS)+',areverse',path+dialogdetails[1]['name']+"_dialog.wav"])
if (index>=2):
print("Merging snippet "+str(index))
#Weird thing #1 - ffmpeg can't merge WAV files without converting them first?
outputsound = AudioSegment.from_wav(path+dialogsampledetails['name']+"_dialog.wav") + AudioSegment.from_wav(dialogsampledetails['filename'])
outputsound.export(path+dialogsampledetails['name']+'_dialog_temp.wav', format="wav")
shutil.copy(path+dialogsampledetails['name']+'_dialog_temp.wav',path+dialogsampledetails['name']+'_dialog.wav')
os.remove(path+dialogsampledetails['name']+'_dialog_temp.wav')
if index+1<len(dialogdetails):
print("Adding silence to snippet "+str(index+1))
subprocess.run(["ffmpeg","-i",path+dialogsampledetails['name']+'_dialog.wav','-af','apad=pad_dur='+str(dialogdetails[index+1]['length']+PAUSE_BETWEEN_SEGMENTS),path+dialogsampledetails['name']+'_dialog_temp.wav'])
shutil.copy(path+dialogsampledetails['name']+'_dialog_temp.wav',path+dialogsampledetails['name']+'_dialog.wav')
os.remove(path+dialogsampledetails['name']+'_dialog_temp.wav')
articlesettingsfile = open("./currentarticle/prompt.json")
articlesettings = json.load(articlesettingsfile)
identifier=articlesettings['identifier']
articlesettingsfile.close()
dialogfile = open("./draft/"+identifier+"/"+identifier+".json")
dialog = json.load(dialogfile)
dialogfile.close()
for dialogsegment in dialog['dialog']:
for name in dialogsegment:
generate_sample(name, dialogsegment[name])
combine_samples()