saurabh singh commited on
Commit ·
fa2229d
1
Parent(s): 791e9b6
Initial Commit
Browse files- Dockerfile +20 -0
- main.py +102 -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,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, send_from_directory
|
| 2 |
+
from flask_cors import CORS
|
| 3 |
+
import os
|
| 4 |
+
import uuid
|
| 5 |
+
from moviepy.editor import VideoFileClip
|
| 6 |
+
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
|
| 9 |
+
# Enable CORS for all domains
|
| 10 |
+
CORS(app)
|
| 11 |
+
|
| 12 |
+
# Directory to store uploaded files and converted audio files
|
| 13 |
+
UPLOAD_FOLDER = 'uploads'
|
| 14 |
+
AUDIO_FOLDER = 'audio'
|
| 15 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 16 |
+
os.makedirs(AUDIO_FOLDER, exist_ok=True)
|
| 17 |
+
|
| 18 |
+
# Allowed file extensions for video and audio
|
| 19 |
+
ALLOWED_VIDEO_EXTENSIONS = {'mp4', 'mkv', 'avi', 'mov', 'webm'}
|
| 20 |
+
ALLOWED_AUDIO_EXTENSIONS = {'mp3', 'wav', 'flac', 'aac'}
|
| 21 |
+
|
| 22 |
+
def allowed_file(filename, file_type):
|
| 23 |
+
ext = filename.rsplit('.', 1)[1].lower()
|
| 24 |
+
if file_type == 'video':
|
| 25 |
+
return '.' in filename and ext in ALLOWED_VIDEO_EXTENSIONS
|
| 26 |
+
elif file_type == 'audio':
|
| 27 |
+
return '.' in filename and ext in ALLOWED_AUDIO_EXTENSIONS
|
| 28 |
+
return False
|
| 29 |
+
|
| 30 |
+
@app.route('/api/v1/video-to-audio-convert', methods=['POST'])
|
| 31 |
+
def convert_video_to_audio():
|
| 32 |
+
if 'file' not in request.files:
|
| 33 |
+
return jsonify({
|
| 34 |
+
'status': 'error',
|
| 35 |
+
'status_code': 400,
|
| 36 |
+
'message': 'No file part'
|
| 37 |
+
}), 400
|
| 38 |
+
|
| 39 |
+
file = request.files['file']
|
| 40 |
+
target_format = request.form.get('format', 'mp3').lower()
|
| 41 |
+
|
| 42 |
+
if file.filename == '':
|
| 43 |
+
return jsonify({
|
| 44 |
+
'status': 'error',
|
| 45 |
+
'status_code': 400,
|
| 46 |
+
'message': 'No selected file'
|
| 47 |
+
}), 400
|
| 48 |
+
|
| 49 |
+
if not allowed_file(file.filename, 'video'):
|
| 50 |
+
return jsonify({
|
| 51 |
+
'status': 'error',
|
| 52 |
+
'status_code': 400,
|
| 53 |
+
'message': 'Unsupported video file type'
|
| 54 |
+
}), 400
|
| 55 |
+
|
| 56 |
+
if target_format not in ALLOWED_AUDIO_EXTENSIONS:
|
| 57 |
+
return jsonify({
|
| 58 |
+
'status': 'error',
|
| 59 |
+
'status_code': 400,
|
| 60 |
+
'message': 'Unsupported audio format'
|
| 61 |
+
}), 400
|
| 62 |
+
|
| 63 |
+
# Generate a unique filename to avoid collisions
|
| 64 |
+
video_filename = str(uuid.uuid4()) + '.' + file.filename.rsplit('.', 1)[1].lower()
|
| 65 |
+
video_path = os.path.join(UPLOAD_FOLDER, video_filename)
|
| 66 |
+
file.save(video_path)
|
| 67 |
+
|
| 68 |
+
# Define output audio file path
|
| 69 |
+
audio_filename = video_filename.rsplit('.', 1)[0] + '.' + target_format
|
| 70 |
+
audio_path = os.path.join(AUDIO_FOLDER, audio_filename)
|
| 71 |
+
|
| 72 |
+
try:
|
| 73 |
+
# Convert video to audio using MoviePy
|
| 74 |
+
video_clip = VideoFileClip(video_path)
|
| 75 |
+
audio_clip = video_clip.audio
|
| 76 |
+
audio_clip.write_audiofile(audio_path, codec='aac' if target_format == 'aac' else None)
|
| 77 |
+
audio_clip.close()
|
| 78 |
+
video_clip.close()
|
| 79 |
+
|
| 80 |
+
# After successful conversion, remove the video file
|
| 81 |
+
os.remove(video_path)
|
| 82 |
+
except Exception as e:
|
| 83 |
+
return jsonify({
|
| 84 |
+
'status': 'error',
|
| 85 |
+
'status_code': 500,
|
| 86 |
+
'message': f'Error during conversion: {str(e)}'
|
| 87 |
+
}), 500
|
| 88 |
+
|
| 89 |
+
# Return server path of the converted audio file
|
| 90 |
+
return jsonify({
|
| 91 |
+
'status': 'success',
|
| 92 |
+
'status_code': 200,
|
| 93 |
+
'message': 'Conversion successful',
|
| 94 |
+
'audio_file': f'/download/{audio_filename}' # Server-relative path for download
|
| 95 |
+
}), 200
|
| 96 |
+
|
| 97 |
+
@app.route('/download/<filename>', methods=['GET'])
|
| 98 |
+
def download_audio(filename):
|
| 99 |
+
return send_from_directory(AUDIO_FOLDER, filename)
|
| 100 |
+
|
| 101 |
+
if __name__ == '__main__':
|
| 102 |
+
app.run(debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
moviepy
|
| 4 |
+
python-multipart
|