kevinwang676 commited on
Commit
6b54c49
1 Parent(s): 8905446

Update app_share.py

Browse files
Files changed (1) hide show
  1. app_share.py +41 -23
app_share.py CHANGED
@@ -308,14 +308,14 @@ import shutil
308
 
309
  import zipfile
310
 
311
- def zip_sliced_files(directory, zip_filename):
312
  # Create a ZipFile object
313
  with zipfile.ZipFile(zip_filename, 'w') as zipf:
314
  # Iterate over all files in the directory
315
  for foldername, subfolders, filenames in os.walk(directory):
316
  for filename in filenames:
317
  # Check if the file starts with "sliced" and has a .wav extension
318
- if filename.startswith("sliced") and filename.endswith(".wav"):
319
  # Create the complete file path
320
  file_path = os.path.join(foldername, filename)
321
  # Add the file to the zip file
@@ -324,24 +324,27 @@ def zip_sliced_files(directory, zip_filename):
324
 
325
  # set speed
326
 
327
- def change_speed(audio_inp, speed=1.0):
328
- audio = AudioSegment.from_file(audio_inp)
329
 
330
- sound_with_altered_frame_rate = audio._spawn(audio.raw_data, overrides={
331
- "frame_rate": int(audio.frame_rate * speed)
332
- })
333
- slower_audio = sound_with_altered_frame_rate.set_frame_rate(audio.frame_rate)
334
- slower_audio.export("slower_speech.wav", format="wav")
335
- return "slower_speech.wav"
336
 
 
 
 
 
 
 
 
337
  # delete files first
338
 
339
- def delete_sliced_files(directory):
340
  # Iterate over all files in the directory
341
  for foldername, subfolders, filenames in os.walk(directory):
342
  for filename in filenames:
343
  # Check if the file starts with "sliced"
344
- if filename.startswith("sliced"):
345
  # Create the complete file path
346
  file_path = os.path.join(foldername, filename)
347
  # Delete the file
@@ -352,7 +355,7 @@ def delete_sliced_files(directory):
352
  def convert_from_srt(api_key, filename, audio_full, voice, multilingual):
353
 
354
  subtitle_list = read_srt(filename)
355
- delete_sliced_files("./")
356
 
357
  #audio_data, sr = librosa.load(audio_full, sr=44100)
358
 
@@ -382,7 +385,7 @@ def convert_from_srt(api_key, filename, audio_full, voice, multilingual):
382
  pass
383
  merge_audios("output")
384
 
385
- zip_sliced_files("./", "参考音频.zip")
386
 
387
  return "AI配音版.wav", "参考音频.zip"
388
 
@@ -392,14 +395,29 @@ restart_markdown = ("""
392
 
393
  import ffmpeg
394
 
395
- def denoise(video_full):
396
-
397
- if os.path.exists("audio_full.wav"):
398
- os.remove("audio_full.wav")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
399
 
400
- ffmpeg.input(video_full).output("audio_full.wav", ac=2, ar=44100).run()
401
 
402
- return "audio_full.wav"
403
 
404
 
405
  with gr.Blocks() as app:
@@ -407,9 +425,9 @@ with gr.Blocks() as app:
407
  gr.Markdown("### <center>🌟 只需上传SRT文件和原版配音文件即可,每次一集视频AI自动配音!Developed by Kevin Wang </center>")
408
  with gr.Tab("📺视频转音频"):
409
  with gr.Row():
410
- inp_video = gr.Video(label="请上传一集包含原声配音的视频", info="需要是.mp4视频文件")
411
  btn_convert = gr.Button("视频文件转音频", variant="primary")
412
- out_audio = gr.Audio(label="视频对应的音频文件,可以下载至本地后进行降噪处理", type="filepath")
413
 
414
  btn_convert.click(denoise, [inp_video], [out_audio])
415
  with gr.Tab("🎶AI配音"):
@@ -426,7 +444,7 @@ with gr.Blocks() as app:
426
  with gr.Column():
427
  out1 = gr.Audio(label="为您生成的AI完整配音", type="filepath")
428
  out2 = gr.File(label="包含所有参考音频的zip文件")
429
- inp_speed = gr.Slider(label="设置AI配音的速度", minimum=0.8, maximum=1.2, value=1.0, step=0.01)
430
  btn2 = gr.Button("一键改变AI配音速度")
431
  out3 = gr.Audio(label="变速后的AI配音", type="filepath")
432
 
 
308
 
309
  import zipfile
310
 
311
+ def zip_sliced_files(directory, zip_filename, chosen_name):
312
  # Create a ZipFile object
313
  with zipfile.ZipFile(zip_filename, 'w') as zipf:
314
  # Iterate over all files in the directory
315
  for foldername, subfolders, filenames in os.walk(directory):
316
  for filename in filenames:
317
  # Check if the file starts with "sliced" and has a .wav extension
318
+ if filename.startswith(f"{chosen_name}") and filename.endswith(".wav"):
319
  # Create the complete file path
320
  file_path = os.path.join(foldername, filename)
321
  # Add the file to the zip file
 
324
 
325
  # set speed
326
 
327
+ from pydub.effects import speedup
 
328
 
329
+ def change_speed(input_file, speed=1.0):
330
+ # Load the audio file
331
+ audio = AudioSegment.from_file(input_file)
 
 
 
332
 
333
+ # Change the speed of the audio
334
+ faster_audio = speedup(audio, playback_speed=speed)
335
+
336
+ # Export the modified audio to a new file
337
+ faster_audio.export("speed_changed_speech.wav", format="wav")
338
+ return "speed_changed_speech.wav"
339
+
340
  # delete files first
341
 
342
+ def delete_sliced_files(directory, chosen_name):
343
  # Iterate over all files in the directory
344
  for foldername, subfolders, filenames in os.walk(directory):
345
  for filename in filenames:
346
  # Check if the file starts with "sliced"
347
+ if filename.startswith(f"{chosen_name}"):
348
  # Create the complete file path
349
  file_path = os.path.join(foldername, filename)
350
  # Delete the file
 
355
  def convert_from_srt(api_key, filename, audio_full, voice, multilingual):
356
 
357
  subtitle_list = read_srt(filename)
358
+ delete_sliced_files("./", "sliced")
359
 
360
  #audio_data, sr = librosa.load(audio_full, sr=44100)
361
 
 
385
  pass
386
  merge_audios("output")
387
 
388
+ zip_sliced_files("./", "参考音频.zip", "sliced")
389
 
390
  return "AI配音版.wav", "参考音频.zip"
391
 
 
395
 
396
  import ffmpeg
397
 
398
+ def save_file_with_new_name(original_file_path, new_file_path):
399
+ shutil.copyfile(original_file_path, new_file_path)
400
+
401
+
402
+ def denoise(input_files):
403
+ delete_sliced_files("./", "input_video")
404
+
405
+ #if os.path.exists("audio_full.wav"):
406
+ # os.remove("audio_full.wav")
407
+ for video_file in input_files:
408
+
409
+ name1 = video_file.name
410
+ file_name_with_extension = name1.split('/')[-1]
411
+ file_name1 = file_name_with_extension.split('.mp4')[0] + ".mp4"
412
+
413
+ save_file_with_new_name(video_file.name, file_name1)
414
+
415
+ ffmpeg.input(file_name1).output("input_video" + file_name1 + ".wav", ac=2, ar=44100).run()
416
+
417
+ zip_sliced_files("./", "转换后的音频.zip", "input_video")
418
 
 
419
 
420
+ return "转换后的音频.zip"
421
 
422
 
423
  with gr.Blocks() as app:
 
425
  gr.Markdown("### <center>🌟 只需上传SRT文件和原版配音文件即可,每次一集视频AI自动配音!Developed by Kevin Wang </center>")
426
  with gr.Tab("📺视频转音频"):
427
  with gr.Row():
428
+ inp_video = gr.Files(label="您可以上传多集包含原声配音的视频", file_types=['.mp4'])
429
  btn_convert = gr.Button("视频文件转音频", variant="primary")
430
+ out_audio = gr.File(label="包含所有配音音频的zip文件")
431
 
432
  btn_convert.click(denoise, [inp_video], [out_audio])
433
  with gr.Tab("🎶AI配音"):
 
444
  with gr.Column():
445
  out1 = gr.Audio(label="为您生成的AI完整配音", type="filepath")
446
  out2 = gr.File(label="包含所有参考音频的zip文件")
447
+ inp_speed = gr.Slider(label="设置AI配音的速度", minimum=1.02, maximum=1.5, value=1.02, step=0.01)
448
  btn2 = gr.Button("一键改变AI配音速度")
449
  out3 = gr.Audio(label="变速后的AI配音", type="filepath")
450