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.

  1. Create the Playlist File (inputs.txt): Inside your text file, define the relative paths to your nested video files using the file directive:

    file 'folder1/video_part1.mp4'
    file 'folder1/nested_folder/video_part2.mp4'
    file 'folder2/video_part3.mp4'
  2. Run the FFmpeg Command: By default, the concat demuxer rejects paths that point to nested directories or external locations for security reasons. You must use the -safe 0 input option to bypass this restriction:

    ffmpeg -f concat -safe 0 -i inputs.txt -c copy output.mp4

    Note: Using -c copy stream-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.

Once merged, run the FFmpeg command on the newly generated master list:

ffmpeg -f concat -safe 0 -i master_playlist.txt -c copy output.mp4

Option 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