Spaces:
Runtime error
Runtime error
saurabh singh commited on
Commit ·
c8f560e
1
Parent(s): 4e03111
Added A2V files
Browse files- Dockerfile +20 -0
- main.py +73 -0
- requirements.txt +4 -0
Dockerfile
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use the official Python image from the Docker Hub
|
| 2 |
+
FROM python:3.10
|
| 3 |
+
|
| 4 |
+
# Set the working directory in the container
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Copy the requirements file into the container
|
| 8 |
+
COPY requirements.txt .
|
| 9 |
+
|
| 10 |
+
# Install any dependencies
|
| 11 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 12 |
+
|
| 13 |
+
# Copy the rest of the application code into the container
|
| 14 |
+
COPY . .
|
| 15 |
+
|
| 16 |
+
# Expose the port your app runs on
|
| 17 |
+
EXPOSE 8000
|
| 18 |
+
|
| 19 |
+
# Command to run the application
|
| 20 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
|
main.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
import moviepy.editor as mp
|
| 4 |
+
import os
|
| 5 |
+
import tempfile
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
# Define a folder where the videos will be saved
|
| 10 |
+
VIDEO_SAVE_FOLDER = 'videos'
|
| 11 |
+
os.makedirs(VIDEO_SAVE_FOLDER, exist_ok=True)
|
| 12 |
+
|
| 13 |
+
# Allowed image content types
|
| 14 |
+
ALLOWED_IMAGE_TYPES = {
|
| 15 |
+
'image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'image/webp'
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
@app.post("/api/v1/audio-to-video/")
|
| 19 |
+
async def create_video(audio_file: UploadFile = File(...), image_file: UploadFile = File(...)):
|
| 20 |
+
# Validate audio file type
|
| 21 |
+
if not audio_file.content_type.startswith('audio/') or audio_file.content_type != 'audio/mpeg':
|
| 22 |
+
raise HTTPException(status_code=400, detail="Invalid audio file format. Only MP3 is allowed.")
|
| 23 |
+
|
| 24 |
+
# Validate image file type
|
| 25 |
+
if image_file.content_type not in ALLOWED_IMAGE_TYPES:
|
| 26 |
+
raise HTTPException(status_code=400, detail="Invalid image file format. Allowed formats: JPG, JPEG, PNG, GIF, WEBP.")
|
| 27 |
+
|
| 28 |
+
# Read the audio file and image file from the request
|
| 29 |
+
audio_data = await audio_file.read()
|
| 30 |
+
image_data = await image_file.read()
|
| 31 |
+
|
| 32 |
+
# Create temporary files for audio and image
|
| 33 |
+
audio_temp_file_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3").name
|
| 34 |
+
image_temp_file_path = tempfile.NamedTemporaryFile(delete=False, suffix=".jpg").name
|
| 35 |
+
|
| 36 |
+
with open(audio_temp_file_path, 'wb') as audio_temp_file:
|
| 37 |
+
audio_temp_file.write(audio_data)
|
| 38 |
+
|
| 39 |
+
with open(image_temp_file_path, 'wb') as image_temp_file:
|
| 40 |
+
image_temp_file.write(image_data)
|
| 41 |
+
|
| 42 |
+
try:
|
| 43 |
+
# Create MoviePy clips
|
| 44 |
+
audio_clip = mp.AudioFileClip(audio_temp_file_path)
|
| 45 |
+
image_clip = mp.ImageClip(image_temp_file_path)
|
| 46 |
+
|
| 47 |
+
# Set the duration of the image clip to match the duration of the audio clip
|
| 48 |
+
image_clip = image_clip.set_duration(audio_clip.duration).set_fps(24)
|
| 49 |
+
|
| 50 |
+
# Create the video with the image and the audio
|
| 51 |
+
video_clip = image_clip.set_audio(audio_clip)
|
| 52 |
+
|
| 53 |
+
# Define a path for the output video file
|
| 54 |
+
output_video_path = os.path.join(VIDEO_SAVE_FOLDER, 'output_video.mp4')
|
| 55 |
+
|
| 56 |
+
# Save the video to the defined path
|
| 57 |
+
video_clip.write_videofile(output_video_path, codec='libx264', audio_codec='aac', fps=24)
|
| 58 |
+
|
| 59 |
+
except Exception as e:
|
| 60 |
+
raise HTTPException(status_code=500, detail=f"Error creating video: {str(e)}")
|
| 61 |
+
|
| 62 |
+
finally:
|
| 63 |
+
# Clean up temporary files
|
| 64 |
+
os.remove(audio_temp_file_path)
|
| 65 |
+
os.remove(image_temp_file_path)
|
| 66 |
+
|
| 67 |
+
# Return the complete file path in a JSON response
|
| 68 |
+
complete_path = os.path.abspath(output_video_path)
|
| 69 |
+
return JSONResponse(status_code=201, content={"file_path": complete_path})
|
| 70 |
+
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
+
import uvicorn
|
| 73 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
moviepy
|
| 4 |
+
python-multipart
|