Nishur commited on
Commit
413f8ab
·
verified ·
1 Parent(s): eb91eca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -22
app.py CHANGED
@@ -110,22 +110,20 @@ def translate_subtitles(srt_path, target_langs):
110
  logger.error(f"Translation failed: {str(e)}", exc_info=True)
111
  raise Exception(f"Translation failed: {str(e)}")
112
 
113
- def add_subtitles_ffmpeg(video_path, srt_path, output_path):
114
- """Add subtitles to video using ffmpeg"""
115
  try:
116
- logger.info(f"Adding subtitles to video using ffmpeg")
 
 
 
 
 
117
  cmd = [
118
  'ffmpeg',
119
  '-i', video_path, # Input video
120
- '-f', 'srt', # SRT format
121
- '-i', srt_path, # Input subtitles
122
- '-map', '0:v', # Map video from first input
123
- '-map', '0:a', # Map audio from first input
124
- '-map', '1', # Map subtitles from second input
125
- '-c:v', 'copy', # Copy video codec
126
  '-c:a', 'copy', # Copy audio codec
127
- '-c:s', 'mov_text', # Use mov_text codec for subtitles
128
- '-metadata:s:s:0', 'language=eng', # Set subtitle language
129
  '-y', # Overwrite output file
130
  output_path
131
  ]
@@ -134,30 +132,30 @@ def add_subtitles_ffmpeg(video_path, srt_path, output_path):
134
  process = subprocess.run(cmd, capture_output=True, text=True)
135
 
136
  if process.returncode != 0:
137
- logger.error(f"ffmpeg error: {process.stderr}")
138
 
139
- # If subtitle embedding fails, try burning subtitles into video
140
- logger.info("Attempting to burn subtitles into video")
141
  cmd = [
142
  'ffmpeg',
143
  '-i', video_path, # Input video
144
- '-vf', f"subtitles='{srt_path}'", # Burn subtitles into video
145
  '-c:a', 'copy', # Copy audio codec
146
  '-y', # Overwrite output file
147
  output_path
148
  ]
149
 
150
- logger.info(f"Running fallback command: {' '.join(cmd)}")
151
  process = subprocess.run(cmd, capture_output=True, text=True)
152
 
153
  if process.returncode != 0:
154
- logger.error(f"Subtitle burning failed: {process.stderr}")
155
- raise Exception(f"Failed to add subtitles: {process.stderr}")
156
 
157
  return output_path
158
  except Exception as e:
159
- logger.error(f"Subtitle addition failed: {str(e)}", exc_info=True)
160
- raise Exception(f"Subtitle addition failed: {str(e)}")
161
 
162
  def process_video(video_file, source_lang, target_langs, progress=gr.Progress()):
163
  """Process video with translation"""
@@ -200,7 +198,7 @@ def process_video(video_file, source_lang, target_langs, progress=gr.Progress())
200
  logger.info(f"Adding {lang_code} subtitles to video: {output_path}")
201
 
202
  try:
203
- output_video = add_subtitles_ffmpeg(base_video, sub_path, output_path)
204
 
205
  # Verify the output file exists and has content
206
  if os.path.exists(output_video) and os.path.getsize(output_video) > 1000:
@@ -214,7 +212,7 @@ def process_video(video_file, source_lang, target_langs, progress=gr.Progress())
214
  # If all output videos failed, return the original
215
  if not output_videos:
216
  logger.warning("All subtitle additions failed, returning original video")
217
- output_videos = [base_video]
218
 
219
  progress(1.0, "Done!")
220
  return output_videos[0], f"Processing complete. Video saved to: {output_videos[0]}"
@@ -252,4 +250,11 @@ with gr.Blocks() as demo:
252
  )
253
 
254
  if __name__ == "__main__":
 
 
 
 
 
 
 
255
  demo.launch()
 
110
  logger.error(f"Translation failed: {str(e)}", exc_info=True)
111
  raise Exception(f"Translation failed: {str(e)}")
112
 
113
+ def burn_subtitles_ffmpeg(video_path, srt_path, output_path):
114
+ """Burn subtitles directly into the video using ffmpeg"""
115
  try:
116
+ logger.info(f"Burning subtitles into video using ffmpeg")
117
+
118
+ # Escape special characters in paths for ffmpeg filters
119
+ escaped_srt_path = srt_path.replace(":", "\\:").replace("'", "\\'").replace(" ", "\\ ")
120
+
121
+ # Command to burn subtitles into video
122
  cmd = [
123
  'ffmpeg',
124
  '-i', video_path, # Input video
125
+ '-vf', f"subtitles={escaped_srt_path}:force_style='FontSize=24,PrimaryColour=&H00FFFFFF,OutlineColour=&H00000000,BorderStyle=3'", # Burn subtitles
 
 
 
 
 
126
  '-c:a', 'copy', # Copy audio codec
 
 
127
  '-y', # Overwrite output file
128
  output_path
129
  ]
 
132
  process = subprocess.run(cmd, capture_output=True, text=True)
133
 
134
  if process.returncode != 0:
135
+ logger.error(f"Subtitle burning failed: {process.stderr}")
136
 
137
+ # Try alternative method
138
+ logger.info("Trying alternative method for subtitle burning")
139
  cmd = [
140
  'ffmpeg',
141
  '-i', video_path, # Input video
142
+ '-vf', f"subtitles='{srt_path}'", # Simpler subtitle filter
143
  '-c:a', 'copy', # Copy audio codec
144
  '-y', # Overwrite output file
145
  output_path
146
  ]
147
 
148
+ logger.info(f"Running alternative command: {' '.join(cmd)}")
149
  process = subprocess.run(cmd, capture_output=True, text=True)
150
 
151
  if process.returncode != 0:
152
+ logger.error(f"Alternative subtitle burning failed: {process.stderr}")
153
+ raise Exception(f"Failed to burn subtitles: {process.stderr}")
154
 
155
  return output_path
156
  except Exception as e:
157
+ logger.error(f"Subtitle burning failed: {str(e)}", exc_info=True)
158
+ raise Exception(f"Subtitle burning failed: {str(e)}")
159
 
160
  def process_video(video_file, source_lang, target_langs, progress=gr.Progress()):
161
  """Process video with translation"""
 
198
  logger.info(f"Adding {lang_code} subtitles to video: {output_path}")
199
 
200
  try:
201
+ output_video = burn_subtitles_ffmpeg(base_video, sub_path, output_path)
202
 
203
  # Verify the output file exists and has content
204
  if os.path.exists(output_video) and os.path.getsize(output_video) > 1000:
 
212
  # If all output videos failed, return the original
213
  if not output_videos:
214
  logger.warning("All subtitle additions failed, returning original video")
215
+ return base_video, "Failed to add subtitles to video, returning original"
216
 
217
  progress(1.0, "Done!")
218
  return output_videos[0], f"Processing complete. Video saved to: {output_videos[0]}"
 
250
  )
251
 
252
  if __name__ == "__main__":
253
+ # Display ffmpeg version at startup
254
+ try:
255
+ version_info = subprocess.run(['ffmpeg', '-version'], capture_output=True, text=True)
256
+ logger.info(f"ffmpeg version info: {version_info.stdout.split('\\n')[0]}")
257
+ except:
258
+ logger.warning("Could not determine ffmpeg version")
259
+
260
  demo.launch()