DHEIVER commited on
Commit
a79fe10
·
verified ·
1 Parent(s): d83a342

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -43
app.py CHANGED
@@ -68,48 +68,50 @@ def process_video(video_path, min_silence_len=1000, silence_thresh=-40, max_work
68
  # Criar diretório temporário
69
  temp_dir = tempfile.mkdtemp()
70
 
71
- # Extrair áudio
72
- temp_audio = os.path.join(temp_dir, "temp_audio.wav")
73
- extract_audio_ffmpeg(video_path, temp_audio)
74
-
75
- # Detectar segmentos não silenciosos
76
- audio = AudioSegment.from_wav(temp_audio)
77
- nonsilent_ranges = detect_nonsilent(
78
- audio,
79
- min_silence_len=min_silence_len,
80
- silence_thresh=silence_thresh
81
- )
82
-
83
- if not nonsilent_ranges:
84
- return video_path
85
-
86
- # Converter para segundos
87
- nonsilent_ranges_sec = [(start/1000.0, end/1000.0) for start, end in nonsilent_ranges]
88
-
89
- # Preparar argumentos para processamento paralelo
90
- chunk_args = []
91
- chunk_outputs = []
92
- for i, (start, end) in enumerate(nonsilent_ranges_sec):
93
- output_chunk = os.path.join(temp_dir, f"chunk_{i}.mp4")
94
- chunk_args.append((video_path, output_chunk, start, end))
95
- chunk_outputs.append(output_chunk)
96
-
97
- # Processar chunks em paralelo
98
- with ThreadPoolExecutor(max_workers=max_workers) as executor:
99
- list(executor.map(process_video_chunk, chunk_args))
100
-
101
- # Concatenar todos os chunks
102
- output_path = os.path.join(temp_dir, "processed_video.mp4")
103
- concatenate_videos_ffmpeg(chunk_outputs, output_path)
104
-
105
- # Criar cópia do resultado em local permanente
106
- final_output = str(Path(video_path).parent / f"processed_{Path(video_path).name}")
107
- shutil.copy2(output_path, final_output)
108
-
109
- # Limpar arquivos temporários
110
- shutil.rmtree(temp_dir)
111
 
112
- return final_output
 
 
113
 
114
  def remove_silence(video, silence_duration, silence_threshold):
115
  if video is None:
@@ -126,7 +128,8 @@ def remove_silence(video, silence_duration, silence_threshold):
126
  progress(1, desc="Processamento concluído!")
127
  return processed_video
128
  except Exception as e:
129
- return str(e)
 
130
 
131
  # Interface Gradio com indicador de progresso
132
  with gr.Blocks(title="Removedor de Silêncio de Vídeos") as app:
@@ -141,7 +144,7 @@ with gr.Blocks(title="Removedor de Silêncio de Vídeos") as app:
141
  with gr.Column():
142
  video_input = gr.Video(
143
  label="Vídeo de Entrada",
144
- type="filepath" # Usar filepath para upload mais rápido
145
  )
146
  silence_duration = gr.Slider(
147
  minimum=0.1,
 
68
  # Criar diretório temporário
69
  temp_dir = tempfile.mkdtemp()
70
 
71
+ try:
72
+ # Extrair áudio
73
+ temp_audio = os.path.join(temp_dir, "temp_audio.wav")
74
+ extract_audio_ffmpeg(video_path, temp_audio)
75
+
76
+ # Detectar segmentos não silenciosos
77
+ audio = AudioSegment.from_wav(temp_audio)
78
+ nonsilent_ranges = detect_nonsilent(
79
+ audio,
80
+ min_silence_len=min_silence_len,
81
+ silence_thresh=silence_thresh
82
+ )
83
+
84
+ if not nonsilent_ranges:
85
+ return video_path
86
+
87
+ # Converter para segundos
88
+ nonsilent_ranges_sec = [(start/1000.0, end/1000.0) for start, end in nonsilent_ranges]
89
+
90
+ # Preparar argumentos para processamento paralelo
91
+ chunk_args = []
92
+ chunk_outputs = []
93
+ for i, (start, end) in enumerate(nonsilent_ranges_sec):
94
+ output_chunk = os.path.join(temp_dir, f"chunk_{i}.mp4")
95
+ chunk_args.append((video_path, output_chunk, start, end))
96
+ chunk_outputs.append(output_chunk)
97
+
98
+ # Processar chunks em paralelo
99
+ with ThreadPoolExecutor(max_workers=max_workers) as executor:
100
+ list(executor.map(process_video_chunk, chunk_args))
101
+
102
+ # Concatenar todos os chunks
103
+ output_path = os.path.join(temp_dir, "processed_video.mp4")
104
+ concatenate_videos_ffmpeg(chunk_outputs, output_path)
105
+
106
+ # Criar cópia do resultado em local permanente
107
+ final_output = str(Path(video_path).parent / f"processed_{Path(video_path).name}")
108
+ shutil.copy2(output_path, final_output)
109
+
110
+ return final_output
111
 
112
+ finally:
113
+ # Limpar arquivos temporários
114
+ shutil.rmtree(temp_dir)
115
 
116
  def remove_silence(video, silence_duration, silence_threshold):
117
  if video is None:
 
128
  progress(1, desc="Processamento concluído!")
129
  return processed_video
130
  except Exception as e:
131
+ gr.Error(str(e))
132
+ return None
133
 
134
  # Interface Gradio com indicador de progresso
135
  with gr.Blocks(title="Removedor de Silêncio de Vídeos") as app:
 
144
  with gr.Column():
145
  video_input = gr.Video(
146
  label="Vídeo de Entrada",
147
+ sources=["upload"] # Replaced 'type' with 'sources'
148
  )
149
  silence_duration = gr.Slider(
150
  minimum=0.1,