Web-ready standardized file processing and serialization. Read, write, convert and send files. Including image, audio, video and any other file. Easily convert between base64, bytes, numpy and more. Create browser elements or use in node.js
npm install @socaity/media-toolkit
bash
npm install @socaity/media-toolkit
`
Usage
This section covers how to effectively work with files when using the SDK.
$3
`typescript
import { MediaFile, ImageFile, AudioFile, VideoFile } from '@socaity/media-toolkit';
`
Or in browser environments
`html
`
$3
The SDK supports loading files from various sources:
`typescript
// From a local file path (Node.js only)
const imageFile = await MediaFile.create("./path/to/image.jpg");
// From a URL
const imageFromUrl = await MediaFile.create("https://example.com/image.jpg");
// From base64 data
const imageFromBase64 = await MediaFile.create("data:image/jpeg;base64,/9j/4AAQ...");
// From binary data (ArrayBuffer, Buffer, or Uint8Array)
const buffer = fs.readFileSync("./image.jpg");
const imageFromBuffer = await MediaFile.create(buffer);
`
$3
After receiving results from Socaity APIs, you can easily save or export them:
`typescript
// Convert to different formats
const mf = await MediaFile.create("..")
const base64String = mf.toBase64(); // Returns data URI by default
const arrayBuffer = mf.toArrayBuffer();
const uint8Array = mf.toUint8Array();
const buffer = mf.toBuffer(); // In Node.js environments
const blob = mf.toBlob(); // In browser environments
`
$3
For web applications, you can easily embed generated content.
We imply that you have the socaity SDK installed
`typescript
// Generate an image with Socaity API
const generatedImage = await socaity.text2img("A futuristic city");
// Create an image and append it to an element. This creates an
element.
document.getElementById('myImg').append(generatedImage.toImageElement())
// Or use in an existing HTML img
const imgElement = document.getElementById('myImg');
imgElement.src = generatedImage.toBase64();
`
$3
Retrieve and modify file metadata:
`typescript
// Get file information
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});
// Check file size in different units
const sizeInKB = mediaFile.fileSize('kb');
const sizeInMB = mediaFile.fileSize('mb');
// Modify file metadata
mediaFile.setFileName("renamed_file.png");
mediaFile.setContentType("image/png");
`
$3
Once you have a MediaFile object, you can use it directly with any Socaity API endpoint:
`typescript
// Use with face swap API
const swappedFace = await socaity.swapImg2Img(sourceImage, targetImage);
// Use with text-to-image API for image-to-image processing
const prompt = "Transform this into a cyberpunk scene";
const enhancedImage = await socaity.text2img(prompt, "flux-schnell", {
init_image: sourceImage,
strength: 0.75
});
``