Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

readme.md

FFmpeg Configuration for AWS Lambda

This configuration creates a minimal, optimized FFmpeg binary specifically tailored for audio processing in AWS Lambda environments. The build is focused on producing a small binary size (around 3.6MB) while maintaining essential audio processing capabilities.

Quick Start

  1. Build the Binary

    • Go to the "Actions" tab in your repository
    • Select "ffmpeg-arm64" workflow
    • Click "Run workflow"
    • Download the artifacts from the completed run
  2. Use the Binary

    • Extract the downloaded artifacts
    • Include the FFmpeg binary in your project
    • Follow the configuration-specific instructions below

Key Features

  • Base Image: Built on Amazon Linux 2023 for deploying to AWS Lambda (al2023-arm64 and al2023-x86_64)
  • Minimal Size: Optimized to be as small as possible while maintaining essential functionality
  • Audio-Focused: Configured primarily for audio processing tasks
  • Lambda-Optimized: Built specifically for AWS Lambda's environment and constraints

Included Libraries

  • libfdk_aac: High-quality AAC audio encoder/decoder
  • libmp3lame: MP3 audio encoder/decoder
  • libopus: Opus audio codec for excellent compression

Enabled Functionality

Filters

  • aresample: Audio resampling
  • atempo: Audio tempo/speed adjustment
  • format: Format conversion
  • silencedetect: Silence detection in audio streams
  • volume: Volume adjustment

Muxers (Output Formats)

  • MP3
  • AAC
  • WAV

Encoders

  • libmp3lame (MP3)
  • libfdk_aac (AAC)
  • aac (native AAC)

Protocols

  • file (local file access only)

Demuxers (Input Formats)

  • AAC
  • MP3
  • MOV
  • MP4
  • Matroska
  • WAV

Decoders

  • AAC
  • MP3
  • Opus
  • FLAC
  • PCM formats
  • Vorbis
  • WavPack

Bitstream Filters

  • aac_adtstoasc: AAC ADTS to ASC conversion
  • extract_extradata: Extract extradata from stream

Disabled Features

To minimize binary size, the following components are disabled:

  • Debug information
  • Documentation
  • FFplay
  • Network protocols
  • Hardware acceleration
  • Most filters
  • Most muxers/demuxers
  • Most encoders/decoders
  • Input/output devices

Use Cases

This FFmpeg build is ideal for AWS Lambda functions that need to:

  • Convert between audio formats (MP3, AAC, WAV)
  • Adjust audio volume or speed
  • Detect silence in audio files
  • Extract audio from video files
  • Process basic audio transformations

Example Usage

Here's an example of how to use the FFmpeg binary in a Python Lambda function to extract and convert audio:

import subprocess
import logging
import os

def extract_audio_segment(input_file: str, output_file: str, start_time: float, end_time: float) -> bool:
    """
    Extract an audio segment from a media file and convert it to MP3.

    Args:
        input_file: Path to the input media file
        output_file: Path where the output MP3 will be saved
        start_time: Start time in seconds (e.g., 10.5)
        end_time: End time in seconds (e.g., 30.0)

    Returns:
        bool: True if successful, False otherwise
    """
    ffmpeg_path = '/opt/ffmpeg'  # Path in AWS Lambda

    try:
        cmd = [
            ffmpeg_path,
            '-i', input_file,
            '-ss', f'{start_time:.3f}',
            '-to', f'{end_time:.3f}',
            '-map', '0:a',  # Only select audio streams
            '-vn',  # No video processing
            '-c:a', 'libmp3lame',
            '-b:a', '192k',
            '-y',  # Overwrite output file if exists
            output_file
        ]

        logging.info(f"Executing FFmpeg command: {' '.join(cmd)}")
        subprocess.run(cmd, check=True, capture_output=True, text=True)
        return True

    except subprocess.CalledProcessError as e:
        logging.error(f"FFmpeg error: {e.stderr}")
        return False
    except Exception as e:
        logging.error(f"Unexpected error: {str(e)}")
        return False

# Example usage in a Lambda handler
def lambda_handler(event, context):
    try:
        # Example: Extract 20 seconds of audio starting at 10 seconds
        success = extract_audio_segment(
            input_file='/tmp/input.mp4',
            output_file='/tmp/output.mp3',
            start_time=10.0,
            end_time=30.0
        )

        if success:
            return {
                'statusCode': 200,
                'body': 'Audio extraction successful'
            }
        else:
            return {
                'statusCode': 500,
                'body': 'Audio extraction failed'
            }

    except Exception as e:
        return {
            'statusCode': 500,
            'body': f'Error: {str(e)}'
        }

This example demonstrates:

  • Extracting a specific time segment from a media file
  • Converting to MP3 format with high-quality settings
  • Proper error handling and logging
  • Integration with AWS Lambda
  • Using the binary from the correct Lambda path

License Notice

This build includes components under GPL and other licenses. The FDK-AAC codec is covered by a non-free license. Please review the FFmpeg License for details about licensing requirements if you distribute this binary.