Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,6 @@ from pathlib import Path
|
|
| 4 |
import uuid
|
| 5 |
import subprocess
|
| 6 |
import shutil
|
| 7 |
-
import io # Für NamedString-Handling
|
| 8 |
import shlex # Für sicheres Escapen von Text
|
| 9 |
|
| 10 |
# Erlaubte Dateiformate
|
|
@@ -13,7 +12,7 @@ allowed_audios = [".mp3", ".wav", ".m4a", ".ogg"]
|
|
| 13 |
|
| 14 |
def save_temp_audio(audio_file):
|
| 15 |
"""
|
| 16 |
-
Speichert die hochgeladene Datei sicher
|
| 17 |
"""
|
| 18 |
if isinstance(audio_file, str):
|
| 19 |
ext = Path(audio_file).suffix
|
|
@@ -35,16 +34,7 @@ def save_temp_audio(audio_file):
|
|
| 35 |
else:
|
| 36 |
raise ValueError("Das übergebene Audio ist kein gültiges Dateiformat oder NamedString.")
|
| 37 |
|
| 38 |
-
def
|
| 39 |
-
"""
|
| 40 |
-
Konvertiert das Audio in das WAV-Format, um sicherzustellen, dass FFmpeg es korrekt verarbeitet.
|
| 41 |
-
"""
|
| 42 |
-
wav_path = Path(audio_path).with_suffix(".wav")
|
| 43 |
-
cmd = ["ffmpeg", "-y", "-i", str(audio_path), str(wav_path)]
|
| 44 |
-
subprocess.run(cmd, check=True, capture_output=True, text=True)
|
| 45 |
-
return wav_path
|
| 46 |
-
|
| 47 |
-
def generate_slideshow_with_audio(images, input_text, duration_per_word=0.5, y_pos=0.5, fade_duration=0.7, font_size=60, speed=1.0, audio_file=None):
|
| 48 |
if not images:
|
| 49 |
return None, "❌ Keine Bilder ausgewählt"
|
| 50 |
|
|
@@ -56,12 +46,10 @@ def generate_slideshow_with_audio(images, input_text, duration_per_word=0.5, y_p
|
|
| 56 |
words = input_text.split()
|
| 57 |
total_words = len(words)
|
| 58 |
|
| 59 |
-
#
|
| 60 |
temp_audio_file = None
|
| 61 |
if audio_file:
|
| 62 |
temp_audio_file = save_temp_audio(audio_file)
|
| 63 |
-
# Konvertiere das Audio in WAV, falls es MP3 oder ein anderes Format ist
|
| 64 |
-
temp_audio_file = convert_audio_to_wav(temp_audio_file)
|
| 65 |
|
| 66 |
# Einzelne Clips für jedes Wort erstellen
|
| 67 |
for i, word in enumerate(words):
|
|
@@ -75,6 +63,7 @@ def generate_slideshow_with_audio(images, input_text, duration_per_word=0.5, y_p
|
|
| 75 |
)
|
| 76 |
|
| 77 |
if text:
|
|
|
|
| 78 |
safe_text = shlex.quote(text)
|
| 79 |
drawtext_filter = (
|
| 80 |
f",drawtext=text={safe_text}:fontcolor=white:fontsize={font_size}:borderw=2:"
|
|
@@ -156,7 +145,6 @@ with gr.Blocks() as demo:
|
|
| 156 |
fade_input = gr.Number(value=0.7, label="Fade Dauer in Sekunden", precision=1)
|
| 157 |
ypos_input = gr.Slider(minimum=0.0, maximum=0.9, step=0.01, value=0.5, label="Y-Position für alle Texte (0=oben, 0.5=mitte, 0.9=unten)")
|
| 158 |
font_size_input = gr.Number(value=60, label="Textgröße (px)")
|
| 159 |
-
speed_input = gr.Slider(minimum=0.1, maximum=3.0, value=1.0, label="Geschwindigkeit der Texteinblendung")
|
| 160 |
|
| 161 |
audio_input = gr.File(
|
| 162 |
label="Audio hinzufügen (optional)",
|
|
@@ -169,7 +157,7 @@ with gr.Blocks() as demo:
|
|
| 169 |
btn = gr.Button("Video erstellen")
|
| 170 |
btn.click(
|
| 171 |
fn=generate_slideshow_with_audio,
|
| 172 |
-
inputs=[img_input, text_input, duration_input, ypos_input, fade_input, font_size_input,
|
| 173 |
outputs=[out_video, status]
|
| 174 |
)
|
| 175 |
|
|
|
|
| 4 |
import uuid
|
| 5 |
import subprocess
|
| 6 |
import shutil
|
|
|
|
| 7 |
import shlex # Für sicheres Escapen von Text
|
| 8 |
|
| 9 |
# Erlaubte Dateiformate
|
|
|
|
| 12 |
|
| 13 |
def save_temp_audio(audio_file):
|
| 14 |
"""
|
| 15 |
+
Speichert die hochgeladene Datei sicher in einem temporären Verzeichnis.
|
| 16 |
"""
|
| 17 |
if isinstance(audio_file, str):
|
| 18 |
ext = Path(audio_file).suffix
|
|
|
|
| 34 |
else:
|
| 35 |
raise ValueError("Das übergebene Audio ist kein gültiges Dateiformat oder NamedString.")
|
| 36 |
|
| 37 |
+
def generate_slideshow_with_audio(images, input_text, duration_per_word=0.5, y_pos=0.5, fade_duration=0.7, font_size=60, audio_file=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
if not images:
|
| 39 |
return None, "❌ Keine Bilder ausgewählt"
|
| 40 |
|
|
|
|
| 46 |
words = input_text.split()
|
| 47 |
total_words = len(words)
|
| 48 |
|
| 49 |
+
# Falls Audio vorhanden ist, speichern wir es einfach als temporäre Datei
|
| 50 |
temp_audio_file = None
|
| 51 |
if audio_file:
|
| 52 |
temp_audio_file = save_temp_audio(audio_file)
|
|
|
|
|
|
|
| 53 |
|
| 54 |
# Einzelne Clips für jedes Wort erstellen
|
| 55 |
for i, word in enumerate(words):
|
|
|
|
| 63 |
)
|
| 64 |
|
| 65 |
if text:
|
| 66 |
+
# Sichere Textverarbeitung (Escape von Sonderzeichen)
|
| 67 |
safe_text = shlex.quote(text)
|
| 68 |
drawtext_filter = (
|
| 69 |
f",drawtext=text={safe_text}:fontcolor=white:fontsize={font_size}:borderw=2:"
|
|
|
|
| 145 |
fade_input = gr.Number(value=0.7, label="Fade Dauer in Sekunden", precision=1)
|
| 146 |
ypos_input = gr.Slider(minimum=0.0, maximum=0.9, step=0.01, value=0.5, label="Y-Position für alle Texte (0=oben, 0.5=mitte, 0.9=unten)")
|
| 147 |
font_size_input = gr.Number(value=60, label="Textgröße (px)")
|
|
|
|
| 148 |
|
| 149 |
audio_input = gr.File(
|
| 150 |
label="Audio hinzufügen (optional)",
|
|
|
|
| 157 |
btn = gr.Button("Video erstellen")
|
| 158 |
btn.click(
|
| 159 |
fn=generate_slideshow_with_audio,
|
| 160 |
+
inputs=[img_input, text_input, duration_input, ypos_input, fade_input, font_size_input, audio_input],
|
| 161 |
outputs=[out_video, status]
|
| 162 |
)
|
| 163 |
|