-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolab_accent_converter.py
More file actions
307 lines (246 loc) · 11.4 KB
/
colab_accent_converter.py
File metadata and controls
307 lines (246 loc) · 11.4 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import streamlit as st
import requests
import tempfile
import os
import subprocess
import math
from concurrent.futures import ThreadPoolExecutor, as_completed
from pyngrok import ngrok
import uuid
import threading
import time
ngrok.set_auth_token("31EFmh5Zw2CX9GQ6yIsHwPVfkoT_2XfTF9Xq3pKzK7y3hUJio")
def process_audio_local(audio_file, gender):
unique_id = str(uuid.uuid4())[:8]
input_path = f"input_{unique_id}.wav"
output_path = f"voice_{unique_id}.wav"
with open(input_path, "wb") as f:
f.write(audio_file)
# Fixed: Added full paths to models and index files
if gender.lower() == "female":
model = "assets/weights/NikkiDorkDiaries.pth"
idx = "assets/weights/added_IVF110_Flat_nprobe_1_NikkiDorkDiaries_v2.index"
else:
model = "assets/weights/AndyField_350e_5950s.pth"
idx = "assets/weights/AndyField.index"
# Verify files exist before processing
if not os.path.exists(model):
raise Exception(f"Model file not found: {model}")
if not os.path.exists(idx):
raise Exception(f"Index file not found: {idx}")
try:
result = subprocess.run([
"python3", "tools/infer_cli.py",
"--input_path", input_path,
"--index_path", idx,
"--f0method", "harvest",
"--opt_path", output_path,
"--model_name", model,
"--index_rate", "0.7",
"--device", "cuda:0",
"--is_half", "False",
"--filter_radius", "1",
"--resample_sr", "44100",
"--rms_mix_rate", "0.5",
"--protect", "0.33",
"--f0up_key", "0"
], capture_output=True, text=True, check=True, timeout=300) # Added timeout
if os.path.exists(input_path):
os.remove(input_path)
if not os.path.exists(output_path):
raise Exception("Voice conversion failed - output not created")
with open(output_path, "rb") as f:
result_audio = f.read()
if os.path.exists(output_path):
os.remove(output_path)
return result_audio
except subprocess.CalledProcessError as e:
if os.path.exists(input_path):
os.remove(input_path)
if os.path.exists(output_path):
os.remove(output_path)
error_msg = f"RVC processing failed: {e.stderr if e.stderr else str(e)}"
raise Exception(error_msg)
except subprocess.TimeoutExpired as e:
if os.path.exists(input_path):
os.remove(input_path)
if os.path.exists(output_path):
os.remove(output_path)
raise Exception("Processing timeout - chunk too large")
except Exception as e:
if os.path.exists(input_path):
os.remove(input_path)
if os.path.exists(output_path):
os.remove(output_path)
raise Exception(f"Processing error: {str(e)}")
def download_video(url, output_path):
with requests.get(url, stream=True) as response:
response.raise_for_status()
total_size = int(response.headers.get("content-length", 0))
progress_bar = st.progress(0)
status_text = st.empty()
downloaded = 0
with open(output_path, "wb") as f:
for chunk in response.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
downloaded += len(chunk)
if total_size > 0:
progress = downloaded / total_size
progress_bar.progress(progress)
status_text.text(f"Downloaded: {downloaded / 1024 / 1024:.1f} MB")
return output_path
def extract_audio(video_path, audio_path):
subprocess.run([
"ffmpeg", "-i", video_path,
"-vn", "-acodec", "pcm_s16le",
"-ar", "44100", "-ac", "2",
audio_path, "-y"
], check=True, capture_output=True)
return audio_path
def get_audio_duration(audio_path):
result = subprocess.run([
"ffprobe", "-v", "quiet", "-show_entries",
"format=duration", "-of", "csv=p=0", audio_path
], capture_output=True, text=True, check=True)
return float(result.stdout.strip())
def split_audio_chunks(audio_path, chunk_duration=300):
duration = get_audio_duration(audio_path)
num_chunks = math.ceil(duration / chunk_duration)
temp_dir = tempfile.mkdtemp()
chunk_files = []
for i in range(num_chunks):
start_time = i * chunk_duration
remaining_duration = duration - start_time
current_chunk_duration = min(chunk_duration, remaining_duration)
chunk_file = os.path.join(temp_dir, f"chunk_{i:03d}.wav")
subprocess.run([
"ffmpeg", "-i", audio_path,
"-ss", str(start_time),
"-t", str(current_chunk_duration),
"-c", "copy", chunk_file, "-y"
], check=True, capture_output=True)
chunk_files.append(chunk_file)
return chunk_files
def combine_audio_chunks(processed_chunks, output_path):
if not processed_chunks:
return None
if len(processed_chunks) == 1:
subprocess.run([
"ffmpeg", "-i", processed_chunks[0],
"-c", "copy", output_path, "-y"
], check=True, capture_output=True)
else:
inputs = []
filter_parts = []
for i, chunk in enumerate(processed_chunks):
inputs.extend(["-i", chunk])
filter_parts.append(f"[{i}:0]")
filter_complex = "".join(filter_parts) + f"concat=n={len(processed_chunks)}:v=0:a=1[out]"
cmd = ["ffmpeg"] + inputs + [
"-filter_complex", filter_complex,
"-map", "[out]", output_path, "-y"
]
subprocess.run(cmd, check=True, capture_output=True)
return output_path
def replace_video_audio(video_path, audio_path, output_path):
subprocess.run([
"ffmpeg", "-i", video_path, "-i", audio_path,
"-c:v", "copy", "-c:a", "aac", "-map", "0:v:0",
"-map", "1:a:0", "-shortest", output_path, "-y"
], check=True, capture_output=True)
return output_path
def main():
st.set_page_config(page_title="Accent Changer")
st.title("American Accent Converter")
video_url = st.text_input("Enter download video url")
voice_gender = st.radio("Select Voice Gender", ("Male", "Female"), index=0)
if st.button("Submit"):
if not video_url.strip():
st.error("Please enter a valid video URL.")
else:
st.info(f"Selected voice: {voice_gender}")
try:
with st.spinner("Downloading video..."):
temp_dir = tempfile.mkdtemp()
video_path = os.path.join(temp_dir, "video.mp4")
download_video(video_url, video_path)
with st.spinner("Extracting audio..."):
audio_path = os.path.join(temp_dir, "audio.wav")
extract_audio(video_path, audio_path)
st.success("Audio extracted successfully!")
with st.spinner("Creating audio chunks..."):
chunk_files = split_audio_chunks(audio_path)
st.success(f"Created {len(chunk_files)} audio chunks")
for i, chunk_file in enumerate(chunk_files):
chunk_size = os.path.getsize(chunk_file) / 1024 / 1024
duration = get_audio_duration(chunk_file)
st.write(f"Chunk {i+1}: {duration:.1f}s ({chunk_size:.1f} MB)")
processed_chunks = {}
failed_chunks = [] # Track failed chunks
def process_chunk(i, chunk_file):
try:
with open(chunk_file, "rb") as f:
audio_data = f.read()
processed_audio = process_audio_local(audio_data, voice_gender)
processed_chunk = os.path.join(temp_dir, f"processed_chunk_{i:03d}.wav")
with open(processed_chunk, "wb") as out_f:
out_f.write(processed_audio)
return i, processed_chunk, None
except Exception as e:
return i, None, str(e)
with st.spinner("Processing audio chunks..."):
max_workers = 2 # Reduced from 4 to avoid overwhelming the system
with ThreadPoolExecutor(max_workers=max_workers) as executor:
futures = [executor.submit(process_chunk, i, chunk_file) for i, chunk_file in enumerate(chunk_files)]
for future in as_completed(futures):
i, processed_file, error = future.result()
if error:
st.error(f"Failed to process chunk {i+1}: {error}")
failed_chunks.append(i)
else:
processed_chunks[i] = processed_file
st.success(f"Chunk {i+1} processed successfully")
if len(processed_chunks) == len(chunk_files):
with st.spinner("Combining processed audio..."):
final_audio_path = os.path.join(temp_dir, "final_audio.wav")
ordered_chunks = [processed_chunks[i] for i in sorted(processed_chunks.keys())]
combine_audio_chunks(ordered_chunks, final_audio_path)
with st.spinner("Creating final video..."):
final_video_path = os.path.join(temp_dir, "final_video.mp4")
replace_video_audio(video_path, final_audio_path, final_video_path)
with open(final_video_path, "rb") as f:
st.download_button(
label="Download Converted Video",
data=f.read(),
file_name="accent_converted_video.mp4",
mime="video/mp4"
)
st.success("Video processing completed successfully!")
else:
st.error(f"Some chunks failed to process ({len(failed_chunks)} failures). Cannot create final video.")
st.write(f"Failed chunks: {[i+1 for i in failed_chunks]}")
except Exception as e:
st.error(f"Error: {str(e)}")
def start_streamlit_with_ngrok():
current_file = os.path.abspath(__file__)
subprocess.Popen([
"streamlit", "run", current_file,
"--server.headless=true",
"--server.port=8501"
])
time.sleep(10)
public_url = ngrok.connect(8501)
print(f"🎉 Streamlit Dashboard: {public_url}")
if __name__ == "__main__":
import sys
if len(sys.argv) > 1 and sys.argv[1] == "init":
print("🚀 Starting Accent Converter...")
start_streamlit_with_ngrok()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("Service stopped.")
else:
main()