Spanicin commited on
Commit
e703756
·
verified ·
1 Parent(s): 3fa99e3

Update app_parallel.py

Browse files
Files changed (1) hide show
  1. app_parallel.py +21 -15
app_parallel.py CHANGED
@@ -176,11 +176,11 @@ def custom_cleanup(temp_dir):
176
  import gc
177
  gc.collect()
178
 
179
- def get_audio_duration(audio_path):
180
- audio_clip = mp.AudioFileClip(audio_path)
181
- duration_in_seconds = audio_clip.duration
182
- audio_clip.close() # Don't forget to close the clip
183
- return duration_in_seconds
184
 
185
 
186
  def generate_audio(voice_cloning, voice_gender, text_prompt):
@@ -227,10 +227,10 @@ def generate_audio(voice_cloning, voice_gender, text_prompt):
227
  temp_file.write(chunk)
228
  driven_audio_path = temp_file.name
229
  print('driven_audio_path',driven_audio_path)
230
- audio_duration = get_audio_duration(driven_audio_path)
231
- print('Total Audio Duration in seconds',audio_duration)
232
 
233
- return driven_audio_path, audio_duration
234
 
235
  def run_preprocessing(args):
236
  global path_of_lm_croper, path_of_net_recon_model, dir_of_BFM_fitting
@@ -262,20 +262,26 @@ def openai_chat_avatar(text_prompt):
262
  )
263
  return response
264
 
265
- def split_audio(audio_path, chunk_duration):
266
  audio_clip = mp.AudioFileClip(audio_path)
267
  total_duration = audio_clip.duration
268
-
 
 
269
  audio_chunks = []
270
- for start_time in range(0, int(total_duration), chunk_duration):
 
271
  end_time = min(start_time + chunk_duration, total_duration)
272
  chunk = audio_clip.subclip(start_time, end_time)
 
 
273
  with tempfile.NamedTemporaryFile(suffix=f"_chunk_{start_time}-{end_time}.wav", prefix="audio_chunk_", dir=TEMP_DIR.name, delete=False) as temp_file:
274
  chunk_path = temp_file.name
275
- chunk.write_audiofile(chunk_path)
276
  audio_chunks.append((start_time, chunk_path))
277
 
278
- return audio_chunks
 
279
 
280
  # Generator function to yield chunk results as they are processed
281
  def generate_chunks(audio_chunks, preprocessed_data, args):
@@ -344,7 +350,7 @@ def parallel_processing():
344
  source_image_path = save_uploaded_file(source_image, 'source_image.png',TEMP_DIR)
345
  print(source_image_path)
346
 
347
- driven_audio_path, audio_duration = generate_audio(voice_cloning, voice_gender, text_prompt)
348
 
349
  save_dir = tempfile.mkdtemp(dir=TEMP_DIR.name)
350
  result_folder = os.path.join(save_dir, "results")
@@ -355,7 +361,7 @@ def parallel_processing():
355
  preprocessed_data = run_preprocessing(args)
356
  chunk_duration = 3
357
  print(f"Splitting the audio into {chunk_duration}-second chunks...")
358
- audio_chunks = split_audio(driven_audio_path, chunk_duration=chunk_duration)
359
  print(f"Audio has been split into {len(audio_chunks)} chunks: {audio_chunks}")
360
 
361
  os.makedirs('lives', exist_ok=True)
 
176
  import gc
177
  gc.collect()
178
 
179
+ # def get_audio_duration(audio_path):
180
+ # audio_clip = mp.AudioFileClip(audio_path)
181
+ # duration_in_seconds = audio_clip.duration
182
+ # audio_clip.close() # Don't forget to close the clip
183
+ # return duration_in_seconds
184
 
185
 
186
  def generate_audio(voice_cloning, voice_gender, text_prompt):
 
227
  temp_file.write(chunk)
228
  driven_audio_path = temp_file.name
229
  print('driven_audio_path',driven_audio_path)
230
+ # audio_duration = get_audio_duration(driven_audio_path)
231
+ # print('Total Audio Duration in seconds',audio_duration)
232
 
233
+ return driven_audio_path
234
 
235
  def run_preprocessing(args):
236
  global path_of_lm_croper, path_of_net_recon_model, dir_of_BFM_fitting
 
262
  )
263
  return response
264
 
265
+ def split_audio(audio_path, TEMP_DIR, chunk_duration):
266
  audio_clip = mp.AudioFileClip(audio_path)
267
  total_duration = audio_clip.duration
268
+ print("split_audio duration:",total_duration)
269
+ number_of_chunks = math.ceil(total_duration / chunk_duration)
270
+ print("Number of audio chunks:",number_of_chunks)
271
  audio_chunks = []
272
+ for i in range(number_of_chunks):
273
+ start_time = i * chunk_duration
274
  end_time = min(start_time + chunk_duration, total_duration)
275
  chunk = audio_clip.subclip(start_time, end_time)
276
+
277
+ # Create a temporary file for the chunk
278
  with tempfile.NamedTemporaryFile(suffix=f"_chunk_{start_time}-{end_time}.wav", prefix="audio_chunk_", dir=TEMP_DIR.name, delete=False) as temp_file:
279
  chunk_path = temp_file.name
280
+ chunk.write_audiofile(chunk_path) # Specify codec if needed
281
  audio_chunks.append((start_time, chunk_path))
282
 
283
+ audio_clip.close() # Close the audio clip to release resources
284
+ return audio_chunks, total_duration
285
 
286
  # Generator function to yield chunk results as they are processed
287
  def generate_chunks(audio_chunks, preprocessed_data, args):
 
350
  source_image_path = save_uploaded_file(source_image, 'source_image.png',TEMP_DIR)
351
  print(source_image_path)
352
 
353
+ driven_audio_path = generate_audio(voice_cloning, voice_gender, text_prompt)
354
 
355
  save_dir = tempfile.mkdtemp(dir=TEMP_DIR.name)
356
  result_folder = os.path.join(save_dir, "results")
 
361
  preprocessed_data = run_preprocessing(args)
362
  chunk_duration = 3
363
  print(f"Splitting the audio into {chunk_duration}-second chunks...")
364
+ audio_chunks, audio_duration = split_audio(driven_audio_path, TEMP_DIR, chunk_duration=chunk_duration)
365
  print(f"Audio has been split into {len(audio_chunks)} chunks: {audio_chunks}")
366
 
367
  os.makedirs('lives', exist_ok=True)