How to Use FFmpeg Concat with Nested Playlists
Using the FFmpeg concat demuxer with nested playlists or
nested file structures allows you to merge multiple video segments
organized across different directories. This article provides a
straightforward guide on how to configure your playlist files for nested
directory structures, how to resolve path safety restrictions, and how
to handle multiple hierarchical playlist files.
Handling Nested File Paths in a Playlist
When your media files are located in nested subdirectories rather than the same folder as your playlist text file, you must format the playlist file to point to these relative paths and configure FFmpeg to allow them.
Create the Playlist File (
inputs.txt): Inside your text file, define the relative paths to your nested video files using thefiledirective:file 'folder1/video_part1.mp4' file 'folder1/nested_folder/video_part2.mp4' file 'folder2/video_part3.mp4'Run the FFmpeg Command: By default, the
concatdemuxer rejects paths that point to nested directories or external locations for security reasons. You must use the-safe 0input option to bypass this restriction:ffmpeg -f concat -safe 0 -i inputs.txt -c copy output.mp4Note: Using
-c copystream-copies the video and audio packets without re-encoding, preserving the original quality and completing the process almost instantly.
Merging Multiple Separate Playlist Files
FFmpeg’s concat demuxer does not natively support
recursive parsing (meaning you cannot list a .txt playlist
file inside another .txt playlist file using the
file directive). If you have multiple nested playlists that
you want to combine, you must flatten them.
Option 1: Flattening Playlists via Command Line
You can quickly combine multiple separate playlist files into a single master playlist using standard operating system commands.
On Linux/macOS:
cat playlist1.txt playlist2.txt > master_playlist.txtOn Windows (Command Prompt):
type playlist1.txt playlist2.txt > master_playlist.txt
Once merged, run the FFmpeg command on the newly generated master list:
ffmpeg -f concat -safe 0 -i master_playlist.txt -c copy output.mp4Option 2: Process Substitution (Unix-based Systems)
If you want to avoid creating a temporary file on your hard drive, you can use bash process substitution to merge the playlists on the fly:
ffmpeg -f concat -safe 0 -i <(cat playlist1.txt playlist2.txt) -c copy output.mp4