Skip to content

Commit c79fc73

Browse files
Stephen G. PopeStephen G. Pope
authored andcommitted
updated docs
1 parent a76f6ba commit c79fc73

1 file changed

Lines changed: 66 additions & 165 deletions

File tree

Lines changed: 66 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -1,237 +1,138 @@
1-
# Media Convert API Endpoint Documentation
1+
# Media Convert Endpoint Documentation
22

33
## 1. Overview
44

5-
The `/v1/media/convert` endpoint provides a service for converting media files from one format to another. It allows users to specify the desired output format and optionally set video and audio codecs. This endpoint is part of the v1 API structure and integrates with the application's queuing system to handle potentially resource-intensive conversion tasks asynchronously.
6-
7-
The endpoint fits into the overall API structure as one of several media processing services, registered in the main application as the `v1_media_convert_bp` blueprint.
5+
The `/v1/media/convert` endpoint is part of the Flask API application and is responsible for converting media files (audio or video) from one format to another. This endpoint fits into the overall API structure as a part of the `v1` blueprint, which contains various media-related functionalities.
86

97
## 2. Endpoint
108

11-
- **URL**: `/v1/media/convert`
12-
- **Method**: `POST`
9+
**URL Path:** `/v1/media/convert`
10+
**HTTP Method:** `POST`
1311

1412
## 3. Request
1513

1614
### Headers
1715

18-
- `x-api-key`: Required. Your API authentication key.
16+
- `x-api-key` (required): The API key for authentication.
1917

2018
### Body Parameters
2119

22-
| Parameter | Type | Required | Description |
23-
|-----------|------|----------|-------------|
24-
| `media_url` | string | Yes | URL of the media file to convert (must be a valid URI) |
25-
| `format` | string | Yes | Desired output format (e.g., "mp4", "webm", "mov") |
26-
| `video_codec` | string | No | Video codec to use (defaults to "libx264") |
27-
| `video_preset` | string | No | Encoding preset for speed/quality tradeoff (defaults to "medium") |
28-
| `video_crf` | number | No | Constant Rate Factor for quality (0-51, default: 23, lower is better quality) |
29-
| `audio_codec` | string | No | Audio codec to use (defaults to "aac") |
30-
| `audio_bitrate` | string | No | Audio bitrate (defaults to "128k") |
31-
| `webhook_url` | string | No | URL to receive conversion completion notification (must be a valid URI) |
32-
| `id` | string | No | Custom identifier for tracking the request |
20+
The request body must be a JSON object with the following properties:
3321

34-
### Example Requests
22+
- `media_url` (required, string): The URL of the media file to be converted.
23+
- `format` (required, string): The desired output format for the converted media file.
24+
- `video_codec` (optional, string): The video codec to be used for the conversion. Default is `libx264`.
25+
- `video_preset` (optional, string): The video preset to be used for the conversion. Default is `medium`.
26+
- `video_crf` (optional, number): The Constant Rate Factor (CRF) value for video encoding. Must be between 0 and 51. Default is 23.
27+
- `audio_codec` (optional, string): The audio codec to be used for the conversion. Default is `aac`.
28+
- `audio_bitrate` (optional, string): The audio bitrate to be used for the conversion. Default is `128k`.
29+
- `webhook_url` (optional, string): The URL to receive a webhook notification upon completion of the conversion process.
30+
- `id` (optional, string): An optional identifier for the conversion request.
3531

36-
#### Video Conversion Example
32+
### Example Request
3733

3834
```json
3935
{
40-
"media_url": "https://example.com/input-video.mp4",
41-
"format": "mp4",
36+
"media_url": "https://example.com/video.mp4",
37+
"format": "avi",
4238
"video_codec": "libx264",
4339
"video_preset": "medium",
4440
"video_crf": 23,
4541
"audio_codec": "aac",
46-
"audio_bitrate": "192k",
47-
"webhook_url": "https://your-server.com/webhook",
48-
"id": "custom-request-123"
49-
}
50-
```
51-
52-
#### Audio Extraction Example
53-
54-
```json
55-
{
56-
"media_url": "https://example.com/input-video.mp4",
57-
"format": "mp3",
58-
"audio_bitrate": "320k",
59-
"id": "audio-extraction-request"
42+
"audio_bitrate": "128k",
43+
"webhook_url": "https://example.com/webhook",
44+
"id": "unique-request-id"
6045
}
6146
```
6247

63-
For audio-only formats (mp3, aac, wav, etc.), the appropriate audio codec will be automatically selected.
64-
65-
### Example cURL Command
66-
6748
```bash
68-
curl -X POST https://api.example.com/v1/media/convert \
69-
-H "Content-Type: application/json" \
70-
-H "x-api-key: your-api-key" \
49+
curl -X POST \
50+
https://api.example.com/v1/media/convert \
51+
-H 'x-api-key: YOUR_API_KEY' \
52+
-H 'Content-Type: application/json' \
7153
-d '{
72-
"media_url": "https://example.com/input-video.mp4",
73-
"format": "mp4",
54+
"media_url": "https://example.com/video.mp4",
55+
"format": "avi",
7456
"video_codec": "libx264",
7557
"video_preset": "medium",
7658
"video_crf": 23,
7759
"audio_codec": "aac",
78-
"audio_bitrate": "192k",
79-
"webhook_url": "https://your-server.com/webhook",
80-
"id": "custom-request-123"
60+
"audio_bitrate": "128k",
61+
"webhook_url": "https://example.com/webhook",
62+
"id": "unique-request-id"
8163
}'
8264
```
8365

8466
## 4. Response
8567

86-
### Success Response (Immediate)
87-
88-
If a webhook URL is provided, the API will queue the task and return an immediate 202 Accepted response:
68+
### Success Response
8969

90-
```json
91-
{
92-
"code": 202,
93-
"id": "custom-request-123",
94-
"job_id": "550e8400-e29b-41d4-a716-446655440000",
95-
"message": "processing",
96-
"pid": 12345,
97-
"queue_id": 67890,
98-
"max_queue_length": "unlimited",
99-
"queue_length": 3,
100-
"build_number": "1.0.0"
101-
}
102-
```
103-
104-
### Success Response (Webhook or Direct)
105-
106-
When the conversion is complete, the following response will be sent to the webhook URL (if provided) or returned directly (if no webhook URL):
70+
The success response will be a JSON object containing the URL of the converted media file uploaded to cloud storage, the endpoint path, and a status code of 200.
10771

10872
```json
10973
{
110-
"endpoint": "/v1/media/convert",
11174
"code": 200,
112-
"id": "custom-request-123",
113-
"job_id": "550e8400-e29b-41d4-a716-446655440000",
114-
"response": "https://storage.example.com/converted-file-123.mp4",
75+
"id": "unique-request-id",
76+
"job_id": "a1b2c3d4-e5f6-g7h8-i9j0-k1l2m3n4o5p6",
77+
"response": "https://cloud.example.com/converted-video.avi",
11578
"message": "success",
11679
"pid": 12345,
117-
"queue_id": 67890,
118-
"run_time": 5.234,
119-
"queue_time": 1.123,
120-
"total_time": 6.357,
121-
"queue_length": 2,
80+
"queue_id": 1234567890,
81+
"run_time": 10.234,
82+
"queue_time": 0.123,
83+
"total_time": 10.357,
84+
"queue_length": 0,
12285
"build_number": "1.0.0"
12386
}
12487
```
12588

12689
### Error Responses
12790

128-
#### Invalid Request (400 Bad Request)
91+
- **400 Bad Request**: Returned when the request payload is missing or invalid.
92+
- **401 Unauthorized**: Returned when the `x-api-key` header is missing or invalid.
93+
- **500 Internal Server Error**: Returned when an unexpected error occurs during the conversion process.
12994

130-
```json
131-
{
132-
"code": 400,
133-
"error": "Invalid request payload",
134-
"details": "Required field 'media_url' is missing"
135-
}
136-
```
137-
138-
#### Authentication Error (401 Unauthorized)
139-
140-
```json
141-
{
142-
"code": 401,
143-
"error": "Authentication failed",
144-
"message": "Invalid or missing API key"
145-
}
146-
```
147-
148-
#### Queue Full (429 Too Many Requests)
95+
Example error response:
14996

15097
```json
15198
{
152-
"code": 429,
153-
"id": "custom-request-123",
154-
"job_id": "550e8400-e29b-41d4-a716-446655440000",
155-
"message": "MAX_QUEUE_LENGTH (100) reached",
156-
"pid": 12345,
157-
"queue_id": 67890,
158-
"queue_length": 100,
159-
"build_number": "1.0.0"
160-
}
161-
```
162-
163-
#### Processing Error (500 Internal Server Error)
164-
165-
```json
166-
{
167-
"endpoint": "/v1/media/convert",
168-
"code": 500,
169-
"id": "custom-request-123",
170-
"job_id": "550e8400-e29b-41d4-a716-446655440000",
171-
"response": null,
172-
"message": "Error downloading media file: Connection timeout",
99+
"code": 400,
100+
"id": "unique-request-id",
101+
"job_id": "a1b2c3d4-e5f6-g7h8-i9j0-k1l2m3n4o5p6",
102+
"message": "Invalid request payload",
173103
"pid": 12345,
174-
"queue_id": 67890,
175-
"run_time": 2.345,
176-
"queue_time": 0.567,
177-
"total_time": 2.912,
178-
"queue_length": 5,
104+
"queue_id": 1234567890,
105+
"queue_length": 0,
179106
"build_number": "1.0.0"
180107
}
181108
```
182109

183110
## 5. Error Handling
184111

185-
The endpoint handles several types of errors:
112+
The endpoint uses the `validate_payload` decorator to validate the request payload against a JSON schema. If the payload is missing or invalid, a 400 Bad Request error is returned.
186113

187-
- **Validation Errors (400)**: Occur when the request payload doesn't match the required schema (missing required fields, invalid formats, or additional properties).
188-
- **Authentication Errors (401)**: Occur when the API key is invalid or missing.
189-
- **Queue Limit Errors (429)**: Occur when the processing queue is full (if MAX_QUEUE_LENGTH is set).
190-
- **Processing Errors (500)**: Occur during the media conversion process, such as:
191-
- Invalid media URL
192-
- Unsupported format
193-
- Media download failures
194-
- Conversion process failures
195-
- Cloud storage upload failures
114+
The `authenticate` decorator is used to ensure that the request includes a valid `x-api-key` header. If the header is missing or invalid, a 401 Unauthorized error is returned.
196115

197-
All errors include descriptive messages to help diagnose the issue.
116+
If an unexpected error occurs during the conversion process, a 500 Internal Server Error is returned, and the error is logged.
198117

199118
## 6. Usage Notes
200119

201-
- The conversion process is queued by default, making it suitable for handling larger files without blocking.
202-
- When providing a `webhook_url`, you'll receive an immediate 202 response, and the final result will be sent to your webhook when processing completes.
203-
- Without a `webhook_url`, the request will still be queued but the response will be held until processing completes.
204-
- The default codecs (`libx264` for video and `aac` for audio) provide good compatibility with most formats.
205-
- The `video_preset` parameter controls the encoding speed/quality tradeoff. Options include: `ultrafast`, `superfast`, `veryfast`, `faster`, `fast`, `medium`, `slow`, `slower`, `veryslow`.
206-
- The `video_crf` parameter (0-51) controls quality - lower values mean better quality but larger files. 23 is a good default.
207-
- The `audio_bitrate` parameter sets the audio quality (e.g., "128k", "192k", "320k").
208-
- You can still use `"video_codec": "copy"` and `"audio_codec": "copy"` to copy streams without re-encoding, which is faster but may not be compatible with all format conversions.
209-
- For audio-only output formats (`mp3`, `aac`, `wav`, `flac`, `ogg`, `opus`), the service will automatically use the appropriate audio codec regardless of the codec you specify:
210-
- `mp3`: Uses `libmp3lame` codec
211-
- `aac`: Uses `aac` codec
212-
- `opus`: Uses `libopus` codec
213-
- `flac`: Uses `flac` codec
214-
- `ogg`: Uses `libvorbis` codec
215-
- `wav`: Uses `pcm_s16le` codec
216-
- The converted file is automatically uploaded to cloud storage, and the URL is provided in the response.
120+
- The `media_url` parameter must be a valid URL pointing to the media file to be converted.
121+
- The `format` parameter must be a valid media format supported by the conversion process.
122+
- The optional parameters (`video_codec`, `video_preset`, `video_crf`, `audio_codec`, `audio_bitrate`) allow you to customize the conversion settings.
123+
- If the `webhook_url` parameter is provided, a webhook notification will be sent to the specified URL upon completion of the conversion process.
124+
- The `id` parameter is optional and can be used to identify the conversion request.
217125

218126
## 7. Common Issues
219127

220-
- **Unsupported Format Combinations**: Not all format and codec combinations are valid. For example, using an MP3 audio codec with a WebM container may fail.
221-
- **Large File Timeouts**: Very large files may time out during download or upload. Consider using more direct file transfer methods for extremely large files.
222-
- **Webhook Failures**: If your webhook endpoint is unavailable when the conversion completes, you may not receive the completion notification.
223-
- **Queue Congestion**: During high load periods, the queue may fill up, resulting in 429 responses.
224-
- **Invalid Media URLs**: The media URL must be directly accessible by the server. URLs requiring authentication or session cookies may fail.
128+
- Providing an invalid or inaccessible `media_url`.
129+
- Specifying an unsupported `format`.
130+
- Providing invalid values for the optional parameters (e.g., `video_crf` outside the valid range).
225131

226132
## 8. Best Practices
227133

228-
- **Use Appropriate Settings**: Choose video/audio codecs and settings based on your needs:
229-
- Use `"copy"` for both codecs when you just need to change the container format without re-encoding
230-
- Use `"libx264"` with `"medium"` preset and CRF 23 for good quality/size balance for video
231-
- For lower file sizes, increase CRF (e.g., 28) or use a faster preset
232-
- For higher quality, decrease CRF (e.g., 18) or use a slower preset
233-
- **Include an ID**: Always include a unique `id` in your requests to help track and identify them, especially when using webhooks.
234-
- **Webhook Reliability**: Ensure your webhook endpoint is reliable and can handle the response payload. Implement retry logic on your webhook receiver.
235-
- **Format Selection**: Choose the output format based on your target platform requirements. For web use, WebM or MP4 are generally recommended.
236-
- **Monitor Queue Length**: If you're making many requests, monitor the `queue_length` in responses to avoid hitting queue limits.
237-
- **Handle Errors Gracefully**: Implement proper error handling in your application to deal with potential conversion failures.
134+
- Always validate the input parameters on the client-side before sending the request.
135+
- Use the `id` parameter to track and identify conversion requests.
136+
- Provide a `webhook_url` to receive notifications about the conversion process completion.
137+
- Monitor the API logs for any errors or issues during the conversion process.
138+
- Consider implementing rate limiting or queue management to handle high volumes of requests.

0 commit comments

Comments
 (0)