前回、
$ sqlite3 radiru_titles.sql3 SQLite version 3.8.0.2 2013-09-03 17:11:13 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite> select oid,* from titles where date like '2014-05-24%'; 1567|2014-05-24-17-00_奥の細道~名句でたどるみちのくの旅.mp3|2014- 05-24 17:00:00|001netr20|古典講読「奥の細道~名句でたどるみちのくの 旅~」(8)|和洋女子大学副学長…佐藤勝明 ... 1570|2014-05-24-21-00_私の日本語辞典「能をささえることば」_004.mp3| 2014-05-24 21:00:00|001netr20|私の日本語辞典「能をささえることば」 (4)|法政大学名誉教授、能楽研究家…西野春雄, 【アナウンサー】秋山和平 1571|2014-05-24-21-00_クラシックの迷宮‐私の試聴室~ヴァインベルクの 作品~.mp3|2014-05-24 21:00:00|001netfm0|クラシックの迷宮 -私の試聴 室~ヴァインベルクの作品~-| sqlite> select * from contents where id=1571; 1571|片山杜秀 - 私の試聴室~ヴァインベルクの作品~ - 1571|「バイオリンと弦楽のための小協奏曲 作品42から 第3楽章」 ヴァインベルク作曲 (4分39秒) (バイオリン)ギドン・クレーメル ....
録音したMP3ファイルにどういう内容が入っているかは、
何かいい方法はないかな、
ID3タグについて
実際のコード等を紹介するまえに、
ID3タグはMP3ファイルにタイトルやアーティスト名を埋めこむために広く利用されている機能なものの、
最初に定義されたID3タグはID3v1と呼ばれ、
一方、
そこで新たに開発されたのがID3v2と呼ばれる形式です。この形式では、
前述のように、
しかしながら、
PythonのeyeD3モジュール
前述のように、


これらタグ編集機能を使えば、
eyeD3はID3タグを操作するために開発されたPython用のモジュールで、
$ eyeD3 02.銀座カンカン娘.mp3 02.銀座カンカン娘.mp3 [ 3.92 MB ] ------------------------------------------------------------------------------- Time: 02:51 MPEG1, Layer III [ 192 kb/s @ 44100 Hz - Joint stereo ] ------------------------------------------------------------------------------- ID3 v2.3: title: 銀座カンカン娘 artist: 遊佐未森 album: スヰート檸檬 recording date: 2008 track: 2 genre: JPop (id 146) OTHER Image: [Size: 7453 bytes] [Type: image/jpeg] Description: -------------------------------------------------------------------------------
さて、
ID3タグ名 | 番組情報 |
---|---|
album | ファイル名 |
title | 番組名 |
artist | 出演者 |
date | 放送日 |
comment | 番組内容 |
これらの情報は番組の放送中ならばNOA情報からダウンロードできます。しかし、
そのあたりを考慮して、
62 filepath = sys.argv[1]
63 file = os.path.basename(filepath)
64 ufile = file.decode('euc-jp')
65
66 query = u"select oid,title,date,ch,act from titles where filename='{}';".format(ufile)
67 (oid, title, date, channel, act) = query_db(query)[0]
番組情報データベースに記録しているのはファイル名だけなので、
データベースを検索するquery_
23 def query_db(query):
24 dbdir = config['DB_dir']
25 dbname = dbdir + '/radiru_titles.sql3'
26 if os.access(dbname, os.R_OK) == False:
27 print("cannot find title DB({})".format(dbname))
28 sys.exit(1)
29
30 connection = sqlite3.connect(dbname)
31 cursor = connection.cursor()
32 c = cursor.execute(query)
33 res = c.fetchall()
34 if len(res) == 0:
35 print("DB doesn't have entry for id: {} ".format(query))
36 print("please check title DB:{}".format(dbname))
37 sys.exit(1)
38 else:
39 return(res)
前回紹介したように、
41 def query_contents(oid):
42 query = u"select * from contents where id={}".format(oid)
43 t = query_db(query)
44 return t
45
contentsテーブルから取り出した曲目リストは、
69 res = query_contents(oid)
70 contents = ''
71 for i in res:
72 (id, l) = i
73 contents = contents + l
74
ID3タグの"album"に記録するファイル名は、
75 album_parts = ufile.rstrip('.mp3').split('_')
76 album = album_parts[1]
以上で必要な情報が揃ったので、
77 tag = {'title':title, 'album':album, 'act':act, 'date':date, 'ch':channel, 'contents':contents}
78 set_id3tag(filepath, tag)
set_
46 def set_id3tag(file, tag):
47 mp3file = file
48
49 new_tag = eyed3.id3.Tag()
50 new_tag.file_info = eyed3.id3.FileInfo(mp3file)
51 new_tag.title = tag['title']
52 new_tag.album = tag['album']
53 new_tag.artist = tag['act']
54 new_tag.release_date = tag['date']
55 if len(tag['contents']) > 0:
56 new_tag.comments.set(tag['contents'])
57
58 new_tag.save()
なお、
今回作成したスクリプトの全体は本稿の末尾に添付しておきます。手元では、
1 #!/bin/sh
2 sleep 10
3 mkfifo /home/kojima/radiru_scripts/fifo/1619
4 file=/home/kojima/MP3/2014-05-31-20-30_漢詩をよむ「中国のこころのうた」_009.mp3
5 (mplayer -prefer-ipv4 -ipv4-only-proxy -slave -input file=/home/kojima/radiru_scripts/fifo/1619 -playlist http://mfile.akamai.com/129932/live/reflector:46056.asx -af format=s16le -ao pcm:file=/dev/stdout -vc null -really-quiet -quiet | lame -r --quiet -q 4 - $file 2> /dev/null) &
6 sleep 1m
7 radiru_noa.py r2 2014-05-31-20-30_漢詩をよむ「中国のこころのうた」_009.mp3
8 sleep 29m
9 echo 'quit' > /home/kojima/radiru_scripts/fifo/1619
10 radiru_id3.py /home/kojima/MP3/2014-05-31-20-30_漢詩をよむ「中国のこころのうた」_009.mp3
11 rm -f /home/kojima/radiru_scripts/fifo/1619 /home/kojima/radiru_scripts/1619
録音作業自体は5行目の mplayer と lame が行ない、
radiru_

本シリーズで紹介してきた各スクリプトや、
なお、
また、
先に切り抜いたFM情報誌をラベルにしたカセットテープの写真を紹介したように、
もっとも最近では、
かってのように、
1 #! /usr/bin/python
2 # -*- coding: utf-8 -*-;
3
4 import sqlite3, os, sys, eyed3
5 from eyed3 import id3
6
7 # global variables which show each directries
8 config_path = "~/.radiru_confs"
9 config = {'script_dir':'~/radiru_scripts', 'MP3_dir':'~/MP3','DB_dir':'~/MP3'}
10
11 def init():
12 global config
13 conf_path = os.path.expanduser(config_path)
14 if os.path.isfile(conf_path):
15 f = open(conf_path,'r')
16 lines = f.readlines()
17 for i in lines:
18 (it, dt) = i.rstrip().split(':')
19 config[it] = dt
20 for key in config:
21 config[key] = os.path.expanduser(config[key])
22
23 def query_db(query):
24 dbdir = config['DB_dir']
25 dbname = dbdir + '/radiru_titles.sql3'
26 if os.access(dbname, os.R_OK) == False:
27 print("cannot find title DB({})".format(dbname))
28 sys.exit(1)
29
30 connection = sqlite3.connect(dbname)
31 cursor = connection.cursor()
32 c = cursor.execute(query)
33 res = c.fetchall()
34 if len(res) == 0:
35 print("DB doesn't have entry for id: {} ".format(query))
36 print("please check title DB:{}".format(dbname))
37 sys.exit(1)
38 else:
39 return(res)
40
41 def query_contents(oid):
42 query = u"select * from contents where id={}".format(oid)
43 t = query_db(query)
44 return t
45
46 def set_id3tag(file, tag):
47 mp3file = file
48
49 new_tag = eyed3.id3.Tag()
50 new_tag.file_info = eyed3.id3.FileInfo(mp3file)
51 new_tag.title = tag['title']
52 new_tag.album = tag['album']
53 new_tag.artist = tag['act']
54 new_tag.release_date = tag['date']
55 if len(tag['contents']) > 0:
56 new_tag.comments.set(tag['contents'])
57
58 new_tag.save()
59
60 def main():
61 init()
62 filepath = sys.argv[1]
63 file = os.path.basename(filepath)
64 ufile = file.decode('euc-jp')
65
66 query = u"select oid,title,date,ch,act from titles where filename='{}';".format(ufile)
67 (oid, title, date, channel, act) = query_db(query)[0]
68
69 res = query_contents(oid)
70 contents = ''
71 for i in res:
72 (id, l) = i
73 contents = contents + l
74
75 album_parts = ufile.rstrip('.mp3').split('_')
76 album = album_parts[1]
77 tag = {'title':title, 'album':album, 'act':act, 'date':date, 'ch':channel, 'contents':contents}
78 set_id3tag(filepath, tag)
79
80 if __name__ == "__main__":
81 main()