The error you're encountering:
[Parsed_xstack_16 @ 0x7399f4040f40] Failed to configure output pad on Parsed_xstack_16
[fc#0 @ 0x5673d1d61e80] Error reinitializing filters!
is due to a mismatch between the number of inputs provided to the xstack filter and the number of layout positions specified. Specifically, you're providing more video inputs than the xstack filter is configured to handle based on the layouts you've specified.
Cause of the Error:
- In your
videoMerge array, you are pushing multiple camera inputs ([cam0], [cam1], etc.) when you have multiple camera input files.
- The
xstack filter is then being called with inputs=3 (or more) when you have multiple camera inputs, but you only specify two layout positions in the layout parameter (layout=0_0|w0_0), which is insufficient for more than two inputs.
- The
xstack filter requires that the number of layout positions matches the number of inputs specified in inputs.
Solution:
To fix this error, you need to ensure that:
- The number of inputs to
xstack matches the number of layout positions specified.
- If you have multiple screen or camera inputs that represent contiguous video segments (due to splitting during recording), you should concatenate them before processing.
Here's how you can adjust your code:
1. Concatenate Multiple Camera and Screen Inputs Before Processing:
If your multiple camera and screen input files are split segments of continuous recordings, it would be best to concatenate them into single files for the camera and screen before applying filters. This can be done using the FFmpeg concat demuxer.
For Screen Inputs:
Create a file screenInputs.txt containing:
file '/mnt/myfileshare/recordings/....../screen_input1.mp4'
file '/mnt/myfileshare/recordings/....../screen_input2.mp4'
...
Then concatenate them:
ffmpeg -f concat -safe 0 -i screenInputs.txt -c copy concatenated_screen.mp4
For Camera Inputs:
Similarly, create cameraInputs.txt and concatenate:
ffmpeg -f concat -safe 0 -i cameraInputs.txt -c copy concatenated_camera.mp4
Note: If there are gaps between the recordings or if you need to account for delays, the concat demuxer might not be sufficient. In that case, you'll need to use the concat filter and manually insert silent or blank segments where needed.
2. Adjust Your Filter Complex and Command:
After concatenation, you'll have single camera and screen video files. Modify your code to handle only one camera and one screen input.
Here's how you can change your code:
let command = ["-y", "-hwaccel", "cuda", "-hwaccel_output_format", "cuda"];
// Assume concatenated files:
const screenInputFile = 'concatenated_screen.mp4';
const cameraInputFile = 'concatenated_camera.mp4';
// Add input files
command.push("-c:v", "h264_cuvid", "-i", screenInputFile);
command.push("-c:v", "h264_cuvid", "-i", cameraInputFile);
// Filter complex
let filterComplex = [];
let videoMerge = [];
// Process screen input
filterComplex.push(
`[0]scale=iw:720:force_original_aspect_ratio=decrease,setpts=PTS-STARTPTS+${screenInputs[0].delay}/TB[screen_scaled]`,
`[screen_scaled]pad=iw:720:(ow-iw)/2:(oh-ih)/2[screen]`
);
videoMerge.push("[screen]");
// Process camera input
filterComplex.push(
`[1]scale=480:-1:force_original_aspect_ratio=1,setpts=PTS-STARTPTS+${cameraInputs[0].delay}/TB[cam]`
);
videoMerge.push(`[cam]`);
// Merge video
filterComplex.push(
`${videoMerge.join('')}xstack=inputs=${videoMerge.length}:layout=0_0|w0_0[vout]`
);
// Add filter complex to command
command.push("-filter_complex", filterComplex.join(";"));
// Map outputs
command.push("-map", "[vout]");
// Add encoding options
command.push(
"-c:v",
"h264_nvenc",
"-preset",
"p4",
"-b:v",
"5M",
"-maxrate",
"10M",
"-bufsize",
"15M",
"-shortest",
outputFilePath
);
Explanation:
- By concatenating your multiple screen and camera inputs into single files, you simplify the processing and ensure that timings and delays are handled correctly.
- In the filter complex, you only have two inputs: one for the screen and one for the camera.
- The
xstack filter now has inputs=2 and layout=0_0|w0_0, which matches the number of inputs.
Handling Delays and Gaps:
If you need to handle delays and gaps (inserting blank screens where there's no input), this requires a more complex filter chain. You might need to:
- Generate a blank video segment using the
color filter for the duration of the gap.
- Use the
concat filter to concatenate the blank segments and your input video segments, adjusting for delays.
Here's an example of how you might handle gaps for the screen inputs:
// Assume you have an array of screen inputs with delays and durations
let screenFilterChains = [];
screenInputs.forEach((input, index) => {
const inputIndex = index; // Assuming only screen inputs
filterComplex.push(
`[${inputIndex}]setpts=PTS-STARTPTS+${input.delay}/TB[screen${index}]`
);
screenFilterChains.push(`[screen${index}]`);
});
// Create a blank video to fill gaps
let blankVideoChains = [];
for (let i = 0; i < screenInputs.length - 1; i++) {
const gapDuration = screenInputs[i + 1].delay - (screenInputs[i].delay + screenInputs[i].duration);
if (gapDuration > 0) {
filterComplex.push(
`color=c=black:s=1280x720:d=${gapDuration}[blank${i}]`
);
blankVideoChains.push(`[blank${i}]`);
}
}
// Combine all segments into one screen input
let screenConcatInputs = [];
for (let i = 0; i < screenFilterChains.length; i++) {
screenConcatInputs.push(screenFilterChains[i]);
if (blankVideoChains[i]) {
screenConcatInputs.push(blankVideoChains[i]);
}
}
filterComplex.push(
`${screenConcatInputs.join('')}concat=n=${screenConcatInputs.length}:v=1:a=0[screen]`
);
Note: The above is a simplified example. Handling variable durations, gaps, and ensuring synchronization can be complex and may require more intricate scripting and calculations.
Key Takeaways:
- Ensure that the number of inputs and layout positions in the
xstack filter match.
- Concatenate multiple input files when they represent continuous streams split into segments.
- Handle delays and gaps by inserting blank segments where needed, using filters like
color and concat.
- Adjust your filter chain to process inputs according to your specific requirements, ensuring synchronization and correct timing.
By restructuring your code to handle inputs properly and matching the filter configurations, you should be able to resolve the error and achieve the desired output.