Skip to content

Instantly share code, notes, and snippets.

@yonta
Last active July 10, 2024 09:10
Show Gist options
  • Save yonta/56d484e5d347eb432522bb9db7970408 to your computer and use it in GitHub Desktop.
Save yonta/56d484e5d347eb432522bb9db7970408 to your computer and use it in GitHub Desktop.
東方シリーズ音楽抜き出し機(https://smdn.jp/works/tools/ThbgmExtractor/ )で抜き出したwaveファイルをmp3にタグ付けしつつ変換するスクリプト。Chat GPTで生成したあと、自分好みに変更した。ffprobeでwavの曲情報を取りつつ、lameでVBRでめちゃ遅変換をする。
#!/bin/bash
# Check if correct number of arguments are provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <base_directory>"
echo " which base_directory has 'wav' subdir having *.wav files."
exit 1
fi
# Get the input and output directories from arguments
base_directory=$1
base_album="$(basename "$base_directory")"
wav_directory="$base_directory/wav"
mp3_directory="$base_directory/mp3/$base_album."
# Create the output directory if it doesn't exist
mkdir -p "$mp3_directory"
# Loop over all wav files in the provided wav directory
for wavfile in "$wav_directory"/*.wav;
do
# Extract the base name of the file (without directory and extension)
base_name=$(basename "$wavfile" .wav)
# Define the output mp3 file path
mp3file="$mp3_directory/$base_name.mp3"
# Extract metadata from the wav file
title=$(ffprobe -v quiet -show_entries format_tags=title -of default=noprint_wrappers=1:nokey=1 "$wavfile")
artist=$(ffprobe -v quiet -show_entries format_tags=artist -of default=noprint_wrappers=1:nokey=1 "$wavfile")
# istr=$(ffprobe -v quiet -show_entries format_tags=ISTR -of default=noprint_wrappers=1:nokey=1 "$wavfile")
iwri=$(ffprobe -v quiet -show_entries format_tags=IWRI -of default=noprint_wrappers=1:nokey=1 "$wavfile")
year=$(ffprobe -v quiet -show_entries format_tags=date -of default=noprint_wrappers=1:nokey=1 "$wavfile")
track=$(ffprobe -v quiet -show_entries format_tags=track -of default=noprint_wrappers=1:nokey=1 "$wavfile")
# ifrm=$(ffprobe -v quiet -show_entries format_tags=IFRM -of default=noprint_wrappers=1:nokey=1 "$wavfile")
copyright=$(ffprobe -v quiet -show_entries format_tags=copyright -of default=noprint_wrappers=1:nokey=1 "$wavfile")
# encoder=$(ffprobe -v quiet -show_entries format_tags=encoder -of default=noprint_wrappers=1:nokey=1 "$wavfile")
album="$base_album."
# Use lame to convert the wav file to mp3 and set the tags
lame --ta "$artist" --tt "$title" --ty "$year" --tn "$track" \
--tg "Game" --tv TCOM="$iwri" --tv TCOP="$copyright" \
--tl "$album" --tv TPE2="$artist" \
-q 0 -V 0 --noreplaygain \
"$wavfile" "$mp3file"
# Deleted tags
# --tv TENC="$encoder" --tc "ISTR: $istr IFRM: $ifrm"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment