7
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Python】iTunesで聞いてる曲の情報を取得する

Last updated at Posted at 2020-10-06

概要

MacのiTunesで今聞いている曲の情報を取得できないかなぁとふと思い調べたら
AppleScriptでそれが実現するらしいが,
今まで触ったことが一ミリもなく諦めようとしたところ
Pythonのappscriptというモジュールでそれが可能ということみたいなので試してた

環境

Python 3.6.0

環境構築

まずはpythonを動かすための環境をvenvで構築をする(別にvenvを使わなくてもいい)

$ python3 -m venv venv
$ source venv/bin/activate
(venv)$ pip3 install appscript

このモジュールの使い方自体の記事もなかったですがgithubのドキュメントをなんとか見つけたのでまずはベースは参考にしました。
アプリケーションごとのドキュメントが全くないので

あとはここに書いてある通りAppleScriptをappscriptに落とし込めばいけそうな気がしたので,
だいたいのメソッド等は力技でこんな感じだろうで試して, Pythonの記法に落とし込みました。
※正直かなり疲れた

実装

前提スクリプト

モジュールのimport等, 大前提として必要なものは切り出しておきます。

getiTunesInfo.py
from aeosa import appscript
it = appscript.app('iTunes')

まずはこれを冒頭に入れちゃって下さい。
以下からのpythonスクリプトはこれがある前提で書いていきます。

プログラムからのちょっとした操作

1. iTunesが起動しているかどうかチェック

getiTunesInfo.py
it.isrunning()

実行結果

これの戻り値としては以下になります。

  • 起動していたら
    • True
  • 起動していなかったら
    • False

2. iTunesを起動する

getiTunesInfo.py
it.run()

実行結果

iTunesが起動します
※但し, 特に前面に出て来たり, アプリの画面に勝手に移動したりはしない

iTunesを(起動して)最前面にする。

getiTunesInfo.py
it.activate()

実行結果

iTunesが起動していなかったら起動して, 最前面になります(iTunesの画面にスライドする)

実行結果

これでiTunesが起動して尚且つ, 最前面にiTunesが開きます。

3. 曲を再生する

getiTunesInfo.py
it.play()

実行結果

曲が再生されます(音量注意)

4. 曲を止める

4-1. 停止

getiTunesInfo.py
it.stop()

実行結果

曲が止まります(再生しても途中からは流れない)

4-2. 一時停止

getiTunesInfo.py
it.pause()

実行結果

曲が一時停止します(もう一度再生をすると止めたところから再生される)

4-3. 再生・一時停止メソッド

getiTunesInfo.py
it.playpause()

実行結果

  • 再生していたら一時停止する
  • 一時停止していたら再生される(音量注意)
  • 停止状態でも再生される(音量注意)

再生中の曲情報を取得する

テンプレート

曲詳細の取得に関しては以下がテンプレートでプロパティ名を適宜帰る形になる

曲詳細テンプレート
it.current_track.{プロパティ名}.get()

例: 以下の曲を例として, 画像の曲詳細の情報を取得する
image.png

取得したい情報 プロパティ名 備考
name -
アーティスト artist -
アルバム album -
アルバムアーティスト album_artist -
作曲者 composer -
すべての表示に作曲者を表示 **すみませんこれだけわからなかったです(泣)**🙏💦 -
グループ grouping -
ジャンル genre -
year 多分発売された年?
もしかしたらアップルミュージックとかで配信になった年かも?ですが特に深掘りしてないです
トラック track_number そのアルバムで何曲目か
ディスク番号 disk_number 複数枚組のアルバムとかの場合ディスクによって変わってくるのだと思う
コンピレーション compilation コンピレーションアルバムかどうか(True/False)
評価 rating 自身で入力したもの
☆=20
❤️ loved または disliked 自身で入力したもの
lovedの場合、チェックなし=False, 好き=True, 好きじゃない=False
dislikedの場合, チェックなし=False, 好き=False, 好きじゃない=True
bpm bpm -
再生回数 played_count 自身が再生した回数
コメント comment 自身で入力したもの

実行結果

これらを標準出力する

getiTunesInfo.py
print(it.current_track.name.get())
print(it.current_track.artist.get())
print(it.current_track.album.get())
print(it.current_track.album_artist.get())
print(it.current_track.composer.get())
print(it.current_track.grouping.get())
print(it.current_track.genre.get())
print(it.current_track.year.get())
print(it.current_track.track_number.get())
print(it.current_track.discc_number.get())
print(it.current_track.compilation.get())
print(it.current_track.rating.get())
print(it.current_track.loved.get())
print(it.current_track.disliked.get())
print(it.current_track.bpm.get())
print(it.current_track.played_count.get())
print(it.current_track.comment.get())

実行結果

実行結果
# 曲
Tokyo Invader
# アーティスト
踊Foot Works
# アルバム
ODD FOOT WORKS
# アルバムアーティスト
踊Foot Works
# 作曲者
Odd Foot Works
# グループ
踊Foot Works
# ジャンル
ヒップホップ/ラップ
# 年
2017
# トラック
2
# ディスク番号
1
# コンピレーション
False
# 評価
60
# ❤️
## loved
False
## disliked
False
# bpm
1
# 再生回数
170
# コメント
お気に入り

アートワーク取得

参考にしたのはAppleScriptのiTunesのことが書かれた記事です
これが一番苦労した...

先ほどの記事の対象の部分が↓こちら

Get_the_artwork_for_the_current_track
tell artwork 1 of current track
   -- get the bytes of the artwork
   set srcBytes to raw data
   -- set the file extension based on the type
   if format is <> then
      set ext to "png"
   else
      set ext to "jpg"
   end if
end tell

-- get the destination filename as cover.ext in a temporary folder
set fileName to (path to temporary items folder from user domain as string) & "cover." & ext)
-- start the output file
set outFile to open for access file fileName with write permission
-- truncate it
set eof outFile to 0
-- write the bytes of the image to the file
write srcBytes to outFile
close access outFile

端的に解説すると

  1. 画像のバイナリーデータを取得
  2. 落としてきてそれをファイルに出力する

っていうやり方

では早速pythonに直していきます。

バイナリー取得と拡張子判別

バイナリー取得と拡張子判別(AppleScript)
tell artwork 1 of current track
   -- get the bytes of the artwork
   set srcBytes to raw data
   -- set the file extension based on the type
   if format is <> then
      set ext to "png"
   else
      set ext to "jpg"
   end if
end tell

↑がAppleScriptの記法
↓こちらがpythonの記法で尚且つappscriptを使用したもの

バイナリー取得と拡張子判別(python)
# バイナリーデータ取得
artworkRaw = it.current_track.artworks[1].raw_data.get()

# 拡張子判別
## 一旦jpgかpngしか見られなかったのでキメでやっています。
if it.current_track.artworks[1].format.get().name == 'JPEG_picture':
  ext = 'jpg'
else:
  ext = 'png'

※とりあえず拡張子の判別は何曲か再生してjpgはpngしか見られなかったので深追いせずにキメでやっちゃってます。すんません🙏

ファイルに書き出し

ファイルに書き出し(AppleScript)
-- get the destination filename as cover.ext in a temporary folder
set fileName to (path to temporary items folder from user domain as string) & "cover." & ext)
-- start the output file
set outFile to open for access file fileName with write permission
-- truncate it
set eof outFile to 0
-- write the bytes of the image to the file
write srcBytes to outFile
close access outFile

↑がAppleScriptの記法
↓こちらがpythonの記法で尚且つappscriptを使用したもの

ファイルに書き出し(python)
# ファイル名は '曲名+拡張子' にする
## ディレクトリは相対パスでわかりやすくデスクトップに出力されるようにしてます
fileName = f'../../{it.current_track.name.get()}.{ext}'

# バイナリ書き込みモードでファイルを開く
with open(fileName, mode='wb') as f:
  # もし既にあった場合の為に一度中身をクリアする
  pass
  # バイナリーデータを書き込む
  f.write(artworkRaw)

以上!


繋げるとこうなります。

getiTunesInfo.py
artworkRaw = it.current_track.artworks[1].raw_data.get()
if it.current_track.artworks[1].format.get().name == 'JPEG_picture':
  ext = 'jpg'
else:
  ext = 'png'

fileName = f'../../{it.current_track.name.get()}.{ext}'
with open(fileName, mode='wb') as f:
  pass
  f.write(artworkRaw)

実行結果

↓こんな感じでデスクトップにちゃんと画像ファイルが出力されました🎉
image.png

実際のデータもこんな感じ↓🎉

image.png

最後に

今回はお遊び程度にやったのでとりあえず実行できる環境を作ってやりました。
何かに利用してみたい..!!!
これを使ってまたなんか記事書こうと思います!!!

参考資料

とにかくappscriptとかAppleScriptの記事が少ないし,
日本語なんてほぼないし,
ましてやiTunesに特化した記事なんてほとんどなかったのでめちゃくちゃ大変でしたので参考にした記事には本当に感謝です🙏

おわり

7
10
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
7
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?