FFmpeg Concat Demuxer with Different Directories
The FFmpeg concat demuxer allows you to seamlessly join
media files without re-encoding, but referencing files across different
folders requires specific formatting in your input text file. This guide
explains how to properly structure your text file using absolute and
relative paths, handle path escaping, and run the required FFmpeg
command to merge files located in separate directories.
The Basic File Syntax
A standard input file for the concat demuxer contains
lines starting with the keyword file, followed by the path
to the media file enclosed in single quotes.
file '/path/to/first_folder/video1.mp4'
file '/path/to/second_folder/video2.mp4'
file '/path/to/third_folder/video3.mp4'
1. Using Absolute Paths
When files reside across completely separate drives or unrelated folder structures, absolute paths are the most reliable method.
Linux / macOS:
file '/home/user/videos/intro.mp4'
file '/var/media/content/main.mp4'
file '/home/user/downloads/outro.mp4'
Windows: Use forward slashes (/)
instead of backslashes (\) for folder separation to prevent
parsing issues:
file 'C:/Users/User/Videos/intro.mp4'
file 'D:/Projects/Media/main.mp4'
file 'C:/Users/User/Downloads/outro.mp4'
2. Using Relative Paths
If your files are located in subdirectories relative to the location of the text file, use relative path navigation:
file 'subfolder1/video1.mp4'
file 'subfolder2/video2.mp4'
file '../sibling_folder/video3.mp4'
Handling Spaces and Special Characters
If any directory or file name contains a single quote, space, or special character, you must escape it:
- Single quotes: Break the quote and escape it using
'\''or escape with a backslash. - Backslashes: If backslashes are required, double
them (
\\).
Example with spaces and single quotes:
file '/media/holiday 2023/john\'s_clip.mp4'
Running the FFmpeg Command
By default, the concat demuxer rejects absolute paths
and relative paths with .. for security reasons. To allow
paths pointing to different directories, you must pass the
-safe 0 flag before the input flag -i.
Run the following command in your terminal:
ffmpeg -f concat -safe 0 -i filelist.txt -c copy output.mp4-f concat: Specifies the concat demuxer.-safe 0: Disables file path safety restrictions, permitting absolute and parent-directory paths.-i filelist.txt: Specifies your formatted text file.-c copy: Copies the audio and video streams without re-encoding (requires files to have matching codecs and parameters).