A high-performance FastAPI application that extracts the first frame from uploaded video files and returns it as a JPEG image with minimal latency.
- Fast Processing: Optimized for speed using OpenCV and efficient file handling
- Multiple Endpoints:
/extract-first-frame/- Returns first frame as image/extract-first-frame-info/- Returns first frame + video metadata
- Format Support: Supports all major video formats (MP4, AVI, MOV, MKV, etc.)
- Error Handling: Comprehensive error handling and validation
- Async Processing: Built with FastAPI's async capabilities for concurrent requests
-
Clone or navigate to the project directory:
cd firstframevid -
Activate your virtual environment (if using one):
source venv/bin/activate # On macOS/Linux # or venv\Scripts\activate # On Windows
-
Install dependencies:
pip install -r requirements.txt
python main.pyuvicorn main:app --host 0.0.0.0 --port 8000The API will be available at http://localhost:8000
POST /extract-first-frame/
Uploads a video file and returns the first frame as a JPEG image.
Request:
- Method: POST
- Content-Type: multipart/form-data
- Body: Video file (key: "file")
Response:
- Content-Type: image/jpeg
- Body: JPEG image data
Example using curl:
curl -X POST "http://localhost:8000/extract-first-frame/" \
-H "accept: image/jpeg" \
-H "Content-Type: multipart/form-data" \
-F "file=@your_video.mp4" \
--output first_frame.jpgPOST /extract-first-frame-info/
Uploads a video file and returns the first frame as base64 along with video metadata.
Request:
- Method: POST
- Content-Type: multipart/form-data
- Body: Video file (key: "file")
Response:
{
"image_base64": "base64_encoded_image_data",
"video_info": {
"filename": "video.mp4",
"width": 1920,
"height": 1080,
"fps": 30.0,
"frame_count": 900,
"duration_seconds": 30.0,
"file_size_bytes": 15728640
}
}Example using curl:
curl -X POST "http://localhost:8000/extract-first-frame-info/" \
-H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "file=@your_video.mp4"GET /
Returns API status.
Response:
{
"message": "First Frame Extractor API is running"
}Once the server is running, you can access:
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc
These provide interactive documentation where you can test the endpoints directly from your browser.
The application is optimized for speed through:
- Efficient File Handling: Uses temporary files for reliable video processing
- OpenCV Optimization: Leverages OpenCV's optimized video decoding
- Async Processing: FastAPI's async capabilities for handling multiple requests
- Resource Management: Proper cleanup of temporary files and video capture objects
- High-Quality JPEG Encoding: Balances file size and image quality
The application supports all video formats that OpenCV can handle, including:
- MP4
- AVI
- MOV
- MKV
- WMV
- FLV
- WebM
- And many more...
The API provides clear error messages for common issues:
- Invalid file types (non-video files)
- Corrupted video files
- Videos without readable frames
- Server errors during processing
import requests
def extract_first_frame(video_path, output_path):
url = "http://localhost:8000/extract-first-frame/"
with open(video_path, "rb") as video_file:
files = {"file": video_file}
response = requests.post(url, files=files)
if response.status_code == 200:
with open(output_path, "wb") as output_file:
output_file.write(response.content)
print(f"First frame saved to {output_path}")
else:
print(f"Error: {response.status_code} - {response.text}")
# Usage
extract_first_frame("input_video.mp4", "first_frame.jpg")This project is open source and available under the MIT License.