nadjibtabanii commited on
Commit
d0c38e9
·
verified ·
1 Parent(s): fcd04ea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -53
app.py CHANGED
@@ -1,67 +1,53 @@
1
- from fastapi import FastAPI, UploadFile, File, Form
2
- from fastapi.responses import FileResponse
3
- from moviepy.editor import *
4
  from gtts import gTTS
5
- import uuid, shutil, os
6
 
7
  app = FastAPI()
8
 
9
- @app.post("/generate")
10
- async def generate_video(
11
- images: list[UploadFile] = File(...),
12
- logo: UploadFile = File(...),
13
- music: UploadFile = File(...),
14
- narration_text: str = Form(...)
15
- ):
16
- uid = str(uuid.uuid4())
17
- temp_dir = f"temp/{uid}"
18
- os.makedirs(temp_dir, exist_ok=True)
19
 
20
- # Save files
21
- image_paths = []
22
- for i, img in enumerate(images):
23
- img_path = f"{temp_dir}/img_{i}.png"
24
- with open(img_path, "wb") as f:
25
- shutil.copyfileobj(img.file, f)
26
- image_paths.append(img_path)
27
 
28
- logo_path = f"{temp_dir}/logo.png"
29
- with open(logo_path, "wb") as f:
30
- shutil.copyfileobj(logo.file, f)
31
 
32
- music_path = f"{temp_dir}/music.mp3"
33
- with open(music_path, "wb") as f:
34
- shutil.copyfileobj(music.file, f)
35
 
36
- # Generate Arabic voice
37
- tts = gTTS(narration_text, lang='ar')
38
- narration_path = f"{temp_dir}/narration.mp3"
39
- tts.save(narration_path)
40
 
41
- # Create image clips with zoom effect
42
- clips = []
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
- video = concatenate_videoclips(clips, method="compose")
 
 
 
49
 
50
- # Overlay logo
51
- logo_clip = (ImageClip(logo_path)
52
- .set_duration(video.duration)
53
- .resize(height=80)
54
- .set_pos(("right", "top")))
55
- final = CompositeVideoClip([video, logo_clip])
56
 
57
- # Combine narration and music
58
- narration_audio = AudioFileClip(narration_path)
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
- # Export
64
- output_path = f"{temp_dir}/final_video.mp4"
65
- final.write_videofile(output_path, fps=24, codec="libx264", audio_codec="aac")
66
 
67
- return FileResponse(output_path, filename="final_video.mp4", media_type="video/mp4")
 
 
 
 
 
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)