This guide provides a comprehensive walkthrough on how to create a Bash script for batch audio conversion. This script relies on ffmpeg
, a powerful tool that can convert multimedia files between various formats.
Prerequisites
Before proceeding, make sure ffmpeg
is installed on your system. If not, you can install it using the package manager of your distribution. For example, on Debian-based distributions:
sudo apt-get update
sudo apt-get install ffmpeg
Step 1: Script Initialization
Start by creating a new bash script file:
touch batch_audio_converter.sh
Open the file in your preferred text editor. The first line of any bash script should be the shebang (#!/bin/bash
), which indicates the script should be executed as a bash script:
#!/bin/bash
Step 2: Accepting User Input
We will allow users to provide custom options and settings, specifically:
- Input Directory: Directory containing the audio files to convert.
- Output Directory: Directory to save the converted files.
- Input Format: Original audio file format.
- Output Format: Desired audio file format.
Here’s how to add this feature to your script:
read -p "Enter the input directory: " input_directory
read -p "Enter the output directory: " output_directory
read -p "Enter the input format (e.g., mp3): " input_format
read -p "Enter the output format (e.g., wav): " output_format
Step 3: Input Validation
Next, validate that the input and output directories exist and are readable and writable, respectively:
if [[ ! -d "$input_directory" ]]; then
echo "Input directory does not exist."
exit 1
fi
if [[ ! -d "$output_directory" ]]; then
echo "Output directory does not exist."
exit 1
fi
Step 4: The Conversion Process
Create a loop that goes through all files in the input directory and uses ffmpeg
to convert them:
for file in "$input_directory"/*.$input_format; do
base_name=$(basename "$file" .$input_format)
ffmpeg -i "$file" "$output_directory"/"$base_name".$output_format
done
Step 5: Handling Errors
To handle conversion errors, capture ffmpeg
‘s exit status:
for file in "$input_directory"/*.$input_format; do
base_name=$(basename "$file" .$input_format)
ffmpeg -i "$file" "$output_directory"/"$base_name".$output_format
if [[ $? -ne 0 ]]; then
echo "An error occurred while converting $file."
fi
done
Full Script
Here is the full bash script:
#!/bin/bash
# Prompt user for input
read -p "Enter the input directory: " input_directory
read -p "Enter the output directory: " output_directory
read -p "Enter the input format (e.g., mp3): " input_format
read -p "Enter the output format (e.g., wav): " output_format
# Validate directories
if [[ ! -d "$input_directory" ]]; then
echo "Input directory does not exist."
exit 1
fi
if [[ ! -d "$output_directory" ]]; then
echo "Output directory does not exist."
exit 1
fi
# Convert audio files
for file in "$input_directory"/*.$input_format; do
base_name=$(basename
"$file" .$input_format)
ffmpeg -i "$file" "$output_directory"/"$base_name".$output_format
if [[ $? -ne 0 ]]; then
echo "An error occurred while converting $file."
fi
done
To run the script, use:
bash batch_audio_converter.sh
Or make the script executable:
chmod +x batch_audio_converter.sh
Then run:
./batch_audio_converter.sh
Considerations
This script doesn’t handle files with spaces in their names. If you intend to use it with such files, consider modifying the script accordingly.
Remember, ffmpeg
can convert between a vast number of formats, and some formats may require additional options. This script uses the most basic form of the ffmpeg -i
command, and may not work for all possible format conversions.
This guide provides a basis for a simple batch audio converter, and can be expanded upon to include more complex features, such as logging, verbose output, parallel conversion, and more.