Media Transformations Binding for Workers
Cloudflare has released a new Workers binding for Media Transformations, enabling developers to programmatically transform videos directly within their Worker functions. This new capability allows you to work with videos stored in private or protected sources, including Cloudflare R2 buckets.
Key Capabilities
The Media Transformations binding supports several transformation operations:
- Resize and crop videos to specific dimensions
- Extract still frames from videos for classification or analysis
- Generate spritesheets containing multiple frames
- Extract audio tracks for transcription or processing
How It Works
The binding integrates seamlessly into your Wrangler configuration. Add the media binding to your wrangler.jsonc or wrangler.toml:
[media]
binding = "MEDIA"
Then reference it in your Worker code to transform videos:
const result = env.MEDIA.input(video.body)
.transform({ width: 480, height: 270 })
.output({ mode: "video", duration: "5s" });
Use Cases
This binding is particularly useful for:
- Private video optimization: Transform videos stored in secure locations without exposing them to external services
- Storage optimization: Process videos and store optimized versions directly back to R2 for efficient retrieval
- AI workflows: Extract frames for computer vision tasks or audio tracks for transcription using Workers AI
- Dynamic generation: Create responsive video assets on-the-fly based on request parameters
Output Modes
The binding supports multiple output formats including video for optimized MP4s, frame for still images, spritesheet for frame collections, and audio for M4A audio extraction.
For implementation details and advanced options, see the Media Transformations binding documentation.