sox和shell结合多通道wav文件中抽取特定通道重新排序的脚本

#########################################################################

# File Name: Fetch5ch.sh

# Function :extract 6 1 2 8 channel as a new file from 8channel wave file

# Created Time: Thu 22 Mar 2018 03:27:02 PM CST

#########################################################################

#!/bin/bash

ARGS=2;

E_BADQUIT=127;

E_BADPARAM=65;

if [[ $# -ne ${ARGS} ]];then

        echo -e "Usage: \n\t `(basename $0 .sh)` wave-file-directory out-file-directory"

        exit ${E_BADPARAM}

fi

wave_dir=$1;

out_dir=$2;

if [[ ! -d ${wave_dir} ]];then

        echo "Error : can not find path ${wave_dir}"

        exit ${E_BADPARAM}

fi

if [[ ! -d ${out_dir} ]];then

        echo "Error : cannot find out directory."

        exit ${E_BADPARMA}

else

        is_empty=`ls ${out_dir} | wc -l`

        if [[ ${is_empty} -ne 0 ]];then

                echo "Warning : ${out_dir} is not empty"

        fi

fi

find ${wave_dir}/*.wav > tmp

cat tmp | awk 'BEGIN{FS="/"}{print $NF}' > tmp1

rm tmp

# 检测输入的wave数据的通道数目,本脚本设定的是8

function CheckChannel()

{

wave=$1;

num_ch=`soxi ${wave} | grep -w "Channels" | awk 'BEGIN{FS=":"}{print $NF}'`

if [[ ${num_ch} -lt 8  ]];then

        return 1;

else

        return 0;

fi

}

# 本脚本从8通道数据抽取5个通道,对5个通道重新排列

ch1=6;

ch2=1;

ch3=2;

ch4=8;

ch5=7;

while read line;do

        name="`echo ${line%%.*}`_valid14.wav"

        CheckChannel "${wave_dir}/${line}"

        if [[ `echo $?` -ne 0 ]];then

                echo "Error : channels of ${wave_dir}/${line} is less than 8"

                exit ${E_BADQUIT}

        fi

        echo $wave_dir/$line

        if [[ -f ${out_dir}/${name} ]];then

                echo "Error : ${out_dir} have existed ${name}"

                exit ${E_BADPARAM}

        fi

        echo $wave_dir/$line

        sox ${wave_dir}/${line} ${out_dir}/${name} remix ${ch1} ${ch2} ${ch3} ${ch4} ${ch5}

done < tmp1

rm tmp1

exit $?

你可能感兴趣的:(sox和shell结合多通道wav文件中抽取特定通道重新排序的脚本)