CF-Incidents Stream - Media Transformations binding for Workers

Cloudflare Service Informational Feeds

ENF News Bot

Automated News Bot
Joined
Apr 15, 2022
Messages
2,591
You can now use a Workers binding to transform videos with Media Transformations. This allows you to resize, crop, extract frames, and extract audio from videos stored anywhere, even in private locations like R2 buckets.

The Media Transformations binding is useful when you want to:

  • Transform videos stored in private or protected sources
  • Optimize videos and store the output directly back to R2 for re-use
  • Extract still frames for classification or description with Workers AI
  • Extract audio tracks for transcription using Workers AI

To get started, add the Media binding to your Wrangler configuration:


Code:
{
  "$schema": "./node_modules/wrangler/config-schema.json",
  "media": {
    "binding": "MEDIA"
  }
}

Code:
[media]
binding = "MEDIA"

Then use the binding in your Worker to transform videos:


Code:
export default {
	async fetch(request, env) {
		const video = await env.R2_BUCKET.get("input.mp4");

		const result = env.MEDIA.input(video.body)
			.transform({ width: 480, height: 270 })
			.output({ mode: "video", duration: "5s" });

		return await result.response();
	},
};

Code:
export default {
	async fetch(request, env) {
		const video = await env.R2_BUCKET.get("input.mp4");

		const result = env.MEDIA.input(video.body)
			.transform({ width: 480, height: 270 })
			.output({ mode: "video", duration: "5s" });

		return await result.response();
	},
};

Output modes include video for optimized MP4 clips, frame for still images, spritesheet for multiple frames, and audio for M4A extraction.

For more information, refer to the Media Transformations binding documentation.

Continue reading...
 
Back
Top