Modern social media platforms don't just serve raw, unoptimized files. To ensure instant load speeds, they generate multiple responsive versions of assets. This repository provides a robust, Next.js-native approach to media processing, ensuring smaller data footprints and significantly faster client-side rendering.
The pipeline is designed to handle high-resolution uploads and transform them into web-ready formats. By generating a variety of resolutions and qualities, we minimize the Largest Contentful Paint (LCP) and reduce bandwidth costs for both the server and the end-user.
- iPhone Compatibility: Automated conversion of HEIC/HEIF images to standard web formats using
heic2any. - High-Performance Image Processing: Leverages
sharpfor lightning-fast resizing, cropping, and WebP/AVIF encoding. - Video Transcoding: Integrated
fluent-ffmpegpipeline to compress videos and generate thumbnails. - Next.js Integration: Built specifically to work within API routes or Server Actions for a seamless full-stack experience.
| Dependency | Purpose |
|---|---|
| sharp | High-speed Node.js image processing. |
| heic2any | Client-side conversion of Apple's HEIC format. |
| fluent-ffmpeg | A fluent API for FFMPEG to process video streams. |
| @ffmpeg-installer/ffmpeg | Provides the FFMPEG binary for various environments. |
- Clone the repository:
git clone https://github.com/The-Rigid-Project/media-optimizer.git- Install the necessary packages:
npm install sharp heic2any fluent-ffmpeg @ffmpeg-installer/ffmpeg- Ensure your environment has the necessary permissions for file system writes if you are processing files locally.
import sharp from 'sharp';
export async function optimizeImage(buffer) {
return await sharp(buffer)
.resize(800) // Resize to 800px width
.webp({ quality: 80 }) // Convert to WebP
.toBuffer();
}This ensures that regardless of the source file size, your application serves a performant, optimized version every time.