'How to make bunch of stereo files from bunch of mono files with ffmpeg in bash?

I have two folders "Left channel" and "Right channel". Each folder contains mono files with same names. Example: "Left channel" contains "A.wav", "B.wav", "C.wav" and "Right channel" contains "A.wav", "B.wav", "C.wav". I need to make stereo files for each mono files.

So I have to combine

ffmpeg -i left.mp3 -i right.mp3 -filter_complex "[0:a][1:a]join=inputs=2:channel_layout=stereo[a]" -map "[a]" output.mp3 

and

for file in /dir/* do ffmpeg -i ...; done

How can I go through all mono files and make bunch of stereo files from these mono files with ffmpeg in bash?



Solution 1:[1]

Would you please try the following:

#!/bin/bash

lch="Left channel"; rch="Right channel" # directory names of wav files
for f in "dir/$lch/"*.wav; do
    fname=${f##*/}                      # filename such as "A.wav"
    outfile="output_${fname%.*}.mp3"    # output filename such as "output_A.mp3"
    if [[ -f dir/$lch/$fname && dir/$rch/$fname ]]; then
        echo ffmpeg -i "dir/$lch/$fname" -i "dir/$rch/$fname" -filter_complex "[0:a][1:a]join=inputs=2:channel_layout=stereo[a]" -map "[a]" "$outfile"
    fi
done

It just outputs the command line as a dry run. If the output looks good, drop echo and run again.
Please note the output of echo removes the double quotes around the filenames. If you copy the output of echo and execute it on the command line, it will not work well.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1