MediaToolkit-JS
Web-ready standardized file processing and serialization
We @socaity.ai designed media-toolkit to work in a standardized way with all kind of media files across node.js, browser, and python environments. This SDK is designed to simplify the process of loading, manipulating, and exporting media files, making it an essential tool for developers working with multimedia content.
We use it to offer a simple unified approach for interacting with the socaity APIs for generative AI.
Check-out our SDKs socaity-js and socaity-python to learn more about it.
Key Features
- Cross-Platform Compatibility: Works effortlessly in Node.js, browser and python environments. See media-toolkit-python
- Versatile File Handling: Supports various file types including images, audio, and video.
- Easy Integration: Simple API for loading files from local paths, URLs, base64 data, and binary data.
- Advanced Processing: Chain operations to create efficient file processing pipelines.
- SEO Optimized: Enhance your web applications with optimized media handling.
- Lightweight & Fast The package comes without additional dependencies and has just ~8kb.
Getting Started
Installation
Install the SDK via npm:
npm install @socaity/media-toolkit
Usage
This section covers how to effectively work with files when using the SDK.
Import
import { MediaFile, ImageFile, AudioFile, VideoFile } from '@socaity/media-toolkit';
Or in browser environments
<script src="media-toolkit.umd.js" />
Loading Files
The SDK supports loading files from various sources:
const imageFile = await MediaFile.create("./path/to/image.jpg");
const imageFromUrl = await MediaFile.create("https://example.com/image.jpg");
const imageFromBase64 = await MediaFile.create("data:image/jpeg;base64,/9j/4AAQ...");
const buffer = fs.readFileSync("./image.jpg");
const imageFromBuffer = await MediaFile.create(buffer);
Saving and Exporting Files
After receiving results from Socaity APIs, you can easily save or export them:
const mf = await MediaFile.create("..")
const base64String = mf.toBase64();
const arrayBuffer = mf.toArrayBuffer();
const uint8Array = mf.toUint8Array();
const buffer = mf.toBuffer();
const blob = mf.toBlob();
Embedding Media in Websites
For web applications, you can easily embed generated content.
We imply that you have the socaity SDK installed
const generatedImage = await socaity.text2img("A futuristic city");
document.getElementById('myImg').append(generatedImage.toImageElement())
const imgElement = document.getElementById('myImg');
imgElement.src = generatedImage.toBase64();
File Information and Manipulation
Retrieve and modify file metadata:
const fileInfo = mediaFile.getInfo();
console.log(`Filename: ${fileInfo.fileName}`);
console.log(`Content type: ${fileInfo.contentType}`);
console.log(`Size: ${fileInfo.size} bytes`);
console.log(`Extension: ${fileInfo.extension}`);
const sizeInKB = mediaFile.fileSize('kb');
const sizeInMB = mediaFile.fileSize('mb');
mediaFile.setFileName("renamed_file.png");
mediaFile.setContentType("image/png");
Sending Files to Socaity APIs
Once you have a MediaFile object, you can use it directly with any Socaity API endpoint:
const swappedFace = await socaity.swapImg2Img(sourceImage, targetImage);
const prompt = "Transform this into a cyberpunk scene";
const enhancedImage = await socaity.text2img(prompt, "flux-schnell", {
init_image: sourceImage,
strength: 0.75
});
Contributing
We welcome contributions from the community. Please read our contributing guidelines to get started.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contact
For support or inquiries, please contact us at support@socaity.ai.
Stay updated with our latest news and releases by following us on Twitter and LinkedIn.
Happy coding!