This documentation provides an overview of the Python code that implements a Video Upload and Processing API. The code is designed to handle video uploads, extract audio from the uploaded videos, and perform asynchronous audio-to-text transcription using OpenAI's API. Below, you will find a detailed explanation of the code structure, its functionality, and important considerations.
This Python code provides the backend functionality for a Video Upload and Processing API. It is built using Django and Django REST framework and utilizes the MoviePy library for video processing and OpenAI for audio-to-text transcription. The code handles the following key functionalities:
- Uploading video files with titles and descriptions.
- Extracting audio from uploaded videos using MoviePy Python Library
- Storing video chunks and associated metadata.
- Asynchronously transcribing audio to text using OpenAI's API.
- Providing API endpoints for listing, streaming, updating, and deleting videos.
The code is organized into several parts:
- API Endpoints: Defines API endpoints using Django REST framework to interact with videos.
- Models: Defines the database models for videos and video chunks.
- RabbitMQ Integration: Manages the asynchronous processing of audio files using RabbitMQ and OpenAI.
- Settings: Contains Django settings and configurations.
- Main Function: Initializes the RabbitMQ consumer for audio file processing.
- Endpoint:
/status/
- HTTP Method: GET
- Description: Returns the status of the server.
- Response:
- Success (HTTP 200):
{ "status": "ok" }
- Success (HTTP 200):
- Endpoint:
/videos/
- HTTP Method: GET
- Description: Retrieves a list of all uploaded videos.
- Response:
- Success (HTTP 200):
{ "status": "success", "message": "All Videos retrieved successfully", "data": [ { "id": 1, "title": "Sample Video 1", "description": "Description of Sample Video 1", "video_url": "https://example.com/video/1/stream/", "transcript": "Transcription of the video content." }, { "id": 2, "title": "Sample Video 2", "description": "Description of Sample Video 2", "video_url": "https://example.com/video/2/stream/", "transcript": "Transcription of the video content." } ] }
- Success (HTTP 200):
- Endpoint:
/videos/
- HTTP Method: POST
- Description: Uploads a new video with a title, description, and video binary data.
- Request Body:
{ "title": "Sample Video", "description": "Description of Sample Video", "video_binary": "[Binary Video Data]" }
- Response:
- Success (HTTP 201):
{ "status": "success", "message": "Video uploaded successfully", "data": { "id": 3, "title": "Sample Video", "description": "Description of Sample Video", "video_url": "https://example.com/video/3/stream/", "transcript": "Transcription of the video content." } }
- Error (HTTP 400 - Bad Request):
{ "status": "error", "message": "Blob video is required." }
- Success (HTTP 201):
- Endpoint:
/videos/{video_id}/stream/
- HTTP Method: GET
- Description: Streams the uploaded video with the specified ID.
- Response: Video stream with the "Content-Disposition" header for inline content.
- Endpoint:
/videos/{video_id}/
- HTTP Method: GET
- Description: Retrieves the uploaded video with the specified ID.
- Response:
- Success (HTTP 200):
{ "status": "success", "message": "Video retrieved successfully", "data": { "id": 1, "title": "Sample Video", "description": "Description of Sample Video", "video_url": "https://example.com/video/1/stream/", "transcript": "Transcription of the video content." } }
- Success (HTTP 200):
- Endpoint:
/videos/{video_id}/
- HTTP Method: PUT
- Description: Updates the transcript for the video with the specified ID.
- Request Body:
{ "transcript": "Updated transcription for the video." }
- Response:
- Success (HTTP 200):
{ "status": "success", "message": "Transcript updated successfully" }
- Error (HTTP 400 - Bad Request):
{ "status": "error", "message": "Transcript cannot be empty" }
- Success (HTTP 200):
- Endpoint:
/videos/{video_id}/
- HTTP Method: DELETE
- Description: Deletes the video with the specified ID.
- Response:
- Success (HTTP 200):
{ "status": "success", "message": "Video deleted successfully" }
- Success (HTTP 200):
To use this API, follow these steps:
- Set up a Django project and configure the database.
- Install required Python packages using
pip install -r requirements.txt
. - Start the Django development server.
- Make POST requests to
/videos/
to upload videos. - Use the provided endpoints to list, stream, update, or delete videos.
- Ensure RabbitMQ is installed and running to enable asynchronous audio transcription.
- Run the
consumer_rabbitmq.py
script to consume audio file paths and initiate transcription.