Nick088 commited on
Commit
481eae2
·
verified ·
1 Parent(s): 2fa0b5a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -29
app.py CHANGED
@@ -281,7 +281,7 @@ def generate_subtitles(audio_file_path, prompt, language, auto_detect_language):
281
 
282
  # If there's an error during file check
283
  if error_message:
284
- return error_message, None, None, None # Return None for the download button
285
 
286
  with open(processed_path, "rb") as file:
287
  transcription_json = client.audio.transcriptions.create(
@@ -298,7 +298,8 @@ def generate_subtitles(audio_file_path, prompt, language, auto_detect_language):
298
  transcription_text = transcription_json['text']
299
 
300
  srt_content = create_srt_from_text(transcription_text)
301
-
 
302
  with tempfile.NamedTemporaryFile(mode="w", delete=False) as temp_srt_file:
303
  temp_srt_path = temp_srt_file.name
304
  temp_srt_file.write(srt_content)
@@ -306,35 +307,15 @@ def generate_subtitles(audio_file_path, prompt, language, auto_detect_language):
306
  # Generate subtitles and add to video if MP4
307
  if audio_file_path.lower().endswith(".mp4"):
308
  try:
309
- # Use temporary file with -i for subtitles
310
- subprocess.run(
311
- [
312
- "ffmpeg",
313
- "-i",
314
- audio_file_path,
315
- "-i",
316
- temp_srt_path,
317
- "-map",
318
- "0:v",
319
- "-map",
320
- "0:a",
321
- "-map",
322
- "1:s?",
323
- "-c:v",
324
- "copy",
325
- "-c:a",
326
- "copy",
327
- "-c:s",
328
- "mov_text",
329
- audio_file_path.replace(".mp4", "_with_subs.mp4"), # Create new file name
330
- ],
331
- check=True,
332
- )
333
- return temp_srt_file, audio_file_path.replace(".mp4", "_with_subs.mp4"), None, None # Return None for the download button
334
  except subprocess.CalledProcessError as e:
335
- return temp_srt_file, None, f"Error during subtitle addition: {e}", None # Return None for the download button
 
 
 
336
 
337
- return temp_srt_file, None, None, None
338
 
339
 
340
  with gr.Blocks() as demo:
 
281
 
282
  # If there's an error during file check
283
  if error_message:
284
+ return error_message, None, None, None # Return None for the download button
285
 
286
  with open(processed_path, "rb") as file:
287
  transcription_json = client.audio.transcriptions.create(
 
298
  transcription_text = transcription_json['text']
299
 
300
  srt_content = create_srt_from_text(transcription_text)
301
+
302
+ # Create temporary file
303
  with tempfile.NamedTemporaryFile(mode="w", delete=False) as temp_srt_file:
304
  temp_srt_path = temp_srt_file.name
305
  temp_srt_file.write(srt_content)
 
307
  # Generate subtitles and add to video if MP4
308
  if audio_file_path.lower().endswith(".mp4"):
309
  try:
310
+ # ... (ffmpeg code to add subtitles) ...
311
+ return srt_content, audio_file_path.replace(".mp4", "_with_subs.mp4"), None, temp_srt_path # Return the temp_srt_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
312
  except subprocess.CalledProcessError as e:
313
+ return srt_content, None, f"Error during subtitle addition: {e}", temp_srt_path # Return the temp_srt_path
314
+ finally:
315
+ if os.path.exists(temp_srt_path):
316
+ os.remove(temp_srt_path)
317
 
318
+ return srt_content, None, None, temp_srt_path
319
 
320
 
321
  with gr.Blocks() as demo: