Ubuntu 基于sox音频处理工具shell脚本批量处理音频文件
Ubuntu 基于sox音频处理工具shell脚本批量处理音频文件
- 安装SOX
- 音频声道采样率转换
- 音频文件信息
- 音频播放速度处理
- 音频音量调节
- M4A转MP3
- MP4转MP3
- PCM转WAV
- WAV转MP3
- 文件名空格转下划线
安装SOX
sudo apt-get install sox
:~$ sox --version
sox: SoX v14.4.1
音频声道采样率转换
audio_convert.sh
#! /bin/sh
old=$1
new=$2
channels=$3
samplerate=$4
function suage_help()
{
echo "./audio_convert.sh ./old_dir ./new_dir channels samplerate"
echo "./audio_convert.sh ./old_dir ./new_dir 1 16000"
exit 0
}
echo "${old}"
echo "${new}"
if [ $# -lt 4 ];then
suage_help
fi
for file in $old/*;
do
#echo $file
echo ${file##*/}
#sox $file -c 1 -r 16000 "$new/${file##*/}"
echo ${file}
sox "${file}" -c ${channels} -r ${samplerate} "$new/${file##*/}"
done
音频文件信息
audio_info.sh
#! /bin/sh
#old=$(pwd)/sysaudios
#new=$(pwd)/new
dir=$1
out=$2
function suage_help()
{
echo "./audio_info.sh ./dir"
exit 0
}
echo "${dir}"
if [ $# -lt 1 ];then
suage_help
fi
for file in $dir/*;
do
echo $file
soxi "${file}"
soxi "${file}" >> "${out}"
done
音频播放速度处理
audio_speed.sh
#! /bin/sh
old_dir=$1
new_dir=$2
function suage_help()
{
echo "./audio_speed.sh old_dir new_dir temo speed"
echo "./audio_speed.sh ./old_dir ./new_dir 1.33 1"
exit 0
}
if [ $# -lt 4 ];then
suage_help
fi
echo "${old_dir}"
echo "${new_dir}"
for file in $old_dir/*;
do
#echo $file
echo ${file##*/}
sox $file "$new_dir/${file##*/}" tempo $3 speed $4
done
音频音量调节
audio_volume.sh
#! /bin/sh
volume=$1
old=$2
new=$3
function suage_help()
{
echo "./audio_volume.sh volume ./old_dir ./new_dir"
echo "./audio_volume.sh 0.85 ./old_dir ./new_dir"
exit 0
}
echo "${old}"
echo "${new}"
if [ $# -lt 3 ];then
suage_help
fi
for file in $old/*;
do
#echo $file
echo ${file##*/}
#sox -v 0.8 $file "$new/${file##*/}"
echo ${file}
sox -v ${volume} "${file}" "$new/${file##*/}"
done
M4A转MP3
m4a2mp3.sh
#! /bin/sh
#old=$(pwd)/sysaudios
#new=$(pwd)/new
m4a=$1
mp3=$2
function suage_help()
{
echo "./m4a2mp3.sh ./m4a_dir ./mp3_dir"
echo "./m4a2mp3.sh ./m4a_dir ./mp3_dir"
exit 0
}
echo "${m4a}"
echo "${mp3}"
if [ $# -lt 2 ];then
suage_help
fi
for file in $m4a/*;
do
#echo $file
echo ${file} # 目录/文件名.后缀
echo ${file%.*} # 目录/文件名
echo ${file##*/} # 文件名.后缀
files=${file##*/} # 文件名.后缀
filename=${files%.*} # 文件名
echo ${filename}
echo ${file##*.} # 后缀
#newfile=`echo $file | sed -e 's/ /_/g'`
#echo ${newfile}
ffmpeg -i "${file}" "$mp3/${filename}.mp3"
done
MP4转MP3
mp42mp3.sh
#! /bin/sh
#old=$(pwd)/sysaudios
#new=$(pwd)/new
mp4=$1
mp3=$2
function suage_help()
{
echo "./mp42mp3.sh ./mp4_dir ./mp3_dir"
echo "./mp42mp3.sh ./mp4_dir ./mp3_dir"
exit 0
}
echo "${mp4}"
echo "${mp3}"
if [ $# -lt 2 ];then
suage_help
fi
for file in $mp4/*;
do
#echo $file
echo ${file} # 目录/文件名.后缀
echo ${file%.*} # 目录/文件名
echo ${file##*/} # 文件名.后缀
files=${file##*/} # 文件名.后缀
filename=${files%.*} # 文件名
echo ${filename}
echo ${file##*.} # 后缀
#newfile=`echo $file | sed -e 's/ /_/g'`
#echo ${newfile}
ffmpeg -i "${file}" "$mp3/${filename}.mp3"
done
PCM转WAV
pcm2wav.sh
#! /bin/sh
pcm_dir=$1
wav_dir=$2
channels=$3
bits=$4
samplerate=$5
function usage_help()
{
echo "pcm file to wav file:"
echo "./pcm2wav.sh pcm_dir wav_dir channels bits samplerate"
echo "./pcm2wav.sh ./pcm_dir ./wav_dir 1 16 16000"
exit 0
}
if [ $# -lt 5 ];then
usage_help
fi
pcm_dir=${pcm_dir}/
wav_dir=${wav_dir}/
echo "pcm_dir:${pcm_dir}"
echo "wav_dir:${wav_dir}"
echo "channels:${channels} bits:${bits} samplerate:${samplerate}"
for pcm_file in $pcm_dir*;
do
#echo ${pcm_file%%.*} #pcm.tar.gz
pcm=${pcm_file%.*} #pcm001.pcm
#echo ${pcm_file} ${pcm_file##*.}
wav=$wav_dir${pcm##*/}.wav
#echo ${pcm}
#echo ${wav}
echo ${pcm_file}
#sox -t raw -c 1 -e signed-integer -b 16 -r 16000 ${pcm_file} ${wav}
sox -t raw -c ${channels} -e signed-integer -b ${bits} -r ${samplerate} ${pcm_file} ${wav}
done
WAV转MP3
wav2mp3.sh
#! /bin/sh
wav_dir=$1
mp3_dir=$2
function usage_help()
{
echo "wav file to mp3 file:"
echo "./wav2mp3.sh wav_dir mp3_dir"
echo "./wav2mp3.sh ./wav_dir/ ./mp3_dir/"
exit 0
}
if [ $# -lt 2 ];then
usage_help
fi
for file in ${wav_dir}/*.wav;
do
echo ${file}
wavfile=${file%.*}
mp3file=$mp3_dir/${wavfile##*/}.mp3
#echo $wavfile $mp3file
sox $file $mp3file
done
文件名空格转下划线
filename_space2underline.sh
#! /bin/sh
dir1=$1
dir2=$2
channels=$3
samplerate=$4
echo "${dir1}"
echo "${dir2}"
for file in $dir1/*.mp3;
do
#echo ${file##*/}
#echo ${file}
echo $file
newfile=`echo $file | sed -e 's/ /_/g'`
echo $newfile
#mv "$file" $newfile
sox "${file}" -c ${channels} -r ${samplerate} "$dir2/${file##*/}"
done