Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,67 +1,53 @@
|
|
1 |
-
from fastapi import FastAPI, UploadFile, File
|
2 |
-
from
|
3 |
-
from moviepy.editor import *
|
4 |
from gtts import gTTS
|
5 |
-
import
|
6 |
|
7 |
app = FastAPI()
|
8 |
|
9 |
-
@app.post("/
|
10 |
-
async def
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
)
|
16 |
-
uid = str(uuid.uuid4())
|
17 |
-
temp_dir = f"temp/{uid}"
|
18 |
-
os.makedirs(temp_dir, exist_ok=True)
|
19 |
|
20 |
-
#
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
with open(img_path, "wb") as f:
|
25 |
-
shutil.copyfileobj(img.file, f)
|
26 |
-
image_paths.append(img_path)
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
shutil.copyfileobj(logo.file, f)
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
|
36 |
-
#
|
37 |
-
|
38 |
-
|
39 |
-
tts.save(narration_path)
|
40 |
|
41 |
-
#
|
42 |
-
|
43 |
-
for path in image_paths:
|
44 |
-
img_clip = ImageClip(path).set_duration(3).resize(width=720)
|
45 |
-
img_clip = img_clip.fx(vfx.zoom_in, 1.1) # Simple Ken Burns
|
46 |
-
clips.append(img_clip)
|
47 |
|
48 |
-
|
|
|
|
|
|
|
49 |
|
50 |
-
#
|
51 |
-
|
52 |
-
|
53 |
-
.resize(height=80)
|
54 |
-
.set_pos(("right", "top")))
|
55 |
-
final = CompositeVideoClip([video, logo_clip])
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
bg_music = AudioFileClip(music_path).volumex(0.1)
|
60 |
-
audio = CompositeAudioClip([bg_music.set_duration(final.duration), narration_audio.set_start(0)])
|
61 |
-
final = final.set_audio(audio)
|
62 |
|
63 |
-
|
64 |
-
output_path = f"{temp_dir}/final_video.mp4"
|
65 |
-
final.write_videofile(output_path, fps=24, codec="libx264", audio_codec="aac")
|
66 |
|
67 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, UploadFile, File
|
2 |
+
from moviepy.editor import ImageClip, AudioFileClip, CompositeVideoClip, TextClip
|
|
|
3 |
from gtts import gTTS
|
4 |
+
import os
|
5 |
|
6 |
app = FastAPI()
|
7 |
|
8 |
+
@app.post("/create-video/")
|
9 |
+
async def create_video(image: UploadFile = File(...), caption: str = "مرحبا بك في الفيديو", logo: UploadFile = File(...)):
|
10 |
+
# Save uploaded files
|
11 |
+
image_path = "input.jpg"
|
12 |
+
logo_path = "logo.png"
|
13 |
+
await save_file(image, image_path)
|
14 |
+
await save_file(logo, logo_path)
|
|
|
|
|
|
|
15 |
|
16 |
+
# Step 1: Create audio from caption
|
17 |
+
tts = gTTS(text=caption, lang="ar")
|
18 |
+
tts_path = "voice.mp3"
|
19 |
+
tts.save(tts_path)
|
|
|
|
|
|
|
20 |
|
21 |
+
# Step 2: Background music (replace with your own)
|
22 |
+
background_music = "calm.mp3" # Place this in the root folder
|
|
|
23 |
|
24 |
+
# Step 3: Create base image clip
|
25 |
+
clip = ImageClip(image_path, duration=10).resize(height=720)
|
26 |
+
clip = clip.set_position("center").fx(vfx.zoom_in, 1.1)
|
27 |
|
28 |
+
# Step 4: Add text caption
|
29 |
+
text = TextClip(caption, fontsize=40, color='white', font="Arial", method="caption", size=(clip.w * 0.9, None))
|
30 |
+
text = text.set_duration(10).set_position(("center", clip.h - 100))
|
|
|
31 |
|
32 |
+
# Step 5: Add logo
|
33 |
+
logo_clip = ImageClip(logo_path).set_duration(10).resize(height=80).set_position(("right", "top"))
|
|
|
|
|
|
|
|
|
34 |
|
35 |
+
# Step 6: Add audio
|
36 |
+
voice_audio = AudioFileClip(tts_path)
|
37 |
+
bg_audio = AudioFileClip(background_music).volumex(0.1)
|
38 |
+
final_audio = CompositeAudioClip([bg_audio.set_duration(10), voice_audio.set_start(0)])
|
39 |
|
40 |
+
# Step 7: Combine all
|
41 |
+
final = CompositeVideoClip([clip, logo_clip, text])
|
42 |
+
final = final.set_audio(final_audio)
|
|
|
|
|
|
|
43 |
|
44 |
+
output_path = "final_video.mp4"
|
45 |
+
final.write_videofile(output_path, fps=24)
|
|
|
|
|
|
|
46 |
|
47 |
+
return {"message": "🎬 Video created", "file": output_path}
|
|
|
|
|
48 |
|
49 |
+
|
50 |
+
async def save_file(uploaded: UploadFile, destination: str):
|
51 |
+
with open(destination, "wb") as f:
|
52 |
+
content = await uploaded.read()
|
53 |
+
f.write(content)
|