younes21000 commited on
Commit
476a03d
·
verified ·
1 Parent(s): 1bce94f

Update app.py

Browse files

download the video with the original subtitles

Files changed (1) hide show
  1. app.py +16 -16
app.py CHANGED
@@ -61,7 +61,7 @@ def add_subtitle(video):
61
  transcriptions = list(executor.map(transcribe_audio, chunks))
62
 
63
  full_transcription = " ".join(transcriptions)
64
- subtitle_storage["original"] = full_transcription # Store the original subtitle
65
  subtitle_storage["video_path"] = video_path # Store the video path
66
 
67
  return f"Subtitle added: {full_transcription[:100]}..." # Display first 100 characters
@@ -119,14 +119,14 @@ def download_word():
119
  except Exception as e:
120
  return f"Error in saving subtitles as Word: {e}"
121
 
122
- def download_video():
 
123
  try:
124
- # Add subtitles to the video
125
  original_subtitle = subtitle_storage.get("original")
126
- translated_subtitle = subtitle_storage.get("translated")
127
 
128
- if not original_subtitle or not translated_subtitle:
129
- return "No subtitles to overlay on video!"
130
 
131
  video_path = subtitle_storage.get("video_path")
132
  video = mp.VideoFileClip(video_path)
@@ -134,13 +134,13 @@ def download_video():
134
  # Function to generate subtitle text
135
  generator = lambda txt: TextClip(txt, font='Arial', fontsize=24, color='white')
136
 
137
- # Generate subtitles (each subtitle will last for 2.5 seconds with millisecond precision)
138
  subs = []
139
  subtitle_length = 2.5 # seconds each subtitle will be displayed
140
- for i in range(0, len(translated_subtitle), 40): # Reduced chunk size for better handling
141
  start_time = (i // 40) * subtitle_length
142
  end_time = start_time + subtitle_length
143
- subtitle_text = translated_subtitle[i:i + 40].strip() # Get the next 40 characters
144
 
145
  if subtitle_text:
146
  subs.append((start_time, end_time, subtitle_text)) # Create a tuple for start time, end time, and text
@@ -154,14 +154,14 @@ def download_video():
154
  # Overlay subtitles on video
155
  subtitled_video = mp.CompositeVideoClip([video, subtitles.set_position(('center', 'bottom'))])
156
 
157
- output_video_path = "subtitled_video.mp4"
158
  subtitled_video.write_videofile(output_video_path, codec='libx264') # Use libx264 for better compatibility
159
 
160
  # Return the file path for download
161
- return output_video_path # Gradio will handle this as a downloadable file
162
 
163
  except Exception as e:
164
- return f"Error in generating subtitled video: {e}"
165
 
166
  # Gradio UI Interface
167
  with gr.Blocks() as demo:
@@ -198,12 +198,12 @@ with gr.Blocks() as demo:
198
 
199
  download_button.click(download_word, inputs=None, outputs=download_status)
200
 
201
- # Download Subtitled Video
202
  with gr.Row():
203
- download_video_button = gr.Button("Download Subtitled Video")
204
- download_video_status = gr.File(label="Download Subtitled Video") # File output for Video download
205
 
206
- download_video_button.click(download_video, inputs=None, outputs=download_video_status)
207
 
208
  # Launch the Gradio app
209
  demo.launch()
 
61
  transcriptions = list(executor.map(transcribe_audio, chunks))
62
 
63
  full_transcription = " ".join(transcriptions)
64
+ subtitle_storage["original"] = full_transcription # Store the original subtitle (English or other)
65
  subtitle_storage["video_path"] = video_path # Store the video path
66
 
67
  return f"Subtitle added: {full_transcription[:100]}..." # Display first 100 characters
 
119
  except Exception as e:
120
  return f"Error in saving subtitles as Word: {e}"
121
 
122
+ def download_original_subtitled_video():
123
+ """Download video with original subtitles (no translation)."""
124
  try:
125
+ # Add original subtitles to the video
126
  original_subtitle = subtitle_storage.get("original")
 
127
 
128
+ if not original_subtitle:
129
+ return "No original subtitles available!"
130
 
131
  video_path = subtitle_storage.get("video_path")
132
  video = mp.VideoFileClip(video_path)
 
134
  # Function to generate subtitle text
135
  generator = lambda txt: TextClip(txt, font='Arial', fontsize=24, color='white')
136
 
137
+ # Generate original subtitles with start_time, end_time, and text
138
  subs = []
139
  subtitle_length = 2.5 # seconds each subtitle will be displayed
140
+ for i in range(0, len(original_subtitle), 40): # Adjusted chunk size
141
  start_time = (i // 40) * subtitle_length
142
  end_time = start_time + subtitle_length
143
+ subtitle_text = original_subtitle[i:i + 40].strip() # Get the next 40 characters
144
 
145
  if subtitle_text:
146
  subs.append((start_time, end_time, subtitle_text)) # Create a tuple for start time, end time, and text
 
154
  # Overlay subtitles on video
155
  subtitled_video = mp.CompositeVideoClip([video, subtitles.set_position(('center', 'bottom'))])
156
 
157
+ output_video_path = "original_subtitled_video.mp4"
158
  subtitled_video.write_videofile(output_video_path, codec='libx264') # Use libx264 for better compatibility
159
 
160
  # Return the file path for download
161
+ return output_video_path
162
 
163
  except Exception as e:
164
+ return f"Error in generating original subtitled video: {e}"
165
 
166
  # Gradio UI Interface
167
  with gr.Blocks() as demo:
 
198
 
199
  download_button.click(download_word, inputs=None, outputs=download_status)
200
 
201
+ # Download Original Subtitled Video (no translation)
202
  with gr.Row():
203
+ download_original_video_button = gr.Button("Download Original Subtitled Video")
204
+ download_original_video_status = gr.File(label="Download Original Subtitled Video") # File output for original subtitled video
205
 
206
+ download_original_video_button.click(download_original_subtitled_video, inputs=None, outputs=download_original_video_status)
207
 
208
  # Launch the Gradio app
209
  demo.launch()