|
set -eo pipefail |
|
|
|
function bms2wav() { |
|
local _bms="$1" |
|
local _wav="$2" |
|
|
|
bms-renderer "$_bms" "$_wav" |
|
} |
|
|
|
function remove_silent_section() { |
|
local _wav_path="$1" |
|
local _tmp="${_wav_path}.silenced.wav" |
|
|
|
sox "$_wav_path" "$_tmp" vad |
|
mv -f "$_tmp" "$_wav_path" |
|
} |
|
|
|
function create_preview() { |
|
local _wav_path="$1" |
|
local _preview_path="$2" |
|
|
|
ffmpeg \ |
|
-i "$_wav_path" \ |
|
-t 10 \ |
|
-vn -ab 160k -acodec libvorbis -f ogg \ |
|
-loglevel warning \ |
|
"$_preview_path" |
|
} |
|
|
|
function find_bms_files() { |
|
local _bms_dir="$1" |
|
|
|
find "$_bms_dir" -name '*.bme' -o -name '*.bms' -o -name '*.bml' |
|
} |
|
|
|
# https://github.com/exch-bms2/beatoraja/issues/400 |
|
function find_preview_files() { |
|
local _bms_dir="$1" |
|
|
|
find "$_bms_dir" -name 'preview*.wav' -o -name 'preview*.ogg' |
|
} |
|
|
|
function bms2preview() { |
|
local _bms_dir="$1" |
|
local _tmp_wav="${_bms_dir}/tmp.wav" |
|
local _preview_path="${_bms_dir}/preview_music.ogg" |
|
|
|
local _bms |
|
_bms="$(find_bms_files "$_bms_dir" | head -n1)" |
|
|
|
if [[ -z "$_bms" ]]; then |
|
echo "BMS file was not found: ${_bms_dir}" |
|
exit 1 |
|
fi |
|
|
|
if [[ -n "$(find_preview_files "$_bms_dir")" ]]; then |
|
echo "A preview file is already exists" |
|
exit |
|
fi |
|
|
|
echo "convert BMS files into a wav file: ${_bms_dir}" |
|
bms2wav "$_bms" "$_tmp_wav" |
|
echo "remove a silent section from the wav file: ${_bms_dir}" |
|
remove_silent_section "$_tmp_wav" |
|
echo "create a preview file from the wav file: ${_bms_dir}" |
|
create_preview "$_tmp_wav" "$_preview_path" |
|
rm "$_tmp_wav" |
|
} |
|
|
|
target_dir="$1" |
|
|
|
if [[ -z "$target_dir" ]]; then |
|
echo "BMS directory's path should be passed" |
|
exit 1 |
|
fi |
|
|
|
if [[ ! -d "$target_dir" ]]; then |
|
echo "The path does not exist: ${target_dir}" |
|
exit 1 |
|
fi |
|
|
|
echo "start: ${target_dir}" |
|
bms2preview "$target_dir" |
|
echo "end: ${target_dir}" |