Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1505,34 +1505,33 @@ async def add_captions_from_srt(request: SrtCaptionRequest):
|
|
| 1505 |
f.write(fc_config)
|
| 1506 |
os.environ['FONTCONFIG_FILE'] = fc_file
|
| 1507 |
|
| 1508 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1509 |
drawtext_pos = {
|
| 1510 |
-
"bottom-left": "x=
|
| 1511 |
-
"bottom-center": "x=(w-tw)/2:y=h-th-
|
| 1512 |
-
"bottom-right": "x=w-tw-
|
| 1513 |
"center": "x=(w-tw)/2:y=(h-th)/2",
|
| 1514 |
-
"left": "x=
|
| 1515 |
-
"right": "x=w-tw-
|
| 1516 |
-
"top-left": "x=
|
| 1517 |
-
"top-center": "x=(w-tw)/2:y=
|
| 1518 |
-
"top-right": "x=w-tw-
|
| 1519 |
}
|
| 1520 |
-
position = drawtext_pos.get(request.caption_style.position, "x=(w-tw)/2:y=h-th-
|
|
|
|
| 1521 |
|
| 1522 |
-
#
|
| 1523 |
-
filter_parts = []
|
| 1524 |
current_input = video_path
|
| 1525 |
-
current_output = os.path.join(work_dir, "temp_output.mp4")
|
| 1526 |
|
| 1527 |
-
# Process each caption with its own drawtext filter
|
| 1528 |
for i, caption in enumerate(captions):
|
| 1529 |
# Create a temporary file for this step
|
| 1530 |
temp_output = os.path.join(work_dir, f"step_{i:03d}.mp4")
|
| 1531 |
|
| 1532 |
-
#
|
| 1533 |
-
duration = caption.end_time - caption.start_time
|
| 1534 |
-
|
| 1535 |
-
# Build drawtext filter for this caption
|
| 1536 |
drawtext_filter = (
|
| 1537 |
f"drawtext=text='{caption.text}':"
|
| 1538 |
f"fontfile={font_path}:"
|
|
@@ -1554,7 +1553,7 @@ async def add_captions_from_srt(request: SrtCaptionRequest):
|
|
| 1554 |
temp_output
|
| 1555 |
]
|
| 1556 |
|
| 1557 |
-
print(f"🎬 Adding caption {i+1}/{len(captions)}: '{caption.text}'")
|
| 1558 |
result = subprocess.run(cmd, capture_output=True, text=True)
|
| 1559 |
|
| 1560 |
if result.returncode != 0:
|
|
|
|
| 1505 |
f.write(fc_config)
|
| 1506 |
os.environ['FONTCONFIG_FILE'] = fc_file
|
| 1507 |
|
| 1508 |
+
# Get margin value from request
|
| 1509 |
+
margin = request.caption_style.margin
|
| 1510 |
+
print(f"📏 Using margin: {margin} pixels")
|
| 1511 |
+
|
| 1512 |
+
# Map position to drawtext position with margin
|
| 1513 |
drawtext_pos = {
|
| 1514 |
+
"bottom-left": f"x={margin}:y=h-th-{margin}",
|
| 1515 |
+
"bottom-center": f"x=(w-tw)/2:y=h-th-{margin}",
|
| 1516 |
+
"bottom-right": f"x=w-tw-{margin}:y=h-th-{margin}",
|
| 1517 |
"center": "x=(w-tw)/2:y=(h-th)/2",
|
| 1518 |
+
"left": f"x={margin}:y=(h-th)/2",
|
| 1519 |
+
"right": f"x=w-tw-{margin}:y=(h-th)/2",
|
| 1520 |
+
"top-left": f"x={margin}:y={margin}",
|
| 1521 |
+
"top-center": f"x=(w-tw)/2:y={margin}",
|
| 1522 |
+
"top-right": f"x=w-tw-{margin}:y={margin}"
|
| 1523 |
}
|
| 1524 |
+
position = drawtext_pos.get(request.caption_style.position, f"x=(w-tw)/2:y=h-th-{margin}")
|
| 1525 |
+
print(f"📍 Position string: {position}")
|
| 1526 |
|
| 1527 |
+
# Process each caption with its own drawtext filter
|
|
|
|
| 1528 |
current_input = video_path
|
|
|
|
| 1529 |
|
|
|
|
| 1530 |
for i, caption in enumerate(captions):
|
| 1531 |
# Create a temporary file for this step
|
| 1532 |
temp_output = os.path.join(work_dir, f"step_{i:03d}.mp4")
|
| 1533 |
|
| 1534 |
+
# Build drawtext filter for this caption with margin
|
|
|
|
|
|
|
|
|
|
| 1535 |
drawtext_filter = (
|
| 1536 |
f"drawtext=text='{caption.text}':"
|
| 1537 |
f"fontfile={font_path}:"
|
|
|
|
| 1553 |
temp_output
|
| 1554 |
]
|
| 1555 |
|
| 1556 |
+
print(f"🎬 Adding caption {i+1}/{len(captions)}: '{caption.text}' at margin {margin}")
|
| 1557 |
result = subprocess.run(cmd, capture_output=True, text=True)
|
| 1558 |
|
| 1559 |
if result.returncode != 0:
|