!/bin/bash
检查是否传入输入目录参数
if [ -z "$1" ]; then
echo "Usage: $0 <input_dir> [output_dir]"
exit 1
fi
获取输入和输出目录
INPUT_DIR="$1"
OUTPUT_DIR="${2:-./mp3_files}" # 如果未指定 output_dir,则默认为当前目录下的 mp3_files
检查输入目录是否存在
if [ ! -d "$INPUT_DIR" ]; then
echo "Error: Input directory '$INPUT_DIR' does not exist."
exit 1
fi
创建输出目录(如果不存在)
mkdir -p "$OUTPUT_DIR"
遍历输入目录中的所有 .mp4 文件
for mp4_file in "$INPUT_DIR"/*.mp4; do
# 检查是否有匹配的文件
if [ ! -f "$mp4_file" ]; then
echo "No .mp4 files found in $INPUT_DIR"
exit 0 # 正常退出,不算错误
fi
# 生成输出的 .mp3 文件名
filename=$(basename "$mp4_file" .mp4)
mp3_file="$OUTPUT_DIR/$filename.mp3"
# 如果 mp3 文件已存在则跳过
if [ -f "$mp3_file" ]; then
echo "File $mp3_file already exists, skipping..."
continue
fi
# 提取音频并保存为 mp3
ffmpeg -i "$mp4_file" -q:a 0 -map a "$mp3_file" -y
if [ $? -eq 0 ]; then
echo "Successfully extracted audio from $mp4_file to $mp3_file"
else
echo "Failed to extract audio from $mp4_file"
fi
done
echo "Batch processing completed."
标签:批量,音频,echo,mp4,mp3,file,INPUT,DIR From: https://www.cnblogs.com/netstat/p/18516171