Skip to content

nathanagez/lambda-response-streaming-video-transformation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lambda Response Streaming Video Transformation

A proof-of-concept project demonstrating real-time video transcoding and streaming using AWS Lambda Response Streaming with FFmpeg.

Overview

This project showcases how to use AWS Lambda's response streaming feature to transcode and resize videos on-the-fly. Videos are fetched from S3, processed with FFmpeg, and streamed directly to the client without storing intermediate files.

demo

Features

  • Real-time video transcoding using FFmpeg
  • Dynamic video resizing via query parameters
  • Lambda Response Streaming

Prerequisites

  • AWS Account
  • AWS CLI configured
  • AWS SAM CLI installed
  • Node.js 20.x
  • An S3 bucket with a video file (preferably MP4 format)

Setup

1. Prepare Your S3 Bucket

Create an S3 bucket and upload a video file (MP4 recommended):

aws s3 mb s3://your-bucket-name
aws s3 cp your-video.mp4 s3://your-bucket-name/

2. Configure SAM

Edit the samconfig.toml file and update the following parameters:

parameter_overrides = [
    "Project=PoC",
    "Name=Lambda Response Streaming",
    "Owner=Your Name",
    "S3BucketName=your-bucket-name",    # Change this!
    "S3VideoKey=your-video.mp4"         # Change this!
]

Parameters:

  • S3BucketName: The name of your S3 bucket containing the video
  • S3VideoKey: The S3 key (path/filename) of your video file

3. Build and Deploy

# Build and deploy with SAM
sam build
sam deploy

After deployment, SAM will output the Lambda Function URL.

Usage

The Lambda Function URL accepts two query parameters to dynamically resize the video:

  • width - Target video width (default: 640)
  • height - Target video height (default: 480)

Examples

Default resolution (640x480):

https://your-function-url.lambda-url.us-west-2.on.aws/

Custom resolution (1280x720):

https://your-function-url.lambda-url.us-west-2.on.aws/?width=1280&height=720

You can open these URLs directly in a browser

Architecture

┌─────────┐      ┌──────────┐      ┌────────┐      ┌────────┐
│ Client  │─────▶│  Lambda  │─────▶│   S3   │─────▶│ FFmpeg │
└─────────┘      │ Function │      │ Bucket │      └────────┘
     ▲           │   URL    │      └────────┘           │
     │           └──────────┘                           │
     │                                                  │
     └──────────────────────────────────────────────────┘
              Streaming Response
  1. Client requests video via Lambda Function URL
  2. Lambda fetches video from S3 using environment variables
  3. FFmpeg transcodes and resizes the video stream
  4. Lambda streams the result back to the client in real-time

Configuration

Environment Variables

The Lambda function uses the following environment variables (automatically configured via template.yaml):

  • S3_BUCKET_NAME: S3 bucket containing the video file
  • S3_VIDEO_KEY: S3 key (path) to the video file

These are set through the CloudFormation parameters during deployment.

Lambda Settings

  • Runtime: Node.js 20.x
  • Memory: 256 MB
  • Timeout: 30 seconds
  • Architecture: x86_64

FFmpeg Options

The transcoding uses the following FFmpeg settings (configurable in src/app.ts):

  • Video codec: H.264 (libx264)
  • Preset: ultrafast (optimized for speed)
  • CRF: 23 (quality level, lower = better quality)
  • Audio codec: AAC
  • Audio bitrate: 128k

Security Warning

⚠️ IMPORTANT: This Lambda Function URL is configured as PUBLIC with no authentication. This is FOR TESTING PURPOSES ONLY.

Limitations

  • Video size: Limited by Lambda's memory and timeout settings
  • Processing time: Max 15 minutes (Lambda timeout) - Default: 30 seconds

About

AWS Lambda Response Streaming Video Transformation PoC

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published