Wizard Notes

Python, JavaScript を使った音楽信号分析の技術録、作曲活動に関する雑記

YouTubeの音楽をPythonで解析:youtube-dlによる動画メタデータ収集

はじめに

投稿日時や再生数といった動画メタデータを収集するスクリプトを作成しました。

youtube-dlによる動画メタデータ収集スクリプト

以下のページを参考に実装しました。 複数のメタデータを一括で取得できたほうが便利だと考えて、コンテンツIDをリストで渡すように実装しています。 www.bogotobogo.com

from __future__ import unicode_literals
import youtube_dl
import os

def print_metadata(meta):
    print('upload date : %s' %(meta['upload_date']) )
    print('uploader    : %s' %(meta['uploader']) )
    print('views       : %d' %(meta['view_count']) )
    print('likes       : %d' %(meta['like_count']) )
    print('dislikes    : %d' %(meta['dislike_count']) )
    print('id          : %s' %(meta['id']) )
    print('format      : %s' %(meta['format']) )
    print('duration    : %s' %(meta['duration']) )
    print('title       : %s' %(meta['title']) )
    print('description : %s' %(meta['description']) )

def download_metadata(
        content_id_list=[],
        pdir='./'
        ):
    ydl_opts = {}
    youtube_v_url = 'http://www.youtube.com/watch?v='
    content_url_list = [youtube_v_url + cid for cid in content_id_list]
    meta_list = []
    # 指定したcontent IDの動画より、メタデータを保存
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        for youtube_url in content_url_list:
            meta = ydl.extract_info(
                    youtube_url, 
                    download=False
                    )
            print_metadata(meta)
            meta_list.append(meta)
    return meta_list

if __name__ == '__main__':
    cid_list = ['cGlFxkWgI84']
    meta_list = mayt.download_metadata(cid_list)

実行結果

Official Music Video] Perfume 「Future Pop」のメタデータを取得してみます。 (コンテンツID:cGlFxkWgI84)

YouTube: Perfume -"Future Pop"

>python get_metadata_from_youtube.py
[youtube] cGlFxkWgI84: Downloading webpage
[youtube] cGlFxkWgI84: Downloading video info webpage
[youtube] cGlFxkWgI84: Downloading js player vflVce_C4
upload date : 20180921
uploader    : Perfume
views       : 1189400
likes       : 28665
dislikes    : 295
id          : cGlFxkWgI84
format      : 137 - 1920x1080 (1080p)+251 - audio only (DASH audio)
duration    : 199
title       : [Official Music Video] Perfume 「Future Pop」
description : 【Perfume × docomo “Future Pop” Project】 https://www.nttdocomo.co.jp/special_contents/future_experiment/future-pop.html

「Future Pop」収録 Perfume New Album 「Future Pop」
‘Future Pop’ from Perfume new album “Future Pop” in stores now!
絶賛デジタル配信中! Listen here http://smarturl.it/FuturePop

▼詳細はこちら More info:
【Perfume Official Site】 http://www.perfume-web.jp/
【A!SMART】 https://www.asmart.jp/perfume/
【UNIVERSAL MUSIC JAPAN】 https://sp.universal-music.co.jp/perfume/fp/

作詞作曲 / 中田ヤスタカ Written by Yasutaka Nakata

楽曲の分析に利用できそうなメタデータ一覧

youtube-dl.extract_info()で得られるdict型のメタデータは多くのKeyを持っています。 そこで、いくつか音楽の分析に利用できそうなKeyをピックアップしてみました。

参考:GitHub - ytdl-org/youtube-dl: Command-line program to download videos from YouTube.com and other video sites

(2018/11/4) view_countなどのパラメタがNoneになっていました。 バグフィックスや最新版の動向・リリースをチェックしてください。

github.com

コーデック関連

  • abr(numeric): オーディオの平均ビットレート
  • vbr(numeric): ビデオの平均ビットレート
  • acodec(string): オーディオコーデック名
  • vcodec(str): ビデオコーデック名
  • age_limit(numeric): ビデオ視聴の年齢制限
    • 0だと無制限?
  • fps(numeric): フレーム毎秒
  • ext(str): 拡張子

コンテンツ関連

  • title(str): 動画タイトル
  • description(str): 動画説明文
  • categories(list of str): 動画の登録カテゴリ
  • tags(list of str): 動画の投稿者タグ一覧
  • thumbnail(str): サムネイルのurl
  • thumbnails(list of dict): サムネイルのurl(複数)
    • 'id'(str)
    • 'url'(str)
  • channel_id(str): 動画のチャンネルID
  • channel_url(str): 動画のチャンネルURL

  • height(numeric): 動画のheight値

  • width(numeric): 動画のwidth値
  • duration(numeric): 時間長 (sec)
  • uploader(str): アップロード者
  • uploader_id(str): アップロード者のニックネーム
  • upload_date(str): 動画アップロード日
    • 2018年6月10日アップロードなら、'20180610'

動画の評価

  • average_rating: 平均評価
    • likeの数、dislikeの数、およびその他の評価値によって5段階で決定?
    • likeがついており、dislikeがない時は5であることを確認
  • like_count(numeric): likeの数
  • dislike_count(numeric): dislikeの数
  • view_count(numeric): 再生数

まとめ

参考ページをもとに、youtube-dlを用いてPythonYouTube動画のメタデータを取得しました。 今後、メタデータとしてどんな情報が取得できるのかを簡単にまとめたいと思います。