yukee1992 commited on
Commit
7f0441a
Β·
verified Β·
1 Parent(s): 1b06761

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -7
app.py CHANGED
@@ -221,7 +221,7 @@ def get_font_path(font_family):
221
  # =============================================
222
 
223
  def create_text_overlay(input_video, output_video, text_style):
224
- """Add text overlay using ASS subtitles - FIXED for subfolders"""
225
  font_path = get_font_path(text_style.font_family)
226
  if not font_path:
227
  print(f"⚠️ Font not found: {text_style.font_family}")
@@ -248,7 +248,7 @@ def create_text_overlay(input_video, output_video, text_style):
248
  bg_color_name = bg_parts[0]
249
  bg_opacity = float(bg_parts[1]) if len(bg_parts) > 1 else 0.5
250
  bg_color_hex = color_map.get(bg_color_name.lower(), "000000")
251
- bg_alpha = int((1 - bg_opacity) * 255)
252
 
253
  # Map position to ASS alignment
254
  pos_map = {
@@ -263,10 +263,37 @@ def create_text_overlay(input_video, output_video, text_style):
263
  margin_r = text_style.margin if alignment in [3,6,9] else 0
264
  margin_v = text_style.margin
265
 
266
- # Get just the filename without extension and path
267
- font_name = os.path.basename(font_path).split('.')[0]
 
268
 
269
- # Create ASS file content with proper font path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
270
  ass_content = f"""[Script Info]
271
  ; Script generated by Video Styling Space
272
  ScriptType: v4.00+
@@ -287,8 +314,13 @@ Dialogue: 0,0:00:00.00,0:00:10.00,Default,,0,0,0,,{text_style.text}"""
287
  f.write(ass_content)
288
 
289
  print(f"πŸ“ Created ASS subtitle file")
 
290
 
291
- # Use FFmpeg to burn subtitles
 
 
 
 
292
  cmd = [
293
  'ffmpeg', '-y',
294
  '-i', input_video,
@@ -302,7 +334,37 @@ Dialogue: 0,0:00:00.00,0:00:10.00,Default,,0,0,0,,{text_style.text}"""
302
 
303
  if result.returncode != 0:
304
  print(f"❌ FFmpeg error: {result.stderr}")
305
- return False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
306
 
307
  print(f"βœ… Video styled successfully with ASS subtitles")
308
  return True
 
221
  # =============================================
222
 
223
  def create_text_overlay(input_video, output_video, text_style):
224
+ """Add text overlay using ASS subtitles - FIXED with full font path"""
225
  font_path = get_font_path(text_style.font_family)
226
  if not font_path:
227
  print(f"⚠️ Font not found: {text_style.font_family}")
 
248
  bg_color_name = bg_parts[0]
249
  bg_opacity = float(bg_parts[1]) if len(bg_parts) > 1 else 0.5
250
  bg_color_hex = color_map.get(bg_color_name.lower(), "000000")
251
+ bg_alpha = int((1 - bg_opacity) * 255) # ASS opacity
252
 
253
  # Map position to ASS alignment
254
  pos_map = {
 
263
  margin_r = text_style.margin if alignment in [3,6,9] else 0
264
  margin_v = text_style.margin
265
 
266
+ # IMPORTANT: For ASS, we need to use the actual font filename
267
+ # But FFmpeg needs the font to be accessible via fontconfig
268
+ # Let's create a fontconfig configuration file
269
 
270
+ font_dir = os.path.dirname(font_path)
271
+ font_file = os.path.basename(font_path)
272
+ font_name = os.path.splitext(font_file)[0]
273
+
274
+ # Create a fontconfig configuration file
275
+ fc_config = f"""<?xml version="1.0"?>
276
+ <!DOCTYPE fontconfig SYSTEM "fonts.dtd">
277
+ <fontconfig>
278
+ <dir>{font_dir}</dir>
279
+ <match target="pattern">
280
+ <test qual="any" name="family">
281
+ <string>{font_name}</string>
282
+ </test>
283
+ <edit name="family" mode="assign" binding="same">
284
+ <string>{font_name}</string>
285
+ </edit>
286
+ </match>
287
+ </fontconfig>"""
288
+
289
+ fc_file = os.path.join(work_dir, "fonts.conf")
290
+ with open(fc_file, 'w') as f:
291
+ f.write(fc_config)
292
+
293
+ # Set environment variable for fontconfig
294
+ os.environ['FONTCONFIG_FILE'] = fc_file
295
+
296
+ # Create ASS file content
297
  ass_content = f"""[Script Info]
298
  ; Script generated by Video Styling Space
299
  ScriptType: v4.00+
 
314
  f.write(ass_content)
315
 
316
  print(f"πŸ“ Created ASS subtitle file")
317
+ print(f"πŸ“ Fontconfig file created at: {fc_file}")
318
 
319
+ # First, test if the font is recognized by fontconfig
320
+ fc_list = subprocess.run(['fc-list', ':' + font_name], capture_output=True, text=True)
321
+ print(f"πŸ“‹ Fontconfig lookup: {fc_list.stdout}")
322
+
323
+ # Use FFmpeg with the ASS file
324
  cmd = [
325
  'ffmpeg', '-y',
326
  '-i', input_video,
 
334
 
335
  if result.returncode != 0:
336
  print(f"❌ FFmpeg error: {result.stderr}")
337
+
338
+ # Try alternative: use drawtext with direct font path
339
+ print("πŸ”„ Trying alternative drawtext method...")
340
+
341
+ # Position mapping for drawtext
342
+ drawtext_pos = {
343
+ "bottom-left": "x=20:y=h-th-20",
344
+ "bottom-center": "x=(w-tw)/2:y=h-th-20",
345
+ "bottom-right": "x=w-tw-20:y=h-th-20",
346
+ "center": "x=(w-tw)/2:y=(h-th)/2",
347
+ "top-left": "x=20:y=20",
348
+ "top-center": "x=(w-tw)/2:y=20",
349
+ "top-right": "x=w-tw-20:y=20"
350
+ }
351
+ position = drawtext_pos.get(text_style.position, "x=(w-tw)/2:y=(h-th)/2")
352
+
353
+ drawtext_cmd = [
354
+ 'ffmpeg', '-y',
355
+ '-i', input_video,
356
+ '-vf', f"drawtext=text='{text_style.text}':fontfile={font_path}:fontsize={text_style.font_size}:fontcolor={text_style.color}:{position}:box=1:boxcolor={bg_color_name}@{bg_opacity}:boxborderw={text_style.padding}",
357
+ '-c:a', 'copy',
358
+ output_video
359
+ ]
360
+
361
+ result2 = subprocess.run(drawtext_cmd, capture_output=True, text=True)
362
+ if result2.returncode == 0:
363
+ print(f"βœ… Alternative drawtext method succeeded")
364
+ return True
365
+ else:
366
+ print(f"❌ Alternative drawtext also failed: {result2.stderr}")
367
+ return False
368
 
369
  print(f"βœ… Video styled successfully with ASS subtitles")
370
  return True