LoginSignup
1
2

More than 1 year has passed since last update.

pythonを使ってyoutubeをダウンロードする

Last updated at Posted at 2022-11-05

検証動画

ライブラリのインストール

pip install yt-dlp

コーディング

from yt_dlp import YoutubeDL

ydl_opts = {'format': 'best'}
with YoutubeDL(ydl_opts) as ydl:
    ydl.download(['https://www.youtube.com/watch?v=47Jl-_0wQHM&t=1s'])

実行結果

[youtube] 47Jl-_0wQHM: Downloading webpage
[youtube] 47Jl-_0wQHM: Downloading android player API JSON
[info] 47Jl-_0wQHM: Downloading 1 format(s): 22
[download] Destination: Pythonを用いて自動で新垣結衣の画像を収集してみた [47Jl-_0wQHM].mp4
[download] 100% of   11.41MiB in 00:00:01 at 6.54MiB/s
0

mp4でダウンロードできましたね。
音声だけ抽出する場合は、フォーマットで「bestaudio」を選択することでできます。

実行結果

[youtube] 47Jl-_0wQHM: Downloading webpage
[youtube] 47Jl-_0wQHM: Downloading android player API JSON
[info] 47Jl-_0wQHM: Downloading 1 format(s): 258
[download] Destination: Pythonを用いて自動で新垣結衣の画像を収集してみた [47Jl-_0wQHM].m4a
[download] 100% of    9.69MiB in 00:00:04 at 2.13MiB/s
WARNING: 47Jl-_0wQHM: writing DASH m4a. Only some players support this container. Install ffmpeg to fix this automatically
0

m4a形式の音声ファイルをダウンロードできていることが確認できましたね。

Github

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