Video Optimization API
Overview
The Video Optimization API is a RESTful API that allows you to optimize videos for the web. The API can be used to convert videos to different formats, resize videos, and extract thumbnails from videos.
File Size Limit in Free version of Femto API
Currently we have file size limit of 128MB. Larger file optimization is coming soon. Till then you can check Femto Desktop App for limitless file optimization.
Endpoint
POST /optimize-video
Request
- Method :
POST
- URL :
https://api.femto.cypherx.in/optimize-video
- Headers
Authorization: Bearer YOUR_API_KEY
Content-Type: multipart/form-data
- Body
level: "default" | "turbo" | "ultra"
- Default :
"default"
(Recommended for balanced mix of size & quality and universel playable format). - “turbo” is for higher compression with minor visual loss of quality & universel playable format.
- “ultra” is the most advanced format with higher compression and quality with popular media plyer support (vlc, kmplayer, web browsers etc ).
- Description : The level of optimization to be performed on the video. The default level is
"default"
. The"ultra"
level performs more aggressive optimization on the video, but it may take more time to process. - Example :
level: "ultra"
- Default :
Request Body
- Form Data
- Key :
video
- Type :
File
- Description : The video file to be optimized.
- Value : Binary Video Data
- Type :
- Key :
The maximum allowed Video size is 200 MB. And the supported video formats are: mp4
, webm
, mov
, avi
, flv
, wmv
, mkv
, m4v
.
Response
- Status Code :
200 OK
- Headers
Content-Type: video/YOUR_VIDEO_FORMAT
Response Body
- Value : Binary video Data
Error Response
-
Status Code :
400 Bad Request
- Headers
Content-Type: text/plain
- Body :
File type not supported
- Headers
-
Status Code :
401 Unauthorized
- Headers
Content-Type: application/json
- Body
{ "message": "Invalid API key" }
- Headers
-
Status Code :
413 Payload Too Large
- Headers
Content-Type: application/json
- Body
{ "message": "video size is too large. Maximum allowed size is 128 MB" }
- Headers
-
Status Code :
500 Internal Server Error
- Headers
Content-Type: text/plain
- Body :
Internal Server Error
- Headers
Request Examples
Here are some sample requests in different programming languages.
cURL
The optimized video will be saved in the current directory with the name optimized_video.mp4
.
curl -X POST \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: multipart/form-data" \
-F "video=@test_video.mp4" \
-o optimized_video.mp4 \
https://api.femto.cypherx.in/optimize-video
JavaScript
In Node.js
- Install the
axios
andform-data
packages.
npm install axios form-data
const axios = require('axios');
const fs = require('fs');
const FormData = require('form-data');
const apiUrl = 'https://api.femto.cypherx.in/optimize-video';
// Replace 'path/to/your/video.mp4' with the actual path to your video file
const videoPath = 'path/to/your/video.mp4';
// Replace 'your-api-key' with the actual API key
const apiKey = 'your-api-key';
async function optimizevideo() {
try {
// Read the video file as a buffer
const videoBuffer = fs.readFileSync(videoPath);
// Create FormData object for the file upload
const form = new FormData();
form.append('video', videoBuffer, { filename: 'video.mp4' });
// Make a POST request to the API with Authorization header
const response = await axios.post(apiUrl, form, {
headers: {
...form.getHeaders(),
'Content-Type': 'multipart/form-data',
'Authorization': `Bearer ${apiKey}`,
},
responseType: 'arraybuffer',
});
// Handle the response
console.log('Response:', response.data);
} catch (error) {
console.error('Error:', error.message);
}
}
// Call the function to make the request
optimizevideo();
In Browser
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>video Optimization</title>
</head>
<body>
<form id="videoForm">
<input type="file" id="videoInput" accept="video/*">
<button type="button" onclick="optimizevideo()">Optimize video</button>
</form>
<script>
async function optimizevideo() {
try {
const videoInput = document.getElementById('videoInput');
const file = videoInput.files[0];
if (!file) {
console.error('Please select an video file.');
return;
}
const form = new FormData();
form.append('video', file);
const apiKey = 'your-api-key'; // Replace with your actual API key
const apiUrl = 'https://api.femto.cypherx.in/optimize-video';
const response = await fetch(apiUrl, {
method: 'POST',
body: form,
headers: {
'Authorization': `Bearer ${apiKey}`,
},
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const optimizedvideoBuffer = await response.arrayBuffer();
// Handle the optimized video buffer as needed
console.log('Optimized video Buffer:', optimizedvideoBuffer);
} catch (error) {
console.error('Error:', error.message);
}
}
</script>
</body>
</html>
Python
- Install the
requests
package.
pip install requests
import requests
from io import BytesIO
api_url = 'https://api.femto.cypherx.in/optimize-video'
# Replace 'path/to/your/video.mp4' with the actual path to your video file
video_path = 'path/to/your/video.mp4'
# Replace 'your-api-key' with the actual API key
api_key = 'your-api-key'
def optimize_video():
try:
# Read the video file as a binary stream
with open(video_path, 'rb') as video_file:
# Create a BytesIO object to simulate a file-like object
video_buffer = BytesIO(video_file.read())
# Create a FormData-like dictionary for the file upload
files = {'video': ('video.mp4', video_buffer)}
# Make a POST request to the API with Authorization header
response = requests.post(api_url, files=files, headers={'Authorization': f'Bearer {api_key}'})
# Handle the response
if response.status_code == 200:
# Assuming the response is the optimized video data
optimized_video_buffer = BytesIO(response.content)
# Handle the optimized video buffer as needed
print('Optimized video Buffer:', optimized_video_buffer)
else:
print(f'Error: {response.status_code} - {response.text}')
except Exception as e:
print(f'Error: {str(e)}')
# Call the function to make the request
optimize_video()
Kotlin
// install the dependencies
dependencies {
implementation 'com.squareup.okhttp3:okhttp:4.9.3'
}
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.Response
import java.io.File
fun optimizevideo(videoPath : String) {
try {
val apiUrl = "https://api.femto.cypherx.in/optimize-video"
val apiKey = "your-api-key"
val videoFile = File(videoPath)
val requestBody = videoFile.asRequestBody("video/YOUR_video_EXT".toMediaType())
val request = Request.Builder()
.url(apiUrl)
.addHeader("Authorization", "Bearer $apiKey")
.post(requestBody)
.build()
val client = OkHttpClient()
val response: Response = client.newCall(request).execute()
// Handle the response
println("Response: ${response.body?.string()}")
} catch (e: Exception) {
println("Error: ${e.message}")
}
}
/*
Call the function to make the request
If you are calling this from AppcompactActivity() class then use it inside Thread
e.g.
Thread({
optimizevideo(video_path)
}).start()
*/
optimizevideo(video_path)