FFmpeg example to overlay one video on top of another using the overlay filter

CK CK 3828 
Created at  
41 0 0

You can use FFmpeg to overlay one video on top of another using the overlay filter. Here's an example command that demonstrates how to do this:

ffmpeg -i background.mp4 -i overlay.mp4 -filter_complex "[1][0]scale2ref[overlay][base];[base][overlay]overlay=W-w:H-h" -c:a copy output.mp4

Here's what this command does:

  1. -i background.mp4: Specifies the background video file.
  2. -i overlay.mp4: Specifies the overlay video file.
  3. -filter_complex: Applies complex filters to the input videos.
  4. [1][0]scale2ref[overlay][base];: Scales the overlay video relative to the background video.
  5. [base][overlay]overlay=W-w:H-h: Places the overlay video in the bottom-right corner of the background video.
  6. -c:a copy: Copies the audio from the background video without re-encoding it.
  7. output.mp4: Specifies the output file name.

You can adjust the overlay filter parameters to position the overlay video as needed:

  • overlay=W-w:H-h: Bottom-right corner
  • overlay=0:0: Top-left corner
  • overlay=W-w:0: Top-right corner
  • overlay=0:H-h: Bottom-left corner
  • overlay=x:y: Custom position where x and y are the coordinates.

For example, to place the overlay video in the top-left corner, you can use:

ffmpeg -i background.mp4 -i overlay.mp4 -filter_complex "[1][0]scale2ref[overlay][base];[base][overlay]overlay=0:0" -c:a copy output.mp4

Feel free to modify the command to suit your specific needs.

 

In addition, you can change the size of the overlay video by using the scale filter in conjunction with the overlay filter. Here's an example command that demonstrates how to resize the overlay video before placing it on top of the background video:

ffmpeg -i background.mp4 -i overlay.mp4 -filter_complex "[1:v]scale=320:240[overlay];[0:v][overlay]overlay=10:10" -c:a copy output.mp4

Here's what this command does:

  1. -i background.mp4: Specifies the background video file.
  2. -i overlay.mp4: Specifies the overlay video file.
  3. -filter_complex: Applies complex filters to the input videos.
  4. [1:v]scale=320:240[overlay];: Scales the overlay video to 320x240 pixels.
  5. [0:v][overlay]overlay=10:10: Places the resized overlay video at coordinates (10, 10) on the background video.
  6. -c:a copy: Copies the audio from the background video without re-encoding it.
  7. output.mp4: Specifies the output file name.

You can adjust the scale parameters to set the desired size for the overlay video, and the overlay parameters to set the desired position.

For example, if you want to resize the overlay video to 640x480 pixels and place it in the bottom-right corner, you can use:

ffmpeg -i background.mp4 -i overlay.mp4 -filter_complex "[1:v]scale=640:480[overlay];[0:v][overlay]overlay=W-w:H-h" -c:a copy output.mp4

Here's a breakdown of this command:

  1. [1:v]scale=640:480[overlay];: Scales the overlay video to 640x480 pixels.
  2. [0:v][overlay]overlay=W-w:H-h: Places the resized overlay video in the bottom-right corner of the background video. W-w and H-h ensure the overlay is positioned at the bottom-right corner, where W and H are the width and height of the background video, and w and h are the width and height of the overlay video.

This way, you can change both the size and the position of the overlay video as needed.

Tags FFmpeg FFmpeg overlay FFmpeg scale overlay video Facebook X
Comments 0
  1. A Deep Dive Into Internet Advertising Ecosystem on Video Streaming Industry
    687
  2. When to use #EXT-X-DISCONTINUITY in HLS?
    1,590
  3. A prefect Apache server settings to avoid CORS error for HLS
    641
  4. CloudFront Settings for Dynamic HLS Streaming
    679
  5. SafeFrame is a web standard developed by IAB
    546
  6. CSAI vs SSAI
    615
  7. Ad Marker: SCTE-35 Message Encoder based in Python
    841
  8. Ad marker: SCTE-35 enhanced
    610
  9. FFmpeg compile options based on configure
    1,189
  10. Script checking internet restriction with FFmpeg
    729
  11. When HTTP redirection is not working with ffmpeg in Amazon EC2 Environment
    491
  12. FFmpeg compile option to support SCTE-35
    876
  13. HTTP Proxy when your URL access is blocked with FFmpeg
    2,086
  14. Limit Maximum Resolution in HLS.js
    703
  15. Magic line when HLS video manifest is mixed with encrypted / unencrypted video chunks
    1,681
  16. Recovering a crashed HLS.js player
    1,374
  17. How do I create animated background for Google Meet?
    27,220
  18. Secure HLS encoding using AES-128 encryption with FFmpeg
    1,206
  19. Machine Learning Models for Ad Detection from a Video Stream
    253
  20. Unlock Flawless HLS Audio Sync: The Power of #EXT-X-INDEPENDENT-SEGMENTS
    425
  21. NVIDIA CUDA library which can be integrated with C/C++
    406
  22. Approaches to Scene Detection in Video Processing
    702
  23. FFmpeg CLI to remove audio track from MP4
    594
  24. Building GPU as a Service aka Dockerized GPU
    1,876
  25. IAB Categories (a.k.a. OpenRTB Code)
    1,055
  26. FFmpeg library that can be directly integrated into C/C++/Rust/Python without CLI
    2,504
  27. HTTP Live Streaming (HLS) was developed by Apple as part of QuickTime X in iOS3.0, and became an official standard (RFC 8126) in 2017
    635
  28. Progressive MP4 is an application of streaming techniques using the standardized MP4 container invented by MPEG LA consortium
    1,145
  29. FFmpeg CLI to mix two different audios in a single audio file such as mp3
    666
  30. FFmpeg CLI to merge a video only mp4 and mp3 audio file
    393
Recently updated
  1. How to Reinstall Microsoft Office After Formatting Your PC via Microsoft Account
    106
  2. Mastering tmux for Continuous Engineering and AI Workflows
    90
  3. What Are H5 Games Ads?
    685
  4. How to Bypass VPN for Specific cURL Requests on Mac
    138
  5. Demystifying BGP and OSPF: A Complete Guide to Network Routing Protocols
    579
  6. What Is a Canonical Redirect and How Can It Boost Your SEO?
    92
  7. How to Set Up a GitHub Organization: Requirements and Step-by-Step Guide
    137
  8. What Is CMAF? Guide to Common Media Application Format in Streaming
    116
  9. Google OAuth Token Expiration: Causes and Solutions
    191
  10. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    141
  11. Why SPF and DKIM Are Essential for Linux Email Servers?
    137
  12. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    850
  13. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    270
  14. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    246
  15. Array of arguments passed to script in PHP
    172
  16. Invalid Auction Storms in Ad Tech
    191
  17. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    301
  18. The Two Clocks of Ad Server Architecture
    183
  19. Frequency Cap Architecture Overview
    651
  20. Collecting Proof-of-Play from 100,000 Screens
    262
  21. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    448
  22. What is RedisVL?
    198
  23. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    430
  24. TPU & GPU Cluster Management Tools
    469
  25. The Concept of Scaffolding in AI Agents
    409
  26. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    731
  27. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    403
  28. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    531
  29. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    308
  30. Machine Learning: Training vs. Ranking
    347