DHEIVER commited on
Commit
8a80570
·
verified ·
1 Parent(s): 12d32cd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -5
app.py CHANGED
@@ -28,8 +28,9 @@ def cut_video_ffmpeg(input_path, output_path, start, end):
28
  'ffmpeg', '-i', input_path,
29
  '-ss', str(start),
30
  '-t', str(end - start),
31
- '-c', 'copy', # Usa codec copying para maior velocidade
32
- '-avoid_negative_ts', '1',
 
33
  '-y',
34
  output_path
35
  ]
@@ -43,6 +44,7 @@ def process_video_chunk(args):
43
 
44
  def concatenate_videos_ffmpeg(video_list, output_path):
45
  """Concatena vídeos usando FFmpeg"""
 
46
  list_file = tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.txt')
47
  for video in video_list:
48
  list_file.write(f"file '{video}'\n")
@@ -52,7 +54,9 @@ def concatenate_videos_ffmpeg(video_list, output_path):
52
  'ffmpeg', '-f', 'concat',
53
  '-safe', '0',
54
  '-i', list_file.name,
55
- '-c', 'copy',
 
 
56
  '-y',
57
  output_path
58
  ]
@@ -67,9 +71,11 @@ def process_video(video_path, min_silence_len=1000, silence_thresh=-40, max_work
67
  temp_dir = tempfile.mkdtemp()
68
 
69
  try:
 
70
  temp_audio = os.path.join(temp_dir, "temp_audio.wav")
71
  extract_audio_ffmpeg(video_path, temp_audio)
72
 
 
73
  audio = AudioSegment.from_wav(temp_audio)
74
  nonsilent_ranges = detect_nonsilent(
75
  audio,
@@ -80,8 +86,10 @@ def process_video(video_path, min_silence_len=1000, silence_thresh=-40, max_work
80
  if not nonsilent_ranges:
81
  return video_path
82
 
 
83
  nonsilent_ranges_sec = [(start/1000.0, end/1000.0) for start, end in nonsilent_ranges]
84
 
 
85
  chunk_args = []
86
  chunk_outputs = []
87
  for i, (start, end) in enumerate(nonsilent_ranges_sec):
@@ -89,12 +97,15 @@ def process_video(video_path, min_silence_len=1000, silence_thresh=-40, max_work
89
  chunk_args.append((video_path, output_chunk, start, end))
90
  chunk_outputs.append(output_chunk)
91
 
 
92
  with ThreadPoolExecutor(max_workers=max_workers) as executor:
93
  list(executor.map(process_video_chunk, chunk_args))
94
 
 
95
  output_path = os.path.join(temp_dir, "processed_video.mp4")
96
  concatenate_videos_ffmpeg(chunk_outputs, output_path)
97
 
 
98
  final_output = str(Path(video_path).parent / f"processed_{Path(video_path).name}")
99
  shutil.copy2(output_path, final_output)
100
 
@@ -147,8 +158,7 @@ with gr.Blocks(title="Removedor de Silêncio de Vídeos") as app:
147
  with gr.Row():
148
  with gr.Column():
149
  video_input = gr.Video(
150
- label="Selecione ou Arraste o Vídeo",
151
- format="mp4"
152
  )
153
 
154
  with gr.Row():
 
28
  'ffmpeg', '-i', input_path,
29
  '-ss', str(start),
30
  '-t', str(end - start),
31
+ '-c:v', 'libx264', # Codec de vídeo
32
+ '-c:a', 'aac', # Codec de áudio
33
+ '-strict', 'experimental',
34
  '-y',
35
  output_path
36
  ]
 
44
 
45
  def concatenate_videos_ffmpeg(video_list, output_path):
46
  """Concatena vídeos usando FFmpeg"""
47
+ # Cria arquivo de lista
48
  list_file = tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.txt')
49
  for video in video_list:
50
  list_file.write(f"file '{video}'\n")
 
54
  'ffmpeg', '-f', 'concat',
55
  '-safe', '0',
56
  '-i', list_file.name,
57
+ '-c:v', 'libx264', # Codec de vídeo
58
+ '-c:a', 'aac', # Codec de áudio
59
+ '-strict', 'experimental',
60
  '-y',
61
  output_path
62
  ]
 
71
  temp_dir = tempfile.mkdtemp()
72
 
73
  try:
74
+ # Extrair áudio para análise
75
  temp_audio = os.path.join(temp_dir, "temp_audio.wav")
76
  extract_audio_ffmpeg(video_path, temp_audio)
77
 
78
+ # Analisar áudio para detectar silêncio
79
  audio = AudioSegment.from_wav(temp_audio)
80
  nonsilent_ranges = detect_nonsilent(
81
  audio,
 
86
  if not nonsilent_ranges:
87
  return video_path
88
 
89
+ # Converter para segundos
90
  nonsilent_ranges_sec = [(start/1000.0, end/1000.0) for start, end in nonsilent_ranges]
91
 
92
+ # Preparar chunks de vídeo
93
  chunk_args = []
94
  chunk_outputs = []
95
  for i, (start, end) in enumerate(nonsilent_ranges_sec):
 
97
  chunk_args.append((video_path, output_chunk, start, end))
98
  chunk_outputs.append(output_chunk)
99
 
100
+ # Processar chunks em paralelo
101
  with ThreadPoolExecutor(max_workers=max_workers) as executor:
102
  list(executor.map(process_video_chunk, chunk_args))
103
 
104
+ # Concatenar chunks
105
  output_path = os.path.join(temp_dir, "processed_video.mp4")
106
  concatenate_videos_ffmpeg(chunk_outputs, output_path)
107
 
108
+ # Copiar resultado final
109
  final_output = str(Path(video_path).parent / f"processed_{Path(video_path).name}")
110
  shutil.copy2(output_path, final_output)
111
 
 
158
  with gr.Row():
159
  with gr.Column():
160
  video_input = gr.Video(
161
+ label="Selecione ou Arraste o Vídeo"
 
162
  )
163
 
164
  with gr.Row():