1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Python】 Youtube の音声データをダウンロードする

Last updated at Posted at 2025-09-01

はじめに

環境音の認識モデルを作ろうとしたときに,Youtube の音源データを使うと良いのではと思いました.
少し難しかったので記事にして,必要な人の役に立てばと思い記事を書いておきます.

環境について

私の環境は以下の通りです.

  • Linux
  • Pyhton 3.13.0

(注意)Python のバージョンは 3.9 以上にしておきましょう.

ライブラリ

ytl-dlp というライブラリを使用します.

手順

ターミナルを開き,以下を実行します.
すると,「.wav」ファイルがダウンロードできると思います.

curl -L https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp -o ~/.local/bin/yt-dlp
chmod a+rx ~/.local/bin/yt-dlp  # Make executable

サンプルプログラム

以下に Python のサンプルプログラムを載せておきます.
Youtube の URL 部分は自身で変更してください.

import yt_dlp

def download_audio_as_wav(video_url):
    options = {
        'format': 'bestaudio/best',
        'outtmpl': 'downloaded_audios/%(title)s.%(ext)s',  # 出力ファイル名のテンプレート
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'wav',   # wav形式に変換
        }],
    }
    with yt_dlp.YoutubeDL(options) as ydl:
        ydl.download([video_url])

# 動画のURLを指定してダウンロード
download_audio_as_mp3('https://www.youtube.com/watch?v=???????')

最後に

ご自身の環境でプログラムを実行することはできましたか?
上記のプログラムの options 部分を変更すれば,時間の指定や名称やファイル形式の変更等をすることができます.
くわしくは,yt-dlp のサイトを見てみて下さい.

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?