Pythonを使ってYoutube動画の出演者を当てる(東海オンエアで検証)
Youtube Data APIを使えば、Youtube動画の様々な情報を得られる。タイトルや概要欄などが分かる。Youtube Data APIについては以下の記事にまとめた。
chindafalldesu.hatenablog.com
上記のほかにコメント欄のコメントを取得することも可能である。
動画をそのまま解析することは難しいので動画の簡易的な解析にコメント欄を用いることができる。
コメントを使って、東海オンエアの動画に誰が出演しているか当ててみよう。
①コメント欄のコメントの取得
import requests import time videoId='JRizOqEl8EM' apikey='(Youtube Data API のキー)' url='https://www.googleapis.com/youtube/v3/commentThreads?part=snippet,id&videoId='+videoId+'&textFormat=plainText&maxResults=50&key='+apikey response = requests.get(url) num=0 for i in range(50): print(str(num)+'. '+response.json()['items'][i]['snippet']['topLevelComment']['snippet']['textDisplay']) num+=1
(実行結果)
> python .\channel4.py 0. 5:06 しばゆーw 1. 8倍速何言ってるかわからん笑 2. 8倍速なんかの曲みたい 3. 犬夜叉の手懐かしい 4. ボクシングのレベル高すぎな 5. なんかどっかで見たんだけど思い出せない、、 6. 2:102:102:102:10 見つからなくて自分で書きました。 : : 45. 7:21 トモダチコレクション 46. 7:43 から2倍速でみると面白い 47. 8倍速どう森みたい 48. てつやでぶった? 49. 久々に見たら、1人どうしようもない雑魚になってて草
②東海オンエアの動画に誰が出演しているか当ててみる。
www.youtube.com
この動画の出演者を当ててみよう。出演者の名前の登場回数から当てる。
import requests import time apikey='(Youtube Data APIのキー)' videoId=input("videoId: ") url='https://www.googleapis.com/youtube/v3/commentThreads?part=snippet,id&videoId='+videoId+'&textFormat=plainText&maxResults=50&key='+apikey yume=0 shiba=0 toshi=0 tetsu=0 mushi=0 ryo=0 response = requests.get(url) for i in range(50): comment=response.json()['items'][i]['snippet']['topLevelComment']['snippet']['textDisplay'] if 'ゆめまる' in comment: yume+=1 elif 'しばゆー' in comment: shiba+=1 elif 'てつや' in comment: tetsu+=1 elif '虫眼鏡' in comment or '虫' in comment: mushi+=1 elif 'としみつ' in comment: toshi+=1 elif 'りょう' in comment: ryo+=1 for j in range(3): next=response.json()["nextPageToken"] nexturl=url+'&pageToken='+next response = requests.get(nexturl) for i in range(50): comment=response.json()['items'][i]['snippet']['topLevelComment']['snippet']['textDisplay'] if 'ゆめまる' in comment: yume+=1 elif 'しばゆー' in comment: shiba+=1 elif 'てつや' in comment: tetsu+=1 elif '虫眼鏡' in comment or '虫' in comment: mushi+=1 elif 'としみつ' in comment: toshi+=1 elif 'りょう' in comment: ryo+=1 print("しばゆー: "+str(shiba)) print("ゆめまる: "+str(yume)) print("としみつ: "+str(toshi)) print("てつや: "+str(tetsu)) print("虫眼鏡: "+str(mushi)) print("りょう: "+str(ryo)+"\n") print("出演者: ") if shiba>3: print("しばゆー") if yume>3: print("ゆめまる") if toshi>3: print("としみつ") if tetsu>3: print("てつや") if mushi>3: print("虫眼鏡") if ryo>3: print("りょう")
(実行結果)
> python .\channel5.py videoId: SEKB8bQNwwU しばゆー: 14 ゆめまる: 14 としみつ: 9 てつや: 13 虫眼鏡: 0 りょう: 1 出演者: しばゆー ゆめまる としみつ てつや
この動画ではうまくいったようである。ほかの動画でも試してみる。
例1)
www.youtube.com
> python .\channel5.py videoId: t6VuxAtz5IU しばゆー: 18 ゆめまる: 0 としみつ: 2 てつや: 19 虫眼鏡: 19 りょう: 9 出演者: しばゆー てつや 虫眼鏡 りょう
例2)
www.youtube.com
> python .\channel5.py videoId: _WE8CwuDDEA しばゆー: 14 ゆめまる: 0 としみつ: 12 てつや: 8 虫眼鏡: 0 りょう: 0 出演者: しばゆー としみつ てつや
仕組みはかなりシンプルだが、うまく動画の解析を行えているようである。
有名人辞書、名前辞書などを作れば、Youtubeのすべての動画の出演者を当てることも可能かもしれない。
(最後まで読んでいただきありがとうございます。間違い等あればコメントよろしくお願いいたします。)