qqwjq1981 commited on
Commit
4291f64
·
verified ·
1 Parent(s): b0d613e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -4
app.py CHANGED
@@ -277,13 +277,13 @@ def process_entry(entry, i, video_width, video_height, add_voiceover, target_lan
277
  audio_segment = None
278
  if add_voiceover:
279
  segment_audio_path = f"segment_{i}_voiceover.wav"
280
- generate_voiceover_OpenAI([entry], target_language, segment_audio_path)
 
281
  audio_clip = AudioFileClip(segment_audio_path)
282
  # Get and log all methods in AudioFileClip
283
  logger.info("Methods in AudioFileClip:")
284
  for method in dir(audio_clip):
285
  logger.info(method)
286
- desired_duration = entry["end"] - entry["start"]
287
 
288
  # Log duration of the audio clip and the desired duration for debugging.
289
  logger.debug(f"Audio clip duration: {audio_clip.duration}, Desired duration: {desired_duration}")
@@ -362,7 +362,26 @@ def generate_voiceover(translated_json, language, output_audio_path):
362
  except Exception as e:
363
  raise ValueError(f"Error generating voiceover: {e}")
364
 
365
- def generate_voiceover_OpenAI(translated_json, language, output_audio_path):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
366
  """
367
  Generate voiceover from translated text for a given language using OpenAI TTS API.
368
  """
@@ -380,12 +399,13 @@ def generate_voiceover_OpenAI(translated_json, language, output_audio_path):
380
 
381
  while retry_count < max_retries:
382
  try:
 
383
  # Create the speech using OpenAI TTS API
384
  response = client.audio.speech.create(
385
  model=model,
386
  voice=voice,
387
  input=full_text,
388
- speed=1.1
389
  )
390
  # Save the audio to the specified path
391
  with open(output_audio_path, 'wb') as f:
 
277
  audio_segment = None
278
  if add_voiceover:
279
  segment_audio_path = f"segment_{i}_voiceover.wav"
280
+ desired_duration = entry["end"] - entry["start"]
281
+ generate_voiceover_OpenAI([entry], target_language, desired_duration, segment_audio_path)
282
  audio_clip = AudioFileClip(segment_audio_path)
283
  # Get and log all methods in AudioFileClip
284
  logger.info("Methods in AudioFileClip:")
285
  for method in dir(audio_clip):
286
  logger.info(method)
 
287
 
288
  # Log duration of the audio clip and the desired duration for debugging.
289
  logger.debug(f"Audio clip duration: {audio_clip.duration}, Desired duration: {desired_duration}")
 
362
  except Exception as e:
363
  raise ValueError(f"Error generating voiceover: {e}")
364
 
365
+ def truncated_linear(x):
366
+ if x < 15:
367
+ return 1
368
+ elif x > 25:
369
+ return 1.2
370
+ else:
371
+ slope = (1.2 - 1) / (25 - 15)
372
+ return 1 + slope * (x - 15)
373
+
374
+ def calculate_speed(text, desired_duration):
375
+ # Calculate characters per second
376
+ char_count = len(text)
377
+ chars_per_second = char_count / (desired_duration + 0.001)
378
+
379
+ # Apply truncated linear function to get speed
380
+ speed = truncated_linear(chars_per_second)
381
+
382
+ return speed
383
+
384
+ def generate_voiceover_OpenAI(translated_json, language, desired_duration, output_audio_path):
385
  """
386
  Generate voiceover from translated text for a given language using OpenAI TTS API.
387
  """
 
399
 
400
  while retry_count < max_retries:
401
  try:
402
+ speed_tts = calculate_speed(full_text, desired_duration)
403
  # Create the speech using OpenAI TTS API
404
  response = client.audio.speech.create(
405
  model=model,
406
  voice=voice,
407
  input=full_text,
408
+ speed=speed_tts
409
  )
410
  # Save the audio to the specified path
411
  with open(output_audio_path, 'wb') as f: