Tim13ekd commited on
Commit
312fd62
·
verified ·
1 Parent(s): bbb5565

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -14
app.py CHANGED
@@ -100,22 +100,83 @@ def generate_slideshow_with_audio(images, input_text, duration_per_word=0.5, dur
100
  except subprocess.CalledProcessError as e:
101
  return None, f"❌ FFmpeg Concat Fehler:\n{e.stderr}"
102
 
103
- # Text separat anzeigen
104
- # Berechnung der Zeiten für jedes Wort
105
- temp_text_file = Path(temp_dir) / "text.txt"
106
- with open(temp_text_file, "w") as f:
107
- for i, word in enumerate(words):
108
- f.write(f"file '{str(output_file)}'\n")
109
- f.write(f"duration {duration_per_word}\n")
110
- f.write(f"word {word}\n")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
 
112
  # Audio hinzufügen, falls vorhanden
113
  if temp_audio_file:
114
- final_output = Path(temp_dir) / f"slideshow_audio_{uuid.uuid4().hex}.mp4"
115
  cmd_audio = [
116
  "ffmpeg",
117
  "-y",
118
- "-i", str(output_file),
119
  "-i", str(temp_audio_file),
120
  "-c:v", "copy",
121
  "-c:a", "aac",
@@ -124,15 +185,15 @@ def generate_slideshow_with_audio(images, input_text, duration_per_word=0.5, dur
124
  ]
125
  try:
126
  subprocess.run(cmd_audio, check=True, capture_output=True, text=True)
127
- return str(final_output), "✅ Slideshow mit Audio und Text erstellt"
128
  except subprocess.CalledProcessError as e:
129
- return None, f"❌ FFmpeg Audio Merge Fehler:\n{e.stderr}"
130
 
131
- return str(output_file), "✅ Slideshow erstellt (ohne Audio)"
132
 
133
  # Gradio UI
134
  with gr.Blocks() as demo:
135
- gr.Markdown("# Slideshow mit Audio und separatem Text")
136
 
137
  img_input = gr.Files(label="Bilder auswählen (mehrere)", file_types=allowed_medias)
138
  text_input = gr.Textbox(
 
100
  except subprocess.CalledProcessError as e:
101
  return None, f"❌ FFmpeg Concat Fehler:\n{e.stderr}"
102
 
103
+ # Text als Overlay Wort für Wort einfügen
104
+ total_duration = duration_per_image * len(images) # Gesamtzeit des Videos
105
+ word_display_time = duration_per_word # Zeit pro Wort (in Sekunden)
106
+
107
+ clips_with_text = []
108
+ word_index = 0
109
+
110
+ for i, img_path in enumerate(images):
111
+ img_path = Path(img_path.name) # Pfad des Bildes
112
+ clip_path_with_text = Path(temp_dir) / f"clip_with_text_{i}.mp4"
113
+
114
+ # Berechne Start- und Endzeit für jedes Wort
115
+ start_time = i * duration_per_image
116
+ end_time = (i + 1) * duration_per_image
117
+
118
+ # Erstelle Text-Filters für jedes Wort
119
+ if word_index < len(words):
120
+ text = words[word_index]
121
+ word_index += 1
122
+ else:
123
+ text = "" # Falls keine weiteren Wörter, leeres Text
124
+
125
+ vf_filters = (
126
+ "scale=w=1280:h=720:force_original_aspect_ratio=decrease,"
127
+ "pad=1280:720:(ow-iw)/2:(oh-ih)/2:color=black,"
128
+ "fps=25,format=yuv420p"
129
+ f",drawtext=text='{shlex.quote(text)}':fontcolor=white:fontsize={font_size}:borderw=2:"
130
+ f"x=(w-text_w)/2:y=(h-text_h)*{y_pos}:"
131
+ f"alpha='if(lt(t,{fade_duration}), t/{fade_duration}, if(lt(t,{duration_per_image}-{fade_duration}), 1, ({duration_per_image}-t)/{fade_duration}))'"
132
+ )
133
+
134
+ # Bild als Video mit Text Overlay erstellen
135
+ cmd = [
136
+ "ffmpeg",
137
+ "-y",
138
+ "-loop", "1",
139
+ "-i", str(img_path),
140
+ "-t", str(duration_per_image),
141
+ "-vf", vf_filters,
142
+ str(clip_path_with_text)
143
+ ]
144
+ try:
145
+ subprocess.run(cmd, check=True, capture_output=True, text=True)
146
+ except subprocess.CalledProcessError as e:
147
+ return None, f"❌ FFmpeg Fehler bei Text Overlay für Bild {i+1}:\n{e.stderr}"
148
+
149
+ clips_with_text.append(clip_path_with_text)
150
+
151
+ # Zusammenfügen der Clips mit Text
152
+ filelist_with_text_path = Path(temp_dir) / "filelist_with_text.txt"
153
+ with open(filelist_with_text_path, "w") as f:
154
+ for clip in clips_with_text:
155
+ f.write(f"file '{clip}'\n")
156
+
157
+ output_with_text_file = Path(temp_dir) / f"slideshow_with_text_{uuid.uuid4().hex}.mp4"
158
+ cmd_concat_with_text = [
159
+ "ffmpeg",
160
+ "-y",
161
+ "-f", "concat",
162
+ "-safe", "0",
163
+ "-i", str(filelist_with_text_path),
164
+ "-c:v", "libx264",
165
+ "-pix_fmt", "yuv420p",
166
+ str(output_with_text_file)
167
+ ]
168
+ try:
169
+ subprocess.run(cmd_concat_with_text, check=True, capture_output=True, text=True)
170
+ except subprocess.CalledProcessError as e:
171
+ return None, f"❌ FFmpeg Fehler beim Zusammenfügen der Clips mit Text:\n{e.stderr}"
172
 
173
  # Audio hinzufügen, falls vorhanden
174
  if temp_audio_file:
175
+ final_output = Path(temp_dir) / f"slideshow_with_audio_{uuid.uuid4().hex}.mp4"
176
  cmd_audio = [
177
  "ffmpeg",
178
  "-y",
179
+ "-i", str(output_with_text_file),
180
  "-i", str(temp_audio_file),
181
  "-c:v", "copy",
182
  "-c:a", "aac",
 
185
  ]
186
  try:
187
  subprocess.run(cmd_audio, check=True, capture_output=True, text=True)
188
+ return str(final_output), "✅ Slideshow mit Audio und Text Overlay erstellt"
189
  except subprocess.CalledProcessError as e:
190
+ return None, f"❌ FFmpeg Fehler beim Hinzufügen des Audios:\n{e.stderr}"
191
 
192
+ return str(output_with_text_file), "✅ Slideshow mit Text Overlay erstellt (ohne Audio)"
193
 
194
  # Gradio UI
195
  with gr.Blocks() as demo:
196
+ gr.Markdown("# Slideshow mit Audio und Text Overlay")
197
 
198
  img_input = gr.Files(label="Bilder auswählen (mehrere)", file_types=allowed_medias)
199
  text_input = gr.Textbox(