Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,38 +6,35 @@ import subprocess
|
|
| 6 |
|
| 7 |
allowed_medias = [".png", ".jpg", ".jpeg", ".bmp", ".gif", ".tiff"]
|
| 8 |
|
| 9 |
-
def
|
| 10 |
"""
|
| 11 |
-
Slideshow
|
| 12 |
-
|
| 13 |
-
|
| 14 |
"""
|
| 15 |
if not images:
|
| 16 |
return None, "❌ Keine Bilder ausgewählt"
|
| 17 |
|
|
|
|
| 18 |
temp_dir = tempfile.mkdtemp()
|
| 19 |
clips = []
|
| 20 |
|
| 21 |
-
# Standardwerte
|
| 22 |
if texts is None:
|
| 23 |
texts = [""] * len(images)
|
| 24 |
-
if y_positions is None:
|
| 25 |
-
y_positions = [0.5] * len(images)
|
| 26 |
|
| 27 |
for i, img_path in enumerate(images):
|
| 28 |
clip_path = Path(temp_dir) / f"clip_{i}.mp4"
|
| 29 |
text = texts[i] if i < len(texts) else ""
|
| 30 |
-
y_pos = y_positions[i] if i < len(y_positions) else 0.5
|
| 31 |
|
| 32 |
drawtext = ""
|
| 33 |
if text:
|
| 34 |
-
# Smooth Fade-In / Fade-Out, Y-Position dynamisch
|
| 35 |
drawtext = (
|
| 36 |
-
f"drawtext=text='{text}':fontcolor=white:fontsize=
|
| 37 |
f"x=(w-text_w)/2:y=(h-text_h)*{y_pos}:"
|
| 38 |
f"alpha='if(lt(t,{fade_duration}), t/{fade_duration}, if(lt(t,{duration}-{fade_duration}), 1, ({duration}-t)/{fade_duration}))'"
|
| 39 |
)
|
| 40 |
|
|
|
|
| 41 |
cmd = [
|
| 42 |
"ffmpeg",
|
| 43 |
"-y",
|
|
@@ -45,7 +42,7 @@ def generate_slideshow_with_text_ypos(images, duration, texts=None, y_positions=
|
|
| 45 |
"-i", str(img_path),
|
| 46 |
"-t", str(duration),
|
| 47 |
"-vf",
|
| 48 |
-
f"scale=
|
| 49 |
str(clip_path)
|
| 50 |
]
|
| 51 |
subprocess.run(cmd, check=True)
|
|
@@ -72,31 +69,32 @@ def generate_slideshow_with_text_ypos(images, duration, texts=None, y_positions=
|
|
| 72 |
subprocess.run(cmd_concat, check=True)
|
| 73 |
|
| 74 |
if output_file.exists():
|
| 75 |
-
return str(output_file), f"✅ Slideshow erstellt ({len(images)} Bilder
|
| 76 |
else:
|
| 77 |
return None, "❌ Video konnte nicht erstellt werden"
|
| 78 |
|
|
|
|
| 79 |
# Gradio UI
|
| 80 |
with gr.Blocks() as demo:
|
| 81 |
-
gr.Markdown("# Slideshow mit Text
|
| 82 |
|
| 83 |
img_input = gr.Files(label="Bilder auswählen (mehrere)", file_types=allowed_medias)
|
| 84 |
duration_input = gr.Number(value=3, label="Dauer pro Bild in Sekunden", precision=1)
|
| 85 |
fade_input = gr.Number(value=0.7, label="Fade Dauer in Sekunden", precision=1)
|
| 86 |
text_input = gr.Textbox(label="Texte pro Bild (mit Komma trennen)", placeholder="Text1, Text2, Text3 ...")
|
| 87 |
-
ypos_input = gr.
|
|
|
|
| 88 |
out_video = gr.Video(interactive=False, label="Generiertes Video")
|
| 89 |
status = gr.Textbox(interactive=False, label="Status")
|
| 90 |
|
| 91 |
-
def wrapper(images, duration, fade_duration, text_str,
|
| 92 |
texts = [t.strip() for t in text_str.split(",")] if text_str else None
|
| 93 |
-
|
| 94 |
-
return generate_slideshow_with_text_ypos(images, duration, texts, y_positions, fade_duration)
|
| 95 |
|
| 96 |
btn = gr.Button("Video erstellen")
|
| 97 |
btn.click(
|
| 98 |
fn=wrapper,
|
| 99 |
-
inputs=[img_input, duration_input, fade_input, text_input, ypos_input],
|
| 100 |
outputs=[out_video, status]
|
| 101 |
)
|
| 102 |
|
|
|
|
| 6 |
|
| 7 |
allowed_medias = [".png", ".jpg", ".jpeg", ".bmp", ".gif", ".tiff"]
|
| 8 |
|
| 9 |
+
def generate_slideshow_with_fixed_text_and_padding(images, duration, texts=None, y_pos=0.5, fade_duration=0.7, font_size=60):
|
| 10 |
"""
|
| 11 |
+
Slideshow:
|
| 12 |
+
- Bilder behalten ihre Proportionen, schwarze Balken falls nötig
|
| 13 |
+
- Text Overlay mit Y-Position, Fade-In/Out und fester Größe
|
| 14 |
"""
|
| 15 |
if not images:
|
| 16 |
return None, "❌ Keine Bilder ausgewählt"
|
| 17 |
|
| 18 |
+
y_pos = min(max(0.0, y_pos), 0.9) # Max 0.9 für unten
|
| 19 |
temp_dir = tempfile.mkdtemp()
|
| 20 |
clips = []
|
| 21 |
|
|
|
|
| 22 |
if texts is None:
|
| 23 |
texts = [""] * len(images)
|
|
|
|
|
|
|
| 24 |
|
| 25 |
for i, img_path in enumerate(images):
|
| 26 |
clip_path = Path(temp_dir) / f"clip_{i}.mp4"
|
| 27 |
text = texts[i] if i < len(texts) else ""
|
|
|
|
| 28 |
|
| 29 |
drawtext = ""
|
| 30 |
if text:
|
|
|
|
| 31 |
drawtext = (
|
| 32 |
+
f"drawtext=text='{text}':fontcolor=white:fontsize={font_size}:borderw=2:"
|
| 33 |
f"x=(w-text_w)/2:y=(h-text_h)*{y_pos}:"
|
| 34 |
f"alpha='if(lt(t,{fade_duration}), t/{fade_duration}, if(lt(t,{duration}-{fade_duration}), 1, ({duration}-t)/{fade_duration}))'"
|
| 35 |
)
|
| 36 |
|
| 37 |
+
# FFmpeg scale + pad für schwarzen Rand
|
| 38 |
cmd = [
|
| 39 |
"ffmpeg",
|
| 40 |
"-y",
|
|
|
|
| 42 |
"-i", str(img_path),
|
| 43 |
"-t", str(duration),
|
| 44 |
"-vf",
|
| 45 |
+
f"scale='min(1280,iw)':-2,pad=1280:720:(1280-iw)/2:(720-ih)/2:color=black,fps=25,format=yuv420p{','+drawtext if drawtext else ''}",
|
| 46 |
str(clip_path)
|
| 47 |
]
|
| 48 |
subprocess.run(cmd, check=True)
|
|
|
|
| 69 |
subprocess.run(cmd_concat, check=True)
|
| 70 |
|
| 71 |
if output_file.exists():
|
| 72 |
+
return str(output_file), f"✅ Slideshow erstellt ({len(images)} Bilder, Text fixiert, Bildproportionen erhalten)"
|
| 73 |
else:
|
| 74 |
return None, "❌ Video konnte nicht erstellt werden"
|
| 75 |
|
| 76 |
+
|
| 77 |
# Gradio UI
|
| 78 |
with gr.Blocks() as demo:
|
| 79 |
+
gr.Markdown("# Slideshow mit Text-Overlay & schwarzem Rand")
|
| 80 |
|
| 81 |
img_input = gr.Files(label="Bilder auswählen (mehrere)", file_types=allowed_medias)
|
| 82 |
duration_input = gr.Number(value=3, label="Dauer pro Bild in Sekunden", precision=1)
|
| 83 |
fade_input = gr.Number(value=0.7, label="Fade Dauer in Sekunden", precision=1)
|
| 84 |
text_input = gr.Textbox(label="Texte pro Bild (mit Komma trennen)", placeholder="Text1, Text2, Text3 ...")
|
| 85 |
+
ypos_input = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, value=0.5, label="Y-Position für alle Texte (0=oben, 0.5=mitte, 1=unten, max 0.9)")
|
| 86 |
+
font_size_input = gr.Number(value=60, label="Textgröße (px)")
|
| 87 |
out_video = gr.Video(interactive=False, label="Generiertes Video")
|
| 88 |
status = gr.Textbox(interactive=False, label="Status")
|
| 89 |
|
| 90 |
+
def wrapper(images, duration, fade_duration, text_str, y_pos, font_size):
|
| 91 |
texts = [t.strip() for t in text_str.split(",")] if text_str else None
|
| 92 |
+
return generate_slideshow_with_fixed_text_and_padding(images, duration, texts, y_pos, fade_duration, font_size)
|
|
|
|
| 93 |
|
| 94 |
btn = gr.Button("Video erstellen")
|
| 95 |
btn.click(
|
| 96 |
fn=wrapper,
|
| 97 |
+
inputs=[img_input, duration_input, fade_input, text_input, ypos_input, font_size_input],
|
| 98 |
outputs=[out_video, status]
|
| 99 |
)
|
| 100 |
|