0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【忘備録】iTunesとかで読み込んだ音楽ファイルからexiftoolでアルバム名のタイトルだけ取得するコマンド

Last updated at Posted at 2025-02-11

コマンド概要

Albumという項目で余分なスペースを無視してawkで抽出

exiftool filename | awk -F ': ' '{gsub(/[ \t]+$/, "", $1); if ($1=="Album") print $2}'

ポイント

  • -F ': ' で : とスペースを区切り文字にする
  • gsub(/[ \t]+$/, "", $1) で $1 の末尾の空白やタブを削除
  • if ($1=="Album") で正しく判定

追加(参考)

pythonで書いた場合

  • exiftoolを使った場合(json処理)

import subprocess
import json

def get_album(filename):
    result = subprocess.run(["exiftool", "-json", filename], capture_output=True, text=True)
    metadata = json.loads(result.stdout)
    return metadata[0].get("Album")  # Album がない場合は None になる

filename = "your_audio_file.m4a"
album_name = get_album(filename)
print("Album:", album_name)
  • subprocessを使った場合

import subprocess
import re

def get_album(filename):
    result = subprocess.run(["exiftool", filename], capture_output=True, text=True)
    for line in result.stdout.splitlines():
        if line.strip().startswith("Album"):
            return re.split(r'\s*:\s*', line, maxsplit=1)[1]
    return None  # Album情報がない場合

filename = "your_audio_file.m4a"
album_name = get_album(filename)
print("Album:", album_name)

感想

shellってなんだかんだで簡単に書けるよね。。。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?