spajicdaniel commited on
Commit
85bbc44
·
verified ·
1 Parent(s): 90d642f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -73
app.py CHANGED
@@ -1,105 +1,92 @@
1
  import gradio as gr
2
- from moviepy.editor import ImageClip, concatenate_videoclips, TextClip, CompositeVideoClip
3
  from PIL import Image
4
  import requests
5
  from io import BytesIO
 
 
 
6
 
7
- # Datenbank: Horror-Bilder (Pixabay, frei verwendbar)
8
  IMAGE_DB = {
9
  "haus": [
10
- "https://cdn.pixabay.com/photo/2016/11/29/05/15/haunted-house-1867832_1280.jpg",
11
- "https://cdn.pixabay.com/photo/2016/11/18/22/15/old-house-1836494_1280.jpg"
12
  ],
13
  "geist": [
14
- "https://cdn.pixabay.com/photo/2017/09/02/13/17/ghost-2709306_1280.jpg",
15
- "https://cdn.pixabay.com/photo/2018/10/18/22/48/halloween-3751882_1280.jpg"
16
  ],
17
  "spuk": [
18
- "https://cdn.pixabay.com/photo/2016/11/21/12/04/spooky-1846021_1280.jpg",
19
- "https://cdn.pixabay.com/photo/2017/11/20/00/35/abandoned-2963828_1280.jpg"
20
  ],
21
  "katze": [
22
- "https://cdn.pixabay.com/photo/2016/11/29/03/53/cat-1867274_1280.jpg"
23
  ],
24
  "ritual": [
25
- "https://cdn.pixabay.com/photo/2018/10/31/21/59/halloween-3783701_1280.jpg"
26
  ],
27
  }
28
 
29
- # Bildauswahl nach Titel
30
- def select_images(title):
31
- title_lower = title.lower()
32
- selected_urls = []
33
- for keyword, urls in IMAGE_DB.items():
34
- if keyword in title_lower:
35
- selected_urls.extend(urls)
36
- if not selected_urls:
37
- for urls in IMAGE_DB.values():
38
- selected_urls.extend(urls)
39
- return selected_urls[:6]
 
 
 
 
40
 
41
- # Sicheres Bild-Download mit Prüfung
42
  def download_image(url):
43
  try:
44
- response = requests.get(url, timeout=10)
45
- if response.status_code == 200 and 'image' in response.headers.get("Content-Type", ""):
46
- img = Image.open(BytesIO(response.content)).convert("RGB")
47
- return img
48
- else:
49
- print(f"WARNUNG: Ungültiges Bild: {url}")
50
- return None
51
  except Exception as e:
52
- print(f"FEHLER beim Bild-Download: {url} — {str(e)}")
53
  return None
54
 
55
- # Video erstellen aus Titel & Bildern
56
  def create_video(title):
57
- image_urls = select_images(title)
58
- clips = []
59
- duration_per_image = 10 # Sekunden pro Bild
60
-
61
- for i, url in enumerate(image_urls):
62
  img = download_image(url)
63
- if img is None:
64
- continue
65
-
66
- img_clip = ImageClip(img).set_duration(duration_per_image)
67
-
68
- # Text-Overlay
69
- txt = TextClip(
70
- f"{title}\nSzene {i+1}",
71
- fontsize=40,
72
- color='white',
73
- font='Amiri-Bold',
74
- method='caption',
75
- size=img.size
76
- )
77
- txt = txt.set_duration(duration_per_image).set_pos('bottom').margin(bottom=30, opacity=0)
78
-
79
- video_clip = CompositeVideoClip([img_clip, txt])
80
- clips.append(video_clip)
81
-
82
- if not clips:
83
  raise RuntimeError("Keine gültigen Bilder gefunden. Bitte gib einen anderen Titel ein.")
84
 
85
- final_clip = concatenate_videoclips(clips, method="compose")
86
- out_path = "output.mp4"
87
- final_clip.write_videofile(out_path, fps=24)
88
- return out_path
 
 
 
 
 
 
89
 
90
- # Gradio-Funktion
91
  def run(title):
92
- video_path = create_video(title)
93
- return video_path
 
 
 
 
 
 
 
 
 
94
 
95
- # Gradio-UI
96
- iface = gr.Interface(
97
- fn=run,
98
- inputs=gr.Textbox(label="🎬 Gib den Titel deines Horror-Videos ein"),
99
- outputs=gr.Video(label="👻 Generiertes Horror-Video"),
100
- title="Creepy Horror Shorts Generator (kostenfrei)",
101
- description="Gib einen gruseligen Titel ein – und erhalte automatisch ein Horror-Video aus freien Bildern.",
102
- )
103
 
104
- if __name__ == "__main__":
105
- iface.launch(share=True)
 
1
  import gradio as gr
 
2
  from PIL import Image
3
  import requests
4
  from io import BytesIO
5
+ from moviepy.editor import ImageClip, concatenate_videoclips
6
+ import random
7
+ import os
8
 
9
+ # Sichere, getestete Horror-Bilder (GitHub gehostet)
10
  IMAGE_DB = {
11
  "haus": [
12
+ "https://raw.githubusercontent.com/chrispawluk/horror-img/main/haunted_house_1.jpg",
13
+ "https://raw.githubusercontent.com/chrispawluk/horror-img/main/haunted_house_2.jpg"
14
  ],
15
  "geist": [
16
+ "https://raw.githubusercontent.com/chrispawluk/horror-img/main/ghost_1.jpg",
17
+ "https://raw.githubusercontent.com/chrispawluk/horror-img/main/ghost_2.jpg"
18
  ],
19
  "spuk": [
20
+ "https://raw.githubusercontent.com/chrispawluk/horror-img/main/spooky_forest.jpg",
21
+ "https://raw.githubusercontent.com/chrispawluk/horror-img/main/abandoned_building.jpg"
22
  ],
23
  "katze": [
24
+ "https://raw.githubusercontent.com/chrispawluk/horror-img/main/black_cat.jpg"
25
  ],
26
  "ritual": [
27
+ "https://raw.githubusercontent.com/chrispawluk/horror-img/main/ritual_circle.jpg"
28
  ],
29
  }
30
 
31
+ def find_images_for_title(title):
32
+ # Suche passende Bilder basierend auf Keywords im Titel
33
+ keywords = IMAGE_DB.keys()
34
+ selected_images = []
35
+ for keyword in keywords:
36
+ if keyword in title.lower():
37
+ selected_images.extend(IMAGE_DB[keyword])
38
+ # Falls keine Keywords gefunden, zufällige Bilder
39
+ if not selected_images:
40
+ # alle Bilder aus allen Kategorien sammeln
41
+ all_images = sum(IMAGE_DB.values(), [])
42
+ selected_images = random.sample(all_images, min(5, len(all_images)))
43
+ else:
44
+ selected_images = selected_images[:5] # max 5 Bilder
45
+ return selected_images
46
 
 
47
  def download_image(url):
48
  try:
49
+ response = requests.get(url)
50
+ img = Image.open(BytesIO(response.content)).convert("RGB")
51
+ return img
 
 
 
 
52
  except Exception as e:
53
+ print(f"WARNUNG: Ungültiges Bild: {url}")
54
  return None
55
 
 
56
  def create_video(title):
57
+ image_urls = find_images_for_title(title)
58
+ images = []
59
+ for url in image_urls:
 
 
60
  img = download_image(url)
61
+ if img:
62
+ images.append(img)
63
+ if not images:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  raise RuntimeError("Keine gültigen Bilder gefunden. Bitte gib einen anderen Titel ein.")
65
 
66
+ clips = []
67
+ duration_per_image = 12 # 5 Bilder x 12s = 60s Video
68
+ for img in images:
69
+ clip = ImageClip(img).set_duration(duration_per_image)
70
+ clips.append(clip)
71
+
72
+ video = concatenate_videoclips(clips, method="compose")
73
+ output_path = "output.mp4"
74
+ video.write_videofile(output_path, fps=24, codec="libx264", audio=False)
75
+ return output_path
76
 
 
77
  def run(title):
78
+ try:
79
+ video_path = create_video(title)
80
+ return video_path
81
+ except Exception as e:
82
+ return str(e)
83
+
84
+ with gr.Blocks() as demo:
85
+ gr.Markdown("# Creepy Horror Movie Shorts Generator")
86
+ title_input = gr.Textbox(label="Gib den Titel oder die Idee des Videos ein", placeholder="z.B. Spuk im alten Haus")
87
+ submit_btn = gr.Button("Submit")
88
+ video_output = gr.Video(label="Dein Horror-Video")
89
 
90
+ submit_btn.click(run, inputs=title_input, outputs=video_output)
 
 
 
 
 
 
 
91
 
92
+ demo.launch(share=True)