はじめに
個人開発のネタとして所有しているMP3ファイルのタグ情報を抽出したくなったので調べてみました🪕
方法
ffmpeg
にくっついてくるffprobe
というコマンドを使うため、Homebrewを使って入れます
$ brew install ffmpeg
以下ようなシェルスクリプトを任意のディレクトリに作成して...
tag2json.bash
# !/bin/bash -eu
input_dir="$(cd "$1"; pwd)"
output_dir="$(cd "$2"; pwd)"
(
cd "$input_dir"
find . -name "*.mp3" |
sort |
while read target_path; do
echo "exporing: $target_path"
output_path="$(dirname "$output_dir/$target_path")/$(basename "$target_path" .mp3).json"
mkdir -p "$(dirname "$output_path")"
ffprobe -v error -show_entries format -of json "$target_path" > "$output_path"
done
)
実行します
ターミナル
# mp3がこんな感じであったとして
$ ll ~/data/music/japanese/さよならポニーテール/ROM/ | head -5
total 545624
-rw-r--r--@ 1 tkhs staff 6.8M 11 14 23:34 1-01 新世界交響楽.mp3
-rw-r--r--@ 1 tkhs staff 6.9M 11 14 23:34 1-02 ナタリー.mp3
-rw-r--r--@ 1 tkhs staff 7.1M 11 14 23:34 1-03 光る街へ.mp3
-rw-r--r--@ 1 tkhs staff 7.2M 11 14 23:34 1-04 遠い日の花火.mp3
$ mkdir out
$ ./tag2json.bash ~/data/music/japanese/さよならポニーテール/ROM/ out/
exporing: ./1-01 新世界交響楽.mp3
exporing: ./1-02 ナタリー.mp3
exporing: ./1-03 光る街へ.mp3
exporing: ./1-04 遠い日の花火.mp3
(略)
# こんな感じでできあがる
$ ll out/ | head -5
total 288
-rw-r--r-- 1 tkhs staff 1.3K 1 5 22:59 1-01 新世界交響楽.json
-rw-r--r-- 1 tkhs staff 1.3K 1 5 22:59 1-02 ナタリー.json
-rw-r--r-- 1 tkhs staff 1.3K 1 5 22:59 1-03 光る街へ.json
-rw-r--r-- 1 tkhs staff 1.3K 1 5 22:59 1-04 遠い日の花火.json
MP3タグだけでなく、ファイルサイズや曲の長さなども取得できます🤗
出力サンプル
{
"format": {
"filename": "./1-01 新世界交響楽.mp3",
"nb_streams": 2,
"nb_programs": 0,
"format_name": "mp3",
"format_long_name": "MP2/3 (MPEG audio layer 2/3)",
"start_time": "0.000000",
"duration": "282.775544",
"size": "7086331",
"bit_rate": "200479",
"probe_score": 51,
"tags": {
"title": "新世界交響楽",
"artist": "さよならポニーテール",
"TCM": "ふっくん",
"album": "ROM",
"track": "1/12",
"TPA": "1/3",
"TS2": "サヨナラポニーテール",
"genre": "Pop",
"iTunPGAP": "0",
"encoded_by": "ミュージック 12.10.1.37",
"iTunNORM": " 000023F4 ...",
"iTunSMPB": " 00000000 00000210 ...",
"iTunes_CDDB_1": "AE0DCD0C+265143+12...",
"iTunes_CDDB_TrackNumber": "1",
"album_artist": "さよならポニーテール",
"TSP": "サヨナラポニーテール",
"date": "2019"
}
}
}
参考
https://qiita.com/PianoScoreJP/items/dcb875757a703c8f3322
https://nico-lab.net/how_to_use_ffprobe/