1
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?

英語のリスニング音源に歌詞を埋め込む

Last updated at Posted at 2025-09-21

背景

英会話の教材(書籍)を購入すると、CDが付属していたり、mp3形式のデータがダウンロードできることってよくありますよね。
私は電車移動中などにその音源を聞いて、英語の勉強をしています。
ただ、聞き取れない単語があったときに、書籍を開くのがとてもめんどくさいです。そもそも満員電車では書籍を物理的に開けない可能性もあります。
電子書籍にすれば解決するのですが、自宅での勉強では圧倒的に紙の書籍が便利なので、電子書籍は購入したくないです。
そこで、mp3に歌詞情報として英文を埋め込めば、スマホだけで勉強を完結できるのでは?と考えました。

Whisperを使ってmp3の音声を認識する

WhisperはOpenAIが公開している音声認識の学習モデルです。
無料で使えて、性能も高そうなのでとりあえずこれを使います。
https://openai.com/ja-JP/index/whisper/

Pythonのライブラリとして公開されており、簡単に使えます。

pip install whisper

mutagenを使ってmp3に歌詞を書き込む

mutagenはPythonでmp3などのメタデータを扱えるライブラリです。
https://mutagen.readthedocs.io/en/latest/

pip install mutagen

ソースコード

import whisper
from mutagen.id3 import ID3, USLT, ID3NoHeaderError
import os
import glob


def transcribe_and_embed_lyrics(mp3_file_path):
    """
    MP3ファイルの音声をWhisperで文字起こしし、そのテキストを歌詞として埋め込みます。
    """
    try:
        print(f"'{os.path.basename(mp3_file_path)}'の音声を処理中...")

        # Whisperモデルをロード
        # "base"は比較的小さなモデルで、高速に動作します。
        # より高精度なモデルとして、"small"や"medium"も選択可能です。
        model = whisper.load_model("base")

        # 音声ファイルを文字起こし
        result = model.transcribe(mp3_file_path, language="en")
        text = result["text"]

        print("文字起こしが完了しました。")

        # 歌詞としてMP3ファイルに埋め込む
        try:
            tags = ID3(mp3_file_path)
        except ID3NoHeaderError:
            print("ID3ヘッダーを追加します。")
            tags = ID3()

        tags.delall('USLT')
        tags.add(USLT(encoding=3, lang=u'eng', desc='', text=text))

        tags.save(mp3_file_path)

        print(f"'{os.path.basename(mp3_file_path)}'に歌詞が埋め込まれました。")
        print("文字起こしされたテキスト:")
        print(text)
        print("-" * 20)

    except Exception as e:
        print(f"エラーが発生しました: {e}")


def main():
    """
    指定されたフォルダ内のすべてのMP3ファイルを処理します。
    """
    folder_path = input("mp3が保存されているディレクトリを入力してください: ")

    # globモジュールを使ってファイルリストを取得
    mp3_files = glob.glob(os.path.join(folder_path, "*.mp3"))

    if not mp3_files:
        print(f"指定されたフォルダ '{folder_path}' にMP3ファイルが見つかりません。")
        return

    for file_path in mp3_files:
        transcribe_and_embed_lyrics(file_path)


if __name__ == "__main__":
    main()

実行結果

例として、私の手元にある「速読速聴・英単語 Basic 2400 ver.4」の音声に対して、上記のプログラムを実施してみます。
上記のソースコードを「transcribe.py」という名前で保存して、実行します。
ディレクトリ名の入力を指示されるので、mp3が保存されているディレクトリを指定します。
指定したディレクトリに存在する全てのmp3ファイルに対して処理が実行されます。

python transcribe.py
mp3が保存されているディレクトリを入力してください: C:\Users\Username\Downloads\basic4_all\honbun
'Ba_001.mp3'の音声を処理中...
文字起こしが完了しました。
'Ba_001.mp3'に歌詞が埋め込まれました。
文字起こしされたテキスト:
 One. Good morning. It's a beautiful day. I think so too. Are you our new neighbor? Yes. I just moved in this week. Nice to meet you. I'm Mike. Nice to meet you too. I'm Sue.
--------------------
'Ba_002.mp3'の音声を処理中...
文字起こしが完了しました。
'Ba_002.mp3'に歌詞が埋め込まれました。
文字起こしされたテキスト:
 2. Hi Sue. How's it going? Great. I like your bicycle. It's so cool. Thanks. Your bike looks nice too. Yeah. I like it.
--------------------
'Ba_003.mp3'の音声を処理中...
文字起こしが完了しました。
'Ba_003.mp3'に歌詞が埋め込まれました。
文字起こしされたテキスト:
 3. I'm leaving for the airport now. And Carol, I really enjoyed our time together. I'll miss you. I'll miss you too, Ben. Please come and visit us again soon. Thanks, Ben. Take care.
--------------------

かなり高い精度で認識できています。
ただ、Ba_003.mp3 の「And Carol」は本当は「Aunt Carol」が正しいです。。。
こんな感じで多少の間違いは生じます。

スマホに入れて見てみる

歌詞情報を埋め込んだmp3ファイルをスマホに保存し、音楽再生アプリから確認してみます。
私の環境は Google Pixel7a (Androd14) で Musicolet と Poweramp の2つのアプリで試してみたところ、歌詞情報が表示されました。
Screenshot_20250921-142913.png

1
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
1
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?