To print multiple file paths to a text file without double quotes or new lines for FFmpeg, use the Windows command prompt's
for loop combined with the
echo command and output redirection. By iterating through your video files and appending each path directly to an input list file using the
>> operator, you create a clean
concat demuxer script. This approach avoids the extra whitespace and quotation marks that standard shell expansions often introduce, ensuring FFmpeg reads the file list correctly during batch processing.
Understanding the Concat Demuxer Requirement
FFmpeg’s
concat protocol is one of the most efficient ways to join multiple video files together without re-encoding them. Unlike the legacy
concat filter which requires complex filtering graphs, the demuxer simply reads a list of files and concatenates their streams sequentially. However, this method is strict about formatting. It expects a plain text file where each line contains exactly
file 'path/to/video.mp4'. If you include double quotes around the entire path or add unexpected newline characters, FFmpeg will fail to parse the input, resulting in errors like "Invalid argument" or "No such file or directory."
The core challenge arises when generating this list programmatically. Many users attempt to use simple shell commands that inadvertently wrap paths in quotes or leave trailing spaces. To solve this, we need a method that outputs the raw path string, prefixed by the keyword
file, followed by a single quote, the path, another single quote, and a newline character, all without any surrounding double quotes from the system shell.
Generating the Input List on Windows
For Windows users, the Command Prompt (cmd.exe) provides a robust way to handle this using a
for loop. The goal is to iterate over every
.mp4 file in the current directory and append the correctly formatted line to a file named
input.txt.
Create a batch script or run the following command in your terminal:
for /f "delims=" %i in ('dir /b *.mp4') do echo file '%i' >> input.txt
Let's break down why this works. The
dir /b *.mp4 part lists all MP4 files in bare format, meaning it returns only the filenames without headers or summary information. The
for /f "delims=" loop processes this output. The
delims= option is crucial; it tells the loop not to split the filename at spaces, preserving paths that contain spaces as single entities. Inside the loop,
echo file '%i' prints the literal string
file , followed by the variable
%i (the filename), enclosed in single quotes. Finally,
>> input.txt appends this line to your input file rather than overwriting it.
If you are running this inside a batch file (.bat), remember to double the percent signs:
%%i.
Executing the FFmpeg Concatenation
Once your
input.txt file is generated, verify its contents. It should look like this:
file 'video1.mp4'
file 'video2.mp4'
file 'video3.mp4'
Notice there are no double quotes around the paths, and each entry is on a new line. You can now run FFmpeg using this list:
ffmpeg -f concat -safe 0 -i input.txt -c copy output.mp4
The
-f concat flag tells FFmpeg to use the concat demuxer. The
-safe 0 flag is necessary if your file paths contain special characters or if you are using absolute paths, though it is generally recommended for safety when using dynamic file lists. The
-c copy flag ensures that FFmpeg copies the audio and video streams directly without re-encoding, making the process extremely fast.
For more details on testing commands before execution, see Possible Test FFmpeg Command Without Actual Running.
Handling Special Characters and Spaces
A common pitfall occurs when file names contain spaces. If you use a standard
for loop without the
delims= parameter, the loop might interpret
my video.mp4 as two separate items:
my and
video.mp4. This results in malformed lines in your input file, such as
file 'my and
file 'video.mp4', which causes FFmpeg to crash.
By explicitly setting
delims=, you ensure that the entire filename is treated as a single token. Additionally, if you are working on Linux or macOS, the equivalent logic uses a
while read loop:
find . -name "*.mp4" -type f | while IFS= read -r file; do echo "file '$file'" >> input.txt; done
This Bash approach similarly preserves spaces and generates the correct syntax for the concat demuxer.
Alternative Approaches for Complex Lists
If your file list is extremely large or located in nested directories, manual generation becomes tedious. In such cases, consider using scripting languages like Python to generate the list. Python gives you precise control over string formatting, allowing you to easily escape characters or handle different operating system path separators.
However, for most batch processing tasks involving a flat directory structure, the Command Prompt or Bash methods described above are sufficient and require no additional dependencies. They leverage built-in tools to produce the exact output format FFmpeg expects. For related techniques, check out
How to Create Animated GIF Sequence Image Files using FFmpeg, which also involves handling multiple input files efficiently.
Frequently Asked Questions
Why does FFmpeg say "Invalid argument" when using concat?
This error usually means the input file list is malformed. Ensure that each line starts with the word
file followed by a space and a single-quoted path. Double-check that there are no trailing spaces after the closing single quote and that the file encoding is UTF-8 without a BOM (Byte Order Mark).
Can I use absolute paths in the concat input file?
Yes, but you must include the
-safe 0 flag in your FFmpeg command. By default, FFmpeg restricts file access to the current directory for security reasons. Absolute paths bypass this restriction, so the flag is required to prevent "Unsafe file name" errors.
How do I remove the input file after concatenation?
You can add a cleanup step to your batch script or command line. After the FFmpeg command succeeds, simply delete the temporary list file using
del input.txt on Windows or
rm input.txt on Linux/macOS to keep your workspace tidy.
What if my videos have different resolutions?
The concat demuxer requires all input files to have identical codecs, resolutions, and frame rates. If your videos differ, you cannot use this method. Instead, you must use the video filter graph approach (
[0:v][0:a][1:v][1:a]...concat=n=2:v=1:a=1) which allows for resizing and format conversion during the join process.
Does this method work for audio files too?
Yes, the concat demuxer works identically for audio files. Simply change the file extension in your search pattern (e.g.,
*.wav or
*.mp3) and adjust the output file extension accordingly. The formatting requirements for the input list remain exactly the same regardless of media type.