|
| 1 | +# Video Split Endpoint |
| 2 | + |
| 3 | +## 1. Overview |
| 4 | + |
| 5 | +The `/v1/video/split` endpoint is part of the Video API and is used to split a video file into multiple segments based on specified start and end times. This endpoint fits into the overall API structure as a part of the version 1 (`v1`) routes, specifically under the `video` category. |
| 6 | + |
| 7 | +## 2. Endpoint |
| 8 | + |
| 9 | +**URL Path:** `/v1/video/split` |
| 10 | +**HTTP Method:** `POST` |
| 11 | + |
| 12 | +## 3. Request |
| 13 | + |
| 14 | +### Headers |
| 15 | + |
| 16 | +- `x-api-key` (required): The API key for authentication. |
| 17 | + |
| 18 | +### Body Parameters |
| 19 | + |
| 20 | +The request body must be a JSON object with the following properties: |
| 21 | + |
| 22 | +- `video_url` (required, string): The URL of the video file to be split. |
| 23 | +- `splits` (required, array of objects): An array of objects specifying the start and end times for each split. Each object must have the following properties: |
| 24 | + - `start` (required, string): The start time of the split in the format `hh:mm:ss.ms`. |
| 25 | + - `end` (required, string): The end time of the split in the format `hh:mm:ss.ms`. |
| 26 | +- `video_codec` (optional, string): The video codec to use for encoding the split videos. Default is `libx264`. |
| 27 | +- `video_preset` (optional, string): The video preset to use for encoding the split videos. Default is `medium`. |
| 28 | +- `video_crf` (optional, number): The Constant Rate Factor (CRF) value for video encoding. Must be between 0 and 51. Default is 23. |
| 29 | +- `audio_codec` (optional, string): The audio codec to use for encoding the split videos. Default is `aac`. |
| 30 | +- `audio_bitrate` (optional, string): The audio bitrate to use for encoding the split videos. Default is `128k`. |
| 31 | +- `webhook_url` (optional, string): The URL to receive a webhook notification when the split operation is complete. |
| 32 | +- `id` (optional, string): A unique identifier for the request. |
| 33 | + |
| 34 | +### Example Request |
| 35 | + |
| 36 | +```json |
| 37 | +{ |
| 38 | + "video_url": "https://example.com/video.mp4", |
| 39 | + "splits": [ |
| 40 | + { |
| 41 | + "start": "00:00:10.000", |
| 42 | + "end": "00:00:20.000" |
| 43 | + }, |
| 44 | + { |
| 45 | + "start": "00:00:30.000", |
| 46 | + "end": "00:00:40.000" |
| 47 | + } |
| 48 | + ], |
| 49 | + "video_codec": "libx264", |
| 50 | + "video_preset": "medium", |
| 51 | + "video_crf": 23, |
| 52 | + "audio_codec": "aac", |
| 53 | + "audio_bitrate": "128k", |
| 54 | + "webhook_url": "https://example.com/webhook", |
| 55 | + "id": "unique-request-id" |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +```bash |
| 60 | +curl -X POST \ |
| 61 | + https://api.example.com/v1/video/split \ |
| 62 | + -H 'x-api-key: YOUR_API_KEY' \ |
| 63 | + -H 'Content-Type: application/json' \ |
| 64 | + -d '{ |
| 65 | + "video_url": "https://example.com/video.mp4", |
| 66 | + "splits": [ |
| 67 | + { |
| 68 | + "start": "00:00:10.000", |
| 69 | + "end": "00:00:20.000" |
| 70 | + }, |
| 71 | + { |
| 72 | + "start": "00:00:30.000", |
| 73 | + "end": "00:00:40.000" |
| 74 | + } |
| 75 | + ], |
| 76 | + "video_codec": "libx264", |
| 77 | + "video_preset": "medium", |
| 78 | + "video_crf": 23, |
| 79 | + "audio_codec": "aac", |
| 80 | + "audio_bitrate": "128k", |
| 81 | + "webhook_url": "https://example.com/webhook", |
| 82 | + "id": "unique-request-id" |
| 83 | + }' |
| 84 | +``` |
| 85 | + |
| 86 | +## 4. Response |
| 87 | + |
| 88 | +### Success Response |
| 89 | + |
| 90 | +The success response follows the general response format specified in `app.py`. Here's an example: |
| 91 | + |
| 92 | +```json |
| 93 | +{ |
| 94 | + "endpoint": "/v1/video/split", |
| 95 | + "code": 200, |
| 96 | + "id": "unique-request-id", |
| 97 | + "job_id": "a1b2c3d4-e5f6-g7h8-i9j0-k1l2m3n4o5p6", |
| 98 | + "response": [ |
| 99 | + { |
| 100 | + "file_url": "https://example.com/split-1.mp4" |
| 101 | + }, |
| 102 | + { |
| 103 | + "file_url": "https://example.com/split-2.mp4" |
| 104 | + } |
| 105 | + ], |
| 106 | + "message": "success", |
| 107 | + "pid": 12345, |
| 108 | + "queue_id": 6789, |
| 109 | + "run_time": 5.234, |
| 110 | + "queue_time": 0.123, |
| 111 | + "total_time": 5.357, |
| 112 | + "queue_length": 0, |
| 113 | + "build_number": "1.0.0" |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +The `response` field contains an array of objects, each representing a split video file. Each object has a `file_url` property containing the URL of the split video file. |
| 118 | + |
| 119 | +### Error Responses |
| 120 | + |
| 121 | +- **400 Bad Request**: Returned when the request payload is missing or invalid. |
| 122 | +- **401 Unauthorized**: Returned when the `x-api-key` header is missing or invalid. |
| 123 | +- **429 Too Many Requests**: Returned when the maximum queue length has been reached. |
| 124 | +- **500 Internal Server Error**: Returned when an unexpected error occurs during the video split process. |
| 125 | + |
| 126 | +Example error response: |
| 127 | + |
| 128 | +```json |
| 129 | +{ |
| 130 | + "code": 400, |
| 131 | + "id": "unique-request-id", |
| 132 | + "job_id": "a1b2c3d4-e5f6-g7h8-i9j0-k1l2m3n4o5p6", |
| 133 | + "message": "Invalid request payload: 'splits' is a required property", |
| 134 | + "pid": 12345, |
| 135 | + "queue_id": 6789, |
| 136 | + "queue_length": 2, |
| 137 | + "build_number": "1.0.0" |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | +## 5. Error Handling |
| 142 | + |
| 143 | +The endpoint handles the following common errors: |
| 144 | + |
| 145 | +- Missing or invalid request parameters: Returns a 400 Bad Request error with a descriptive error message. |
| 146 | +- Authentication failure: Returns a 401 Unauthorized error if the `x-api-key` header is missing or invalid. |
| 147 | +- Queue length exceeded: Returns a 429 Too Many Requests error if the maximum queue length has been reached. |
| 148 | +- Unexpected exceptions: Returns a 500 Internal Server Error with the exception message. |
| 149 | + |
| 150 | +The main application context (`app.py`) also includes error handling for queue length limits and webhook notifications. |
| 151 | + |
| 152 | +## 6. Usage Notes |
| 153 | + |
| 154 | +- The `video_url` parameter must be a valid URL pointing to a video file. |
| 155 | +- The `splits` array must contain at least one object specifying the start and end times for a split. |
| 156 | +- The start and end times must be in the format `hh:mm:ss.ms` (hours:minutes:seconds.milliseconds). |
| 157 | +- The `video_codec`, `video_preset`, `video_crf`, `audio_codec`, and `audio_bitrate` parameters are optional and can be used to customize the encoding settings for the split videos. |
| 158 | +- If the `webhook_url` parameter is provided, a webhook notification will be sent to the specified URL when the split operation is complete. |
| 159 | +- The `id` parameter is optional and can be used to uniquely identify the request. |
| 160 | + |
| 161 | +## 7. Common Issues |
| 162 | + |
| 163 | +- Providing an invalid or inaccessible `video_url`. |
| 164 | +- Specifying overlapping or invalid start and end times in the `splits` array. |
| 165 | +- Exceeding the maximum queue length, which can result in a 429 Too Many Requests error. |
| 166 | + |
| 167 | +## 8. Best Practices |
| 168 | + |
| 169 | +- Validate the `video_url` parameter before sending the request to ensure it points to a valid video file. |
| 170 | +- Ensure that the start and end times in the `splits` array are correctly formatted and do not overlap. |
| 171 | +- Consider using the `webhook_url` parameter to receive notifications about the completion of the split operation, especially for long-running or asynchronous requests. |
| 172 | +- Implement retry mechanisms and error handling in your client application to handle potential errors and failures. |
| 173 | +- Monitor the queue length and adjust the `MAX_QUEUE_LENGTH` environment variable as needed to prevent excessive queuing and potential timeouts. |
0 commit comments