malleswar commited on
Commit
14e68a8
·
verified ·
1 Parent(s): 5f71858

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -24
app.py CHANGED
@@ -197,15 +197,15 @@ def process_video(video_file, language="hindi", subtitle_position="bottom-center
197
 
198
  # Position mapping: alignment and vertical margin
199
  POSITION_MAP = {
200
- "top-left": (7, 80),
201
- "top-center": (8, 80),
202
- "top-right": (9, 80),
203
  "middle-left": (4, 0),
204
  "middle-center": (5, 0),
205
  "middle-right": (6, 0),
206
- "bottom-left": (1, 50),
207
- "bottom-center": (2, 50),
208
- "bottom-right": (3, 50),
209
  }
210
 
211
  alignment, margin = POSITION_MAP.get(subtitle_position, (2, 50))
@@ -269,28 +269,44 @@ def process_video(video_file, language="hindi", subtitle_position="bottom-center
269
  # Modify ASS file to change subtitle position
270
  print(f"\nModifying subtitle position to: {subtitle_position}...")
271
  with open(ass_path, 'r', encoding='utf-8') as f:
272
- ass_content = f.read()
273
 
274
- # Replace default style with custom position
275
- # Format: Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour,
276
- # Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle,
277
- # Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
278
-
279
- # Find and replace the style line
280
- import re
281
- style_pattern = r'Style: Default,[^,]+,\d+,[^,]+,[^,]+,[^,]+,[^,]+,-?\d+,-?\d+,-?\d+,-?\d+,\d+,\d+,\d+,\d+,\d+,\d+,\d+,\d+,\d+,\d+,\d+,\d+'
282
-
283
- def replace_style(match):
284
- parts = match.group(0).split(',')
285
- # Replace alignment (index 17) and MarginV (index 20)
286
- parts[17] = str(alignment)
287
- parts[20] = str(margin)
288
- return ','.join(parts)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
289
 
290
- ass_content = re.sub(style_pattern, replace_style, ass_content)
 
291
 
292
  with open(ass_path, 'w', encoding='utf-8') as f:
293
- f.write(ass_content)
294
 
295
  print(f"✅ Subtitle position updated: {subtitle_position}")
296
 
 
197
 
198
  # Position mapping: alignment and vertical margin
199
  POSITION_MAP = {
200
+ "top-left": (7, 120), # Increased top margin
201
+ "top-center": (8, 120),
202
+ "top-right": (9, 120),
203
  "middle-left": (4, 0),
204
  "middle-center": (5, 0),
205
  "middle-right": (6, 0),
206
+ "bottom-left": (1, 120), # Increased bottom margin
207
+ "bottom-center": (2, 120),
208
+ "bottom-right": (3, 120),
209
  }
210
 
211
  alignment, margin = POSITION_MAP.get(subtitle_position, (2, 50))
 
269
  # Modify ASS file to change subtitle position
270
  print(f"\nModifying subtitle position to: {subtitle_position}...")
271
  with open(ass_path, 'r', encoding='utf-8') as f:
272
+ ass_lines = f.readlines()
273
 
274
+ # Find and modify the Style line
275
+ modified = False
276
+ for i, line in enumerate(ass_lines):
277
+ if line.startswith('Style: Default,'):
278
+ # Split the style line by commas
279
+ parts = line.strip().split(',')
280
+
281
+ # ASS Style format (0-indexed after "Style: Default,"):
282
+ # 0=Fontname, 1=Fontsize, 2=PrimaryColour, 3=SecondaryColour, 4=OutlineColour,
283
+ # 5=BackColour, 6=Bold, 7=Italic, 8=Underline, 9=StrikeOut, 10=ScaleX, 11=ScaleY,
284
+ # 12=Spacing, 13=Angle, 14=BorderStyle, 15=Outline, 16=Shadow, 17=Alignment,
285
+ # 18=MarginL, 19=MarginR, 20=MarginV, 21=Encoding
286
+
287
+ if len(parts) >= 22:
288
+ parts[18] = str(alignment) # Alignment (index 18 after "Style: Default,")
289
+
290
+ # Set horizontal margins for center-aligned subtitles
291
+ if 'center' in subtitle_position:
292
+ parts[19] = '50' # MarginL (left margin)
293
+ parts[20] = '50' # MarginR (right margin)
294
+ else:
295
+ parts[19] = '20' # MarginL
296
+ parts[20] = '20' # MarginR
297
+
298
+ parts[21] = str(margin) # MarginV (vertical margin)
299
+
300
+ ass_lines[i] = ','.join(parts) + '\n'
301
+ modified = True
302
+ print(f"✅ Modified style: Alignment={alignment}, MarginV={margin}")
303
+ break
304
 
305
+ if not modified:
306
+ print("⚠️ Warning: Could not find Style line to modify, using default position")
307
 
308
  with open(ass_path, 'w', encoding='utf-8') as f:
309
+ f.writelines(ass_lines)
310
 
311
  print(f"✅ Subtitle position updated: {subtitle_position}")
312