Merge Video with FFmpeg
What is FFmpeg?
FFmpeg is a powerful open-source software suite for handling multimedia data. It can be used to record, convert, and stream audio and video. One of its common uses is merging video and audio files.
The basic command to merge a video file with an audio file is:
bash
ffmpeg -i video.mp4 -i audio.mp4 -c:v copy -c:a aac output.mp4
Installing FFmpeg
Windows
- Visit the official FFmpeg download page: https://ffmpeg.org/download.html
- Click on "Windows Builds"
- Download the latest release
- Extract the zip file
- Add FFmpeg to your system PATH:
- Right-click on "This PC" or "My Computer"
- Click "Properties"
- Click "Advanced system settings"
- Click "Environment Variables"
- Under "System Variables", find and select "Path"
- Click "Edit"
- Click "New"
- Add the path to the FFmpeg bin folder (e.g., C:\ffmpeg\bin)
- Click "OK" to save
macOS
Using Homebrew:
bash
brew install ffmpeg
Batch Merging Videos
Windows Script
Create a new file called merge_videos.bat
with the following content:
batch
@echo off
setlocal enabledelayedexpansion
mkdir merged 2>nul
for %%f in (*1080p.mp4 *720p.mp4) do (
set "filename=%%~nf"
set "quality=!filename:~-5!"
set "basename=!filename:~0,-5!"
if exist "!basename!.audio.mp4" (
echo Merging !basename! with !quality! quality...
ffmpeg -i "%%f" -i "!basename!.audio.mp4" -c:v copy -c:a aac "merged/!basename!.merged.mp4"
)
)
echo All videos have been merged!
pause
Save this script in the same folder as your videos and run it. It will:
- Create a
merged
directory if it doesn't exist - Find all files ending with
1080p.mp4
or720p.mp4
- Look for corresponding
.audio.mp4
files - Merge them and save as
.merged.mp4
in themerged
directory
macOS Script
Create a new file called merge_videos.sh
with the following content:
bash
#!/bin/bash
# Create merged directory if it doesn't exist
mkdir -p merged
# Find all video files with quality suffix (1080p.mp4 or 720p.mp4)
for video in *[0-9]p.mp4; do
if [ -f "$video" ]; then
# Get base name without quality suffix
basename="${video%[0-9]p.mp4}"
# Check if corresponding audio file exists
if [ -f "${basename}.audio.mp4" ]; then
echo "Merging ${basename}..."
ffmpeg -i "$video" -i "${basename}.audio.mp4" -c:v copy -c:a aac "merged/${basename}.merged.mp4"
fi
fi
done
echo "All videos have been merged!"
To use the macOS script:
- Save it as
merge_videos.sh
- Make it executable:bash
chmod +x merge_videos.sh
- Run it:bash
./merge_videos.sh
The script will perform the same operations as the Windows version, creating a merged
directory and combining the video and audio files.
Notes
- Make sure FFmpeg is properly installed and accessible from the command line before running the scripts
- The scripts will match video files with quality suffixes like
1080p.mp4
or720p.mp4
- Each video file should have a corresponding audio file named
filename.audio.mp4
- The merged files will be saved as
filename.merged.mp4
in themerged
directory - The original files will not be modified or deleted