Skip to content

Instantly share code, notes, and snippets.

@tgck
Last active December 18, 2015 13:10
Show Gist options
  • Save tgck/5788396 to your computer and use it in GitHub Desktop.
Save tgck/5788396 to your computer and use it in GitHub Desktop.
[sound][sox] コマンドラインで使えるオーディオファイルスライサ。引数で指定した秒数のファイルを複数作成する
#!/bin/bash
# 音声ファイルスライサ
# ファイルの長さが指定した時間で均一になるようスライスする
# ※今のところは.aifのみ対応
#
# ./slicer.sh infile.aif outfile 10
# 引数1:入力ファイル(拡張子含む)
# 引数2:出力ファイル名(拡張子含まず)
# 引数3:出力ファイル一本あたり長さ(秒)
# TODO: 各種拡張子に対応
# 出力ファイル名のゼロ埋め
# 引数チェック
if [ $# -ne "3" ] ; then
echo "invalid arguments."
echo "Usage: $0 infile.aif outfile duration"
exit 1
fi
in=$1
out=$2
unitdur=$3
# ファイルチェック
if [ ! -f $in ]; then
echo "ERROR: file $in does not exist."
exit 1
fi
# ファイルの長さをチェック
# 整数部のみ取り出し
duration=`soxi -D $in | awk -F\. '{print $1}'`
echo "duration:" $duration
# ひとまず余りの秒数は切り捨てとする
unitsNum=`echo $duration / $unitdur | bc`
echo "out units:" $unitsNum
truncatedTime=`echo $duration % $unitdur | bc`
echo "truncated last(seconds):" $truncatedTime
# 実処理
echo "Main Proc..."
for count in `eval echo {1..$unitsNum}`; do
outfilename=`echo ${out}_${count}.aif` # TODO: ゼロ埋め
from=`echo "( $count -1 ) * $unitdur" | bc`
# DRY-RUN
#echo "" | awk '{printf "sox '${in}' '${outfilename}' trim '${from}' '${unitdur}'\n"}'
# EXECUTE
echo "" | awk '{printf "sox '${in}' '${outfilename}' trim '${from}' '${unitdur}'\n"}' | sh ;
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment