Delete main.py
Browse files
main.py
DELETED
|
@@ -1,68 +0,0 @@
|
|
| 1 |
-
from fastapi import FastAPI, UploadFile, File
|
| 2 |
-
from fastapi.responses import FileResponse
|
| 3 |
-
from moviepy.editor import (
|
| 4 |
-
VideoFileClip,
|
| 5 |
-
ImageClip,
|
| 6 |
-
TextClip,
|
| 7 |
-
AudioFileClip,
|
| 8 |
-
CompositeVideoClip,
|
| 9 |
-
ColorClip
|
| 10 |
-
)
|
| 11 |
-
import os
|
| 12 |
-
import uuid
|
| 13 |
-
|
| 14 |
-
app = FastAPI()
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
@app.get("/")
|
| 18 |
-
async def root():
|
| 19 |
-
return {"message": "FastAPI with MoviePy is running."}
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
@app.post("/generate")
|
| 23 |
-
async def generate_video():
|
| 24 |
-
# Output video path
|
| 25 |
-
output_path = f"/app/generated_{uuid.uuid4().hex[:6]}.mp4"
|
| 26 |
-
|
| 27 |
-
# Create a colored background clip
|
| 28 |
-
bg_clip = ColorClip(size=(1280, 720), color=(10, 10, 10), duration=5)
|
| 29 |
-
|
| 30 |
-
# Create a text clip
|
| 31 |
-
txt_clip = TextClip("This is MoviePy via FastAPI", fontsize=70, color='white', font="DejaVu-Serif")
|
| 32 |
-
txt_clip = txt_clip.set_duration(5)
|
| 33 |
-
txt_clip = txt_clip.set_position(("center", "center"))
|
| 34 |
-
|
| 35 |
-
# Composite the video
|
| 36 |
-
final_clip = CompositeVideoClip([bg_clip, txt_clip])
|
| 37 |
-
|
| 38 |
-
# Write video to file
|
| 39 |
-
final_clip.write_videofile(output_path, fps=24, codec="libx264")
|
| 40 |
-
|
| 41 |
-
return FileResponse(output_path, media_type="video/mp4", filename=os.path.basename(output_path))
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
@app.post("/combine")
|
| 45 |
-
async def combine_video_audio(video_file: UploadFile = File(...), audio_file: UploadFile = File(...)):
|
| 46 |
-
video_path = f"/app/temp_video_{uuid.uuid4().hex[:6]}.mp4"
|
| 47 |
-
audio_path = f"/app/temp_audio_{uuid.uuid4().hex[:6]}.mp3"
|
| 48 |
-
output_path = f"/app/output_{uuid.uuid4().hex[:6]}.mp4"
|
| 49 |
-
|
| 50 |
-
# Save the uploaded files
|
| 51 |
-
with open(video_path, "wb") as f:
|
| 52 |
-
f.write(await video_file.read())
|
| 53 |
-
|
| 54 |
-
with open(audio_path, "wb") as f:
|
| 55 |
-
f.write(await audio_file.read())
|
| 56 |
-
|
| 57 |
-
# Process with MoviePy
|
| 58 |
-
videoclip = VideoFileClip(video_path)
|
| 59 |
-
audioclip = AudioFileClip(audio_path)
|
| 60 |
-
videoclip = videoclip.set_audio(audioclip)
|
| 61 |
-
|
| 62 |
-
videoclip.write_videofile(output_path, codec="libx264", fps=24)
|
| 63 |
-
|
| 64 |
-
# Clean temp files
|
| 65 |
-
os.remove(video_path)
|
| 66 |
-
os.remove(audio_path)
|
| 67 |
-
|
| 68 |
-
return FileResponse(output_path, media_type="video/mp4", filename=os.path.basename(output_path))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|