artificialguybr commited on
Commit
e00b158
·
1 Parent(s): 39d5916

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -9
app.py CHANGED
@@ -8,6 +8,8 @@ import os
8
  import ffmpeg
9
  from zipfile import ZipFile
10
  import stat
 
 
11
 
12
  ZipFile("ffmpeg.zip").extractall()
13
  st = os.stat('ffmpeg')
@@ -27,15 +29,14 @@ def process_video(Video, target_language):
27
  run(["ffmpeg", "-version"])
28
 
29
 
30
- audio_file = tempfile.NamedTemporaryFile(suffix=".wav").name
31
- print("Executando FFmpeg para extração de áudio")
32
  run(["ffmpeg", "-i", Video, audio_file])
33
 
34
  print("Iniciando transcrição com Whisper")
35
  segments, _ = whisper_model.transcribe(audio_file, beam_size=5)
36
  segments = list(segments)
37
- temp_transcript_file = tempfile.NamedTemporaryFile(delete=False, suffix=".srt")
38
- with open(temp_transcript_file.name, "w", encoding="utf-8") as f:
39
  counter = 1
40
  for segment in segments:
41
  start_minutes = int(segment.start // 60)
@@ -51,8 +52,8 @@ def process_video(Video, target_language):
51
  f.write(f"{segment.text}\n\n")
52
  counter += 1
53
  flores_code = lang_codes.get(target_language, "eng_Latn")
54
- temp_translated_file = tempfile.NamedTemporaryFile(delete=False, suffix=".srt")
55
- with open(temp_transcript_file.name, "r", encoding="utf-8") as infile, open(temp_translated_file.name, "w", encoding="utf-8") as outfile:
56
  for line in infile:
57
  if line.strip().isnumeric() or "-->" in line:
58
  outfile.write(line)
@@ -89,16 +90,17 @@ def process_video(Video, target_language):
89
  # Debugging: Validate FFmpeg command for subtitle embedding
90
  print("Validating FFmpeg command for subtitle embedding...")
91
  try:
92
- result = run(["ffmpeg", "-i", Video, "-vf", f"subtitles={absolute_path_translated}", output_video])
93
  if result.returncode == 0:
94
  print("FFmpeg executed successfully.")
95
  else:
96
  print(f"FFmpeg failed with return code {result.returncode}.")
97
  except Exception as e:
98
  print(f"Exception occurred: {e}")
99
- os.unlink(temp_transcript_file.name)
100
- os.unlink(temp_translated_file.name)
101
  print("process_video concluído com sucesso")
 
 
 
102
  return output_video
103
 
104
  iface = gr.Interface(
 
8
  import ffmpeg
9
  from zipfile import ZipFile
10
  import stat
11
+ import uuid
12
+
13
 
14
  ZipFile("ffmpeg.zip").extractall()
15
  st = os.stat('ffmpeg')
 
29
  run(["ffmpeg", "-version"])
30
 
31
 
32
+ audio_file = f"{uuid.uuid4()}.wav"
 
33
  run(["ffmpeg", "-i", Video, audio_file])
34
 
35
  print("Iniciando transcrição com Whisper")
36
  segments, _ = whisper_model.transcribe(audio_file, beam_size=5)
37
  segments = list(segments)
38
+ transcript_file = f"{uuid.uuid4()}.srt"
39
+ with open(transcript_file, "w", encoding="utf-8") as f:
40
  counter = 1
41
  for segment in segments:
42
  start_minutes = int(segment.start // 60)
 
52
  f.write(f"{segment.text}\n\n")
53
  counter += 1
54
  flores_code = lang_codes.get(target_language, "eng_Latn")
55
+ translated_file = f"{uuid.uuid4()}.srt"
56
+ with open(transcript_file, "r", encoding="utf-8") as infile, open(translated_file, "w", encoding="utf-8") as outfile:
57
  for line in infile:
58
  if line.strip().isnumeric() or "-->" in line:
59
  outfile.write(line)
 
90
  # Debugging: Validate FFmpeg command for subtitle embedding
91
  print("Validating FFmpeg command for subtitle embedding...")
92
  try:
93
+ result = run(["ffmpeg", "-i", Video, "-vf", f"subtitles={translated_file}", output_video])
94
  if result.returncode == 0:
95
  print("FFmpeg executed successfully.")
96
  else:
97
  print(f"FFmpeg failed with return code {result.returncode}.")
98
  except Exception as e:
99
  print(f"Exception occurred: {e}")
 
 
100
  print("process_video concluído com sucesso")
101
+ os.unlink(audio_file)
102
+ os.unlink(transcript_file)
103
+ os.unlink(translated_file)
104
  return output_video
105
 
106
  iface = gr.Interface(