Tim13ekd commited on
Commit
77f3dab
·
verified ·
1 Parent(s): 8320d85

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -16
app.py CHANGED
@@ -45,12 +45,11 @@ def save_temp_audio(audio_file):
45
  return temp_audio
46
  return None
47
 
48
- # KORRIGIERTE HILFSFUNKTION für den Drawtext-Filter
49
  def create_timed_drawtext(word, start_time, duration, font_option, font_size, y_pos):
50
  """Erstellt einen FFmpeg drawtext Filter, der ein Wort für eine bestimmte Zeit einblendet."""
51
- global FFMPEG_ESCAPE_CHAR # Verwenden der globalen Konstante
52
 
53
- # 1. Escaping: Ersetze alle ":" durch "\:" für FFmpeg, um den F-String-Fehler zu umgehen
54
  escaped_word = word.replace(':', f"{FFMPEG_ESCAPE_CHAR}:")
55
 
56
  # Definiere die Start- und Endzeit des Wortes
@@ -90,23 +89,24 @@ def generate_slideshow_with_audio(images, input_text, duration_per_word, duratio
90
  if audio_file:
91
  temp_audio_file = save_temp_audio(audio_file)
92
 
93
- # 1. ERSTES BILD: Hier wird die sequenzielle Textanzeige angewendet
94
 
95
  # Dauer des ersten Clips ist mindestens so lang wie der Text
96
  duration_clip_1 = max(duration_per_image, total_text_duration)
97
 
 
 
 
 
 
98
  # Generiere die sequentiellen Drawtext-Filter
99
  drawtext_filters = []
100
  current_time = 0.0
101
  for word in words:
102
- # Erstelle den Filter für das aktuelle Wort
103
  filter_str = create_timed_drawtext(word, current_time, duration_per_word, font_option, font_size, y_pos)
104
  drawtext_filters.append(filter_str)
105
- # Nächste Startzeit erhöhen
106
  current_time += duration_per_word
107
 
108
- # FÜGE ALLE FILTER ZUSAMMEN: scale, pad, fps, format, [Text-Filter], fade
109
-
110
  # 1. Basisanpassungen
111
  base_filters = (
112
  "scale=w=1280:h=720:force_original_aspect_ratio=decrease,"
@@ -114,18 +114,17 @@ def generate_slideshow_with_audio(images, input_text, duration_per_word, duratio
114
  "fps=25,format=yuv420p"
115
  )
116
 
117
- # 2. Sequentielle Textfilter (Komma-separiert)
118
- all_drawtext_filters = ",".join(drawtext_filters)
119
 
120
- # 3. Fade-Filter: Muss der letzte in der Kette sein
121
- fade_img_filter_1 = f"fade=t=in:st=0:d={fade_duration},fade=t=out:st={duration_clip_1}-{fade_duration}:d={fade_duration}"
122
 
123
  if drawtext_filters:
 
124
  vf_filters_clip1 = f"{base_filters},{all_drawtext_filters},{fade_img_filter_1}"
125
  else:
126
  vf_filters_clip1 = f"{base_filters},{fade_img_filter_1}"
127
 
128
- # Erstelle Clip 1 (mit Text)
129
  img_path_1 = Path(images[0].name)
130
  clip_path_1 = Path(temp_dir) / "clip_with_text_0.mp4"
131
 
@@ -142,13 +141,17 @@ def generate_slideshow_with_audio(images, input_text, duration_per_word, duratio
142
  except subprocess.CalledProcessError as e:
143
  return None, f"❌ FFmpeg Fehler bei Bild 1 (mit Text):\n{e.stderr}"
144
 
145
- # 2. FOLGE-BILDER: Nur Bild mit Fade
146
  for i in range(1, len(images)):
147
  img_path = Path(images[i].name)
148
  clip_path = Path(temp_dir) / f"clip_{i}.mp4"
149
 
150
- # Nur Bild-Filter mit Fade
151
- fade_img_filter = f"fade=t=in:st=0:d={fade_duration},fade=t=out:st={duration_per_image}-{fade_duration}:d={fade_duration}"
 
 
 
 
152
  vf_filters_clip = (
153
  "scale=w=1280:h=720:force_original_aspect_ratio=decrease,"
154
  "pad=1280:720:(ow-iw)/2:(oh-ih)/2:color=black,"
 
45
  return temp_audio
46
  return None
47
 
 
48
  def create_timed_drawtext(word, start_time, duration, font_option, font_size, y_pos):
49
  """Erstellt einen FFmpeg drawtext Filter, der ein Wort für eine bestimmte Zeit einblendet."""
50
+ global FFMPEG_ESCAPE_CHAR
51
 
52
+ # 1. Escaping: Ersetze alle ":" durch "\:" für FFmpeg
53
  escaped_word = word.replace(':', f"{FFMPEG_ESCAPE_CHAR}:")
54
 
55
  # Definiere die Start- und Endzeit des Wortes
 
89
  if audio_file:
90
  temp_audio_file = save_temp_audio(audio_file)
91
 
92
+ # --- 1. ERSTES BILD (Sequenzieller Text) ---
93
 
94
  # Dauer des ersten Clips ist mindestens so lang wie der Text
95
  duration_clip_1 = max(duration_per_image, total_text_duration)
96
 
97
+ # **KORREKTUR für FFmpeg:** Fade-Out Startzeit in Python berechnen
98
+ fade_out_start_1 = duration_clip_1 - fade_duration
99
+ if fade_out_start_1 < 0: fade_out_start_1 = 0
100
+
101
+
102
  # Generiere die sequentiellen Drawtext-Filter
103
  drawtext_filters = []
104
  current_time = 0.0
105
  for word in words:
 
106
  filter_str = create_timed_drawtext(word, current_time, duration_per_word, font_option, font_size, y_pos)
107
  drawtext_filters.append(filter_str)
 
108
  current_time += duration_per_word
109
 
 
 
110
  # 1. Basisanpassungen
111
  base_filters = (
112
  "scale=w=1280:h=720:force_original_aspect_ratio=decrease,"
 
114
  "fps=25,format=yuv420p"
115
  )
116
 
117
+ # 2. Fade-Filter (jetzt mit korrekter berechneter Startzeit)
118
+ fade_img_filter_1 = f"fade=t=in:st=0:d={fade_duration},fade=t=out:st={fade_out_start_1}:d={fade_duration}"
119
 
 
 
120
 
121
  if drawtext_filters:
122
+ all_drawtext_filters = ",".join(drawtext_filters)
123
  vf_filters_clip1 = f"{base_filters},{all_drawtext_filters},{fade_img_filter_1}"
124
  else:
125
  vf_filters_clip1 = f"{base_filters},{fade_img_filter_1}"
126
 
127
+ # Erstelle Clip 1
128
  img_path_1 = Path(images[0].name)
129
  clip_path_1 = Path(temp_dir) / "clip_with_text_0.mp4"
130
 
 
141
  except subprocess.CalledProcessError as e:
142
  return None, f"❌ FFmpeg Fehler bei Bild 1 (mit Text):\n{e.stderr}"
143
 
144
+ # --- 2. FOLGE-BILDER (Nur Bild mit Fade) ---
145
  for i in range(1, len(images)):
146
  img_path = Path(images[i].name)
147
  clip_path = Path(temp_dir) / f"clip_{i}.mp4"
148
 
149
+ # **KORREKTUR für FFmpeg:** Fade-Out Startzeit in Python berechnen
150
+ fade_out_start_n = duration_per_image - fade_duration
151
+ if fade_out_start_n < 0: fade_out_start_n = 0
152
+
153
+ # Nur Bild-Filter mit Fade (jetzt mit korrekter berechneter Startzeit)
154
+ fade_img_filter = f"fade=t=in:st=0:d={fade_duration},fade=t=out:st={fade_out_start_n}:d={fade_duration}"
155
  vf_filters_clip = (
156
  "scale=w=1280:h=720:force_original_aspect_ratio=decrease,"
157
  "pad=1280:720:(ow-iw)/2:(oh-ih)/2:color=black,"