LoginSignup
2
3

More than 5 years have passed since last update.

ニコニコ動画のコメントを動画と連動してターミナル上で再生

Last updated at Posted at 2017-09-08

動機

アマゾンプライムビデオで見られる「干物妹!うまるちゃん」をニコニコ動画のコメントと一緒に見たかった。

ニコニコ動画のコメント自体は、

Pythonでニコニコ動画のコメントを取得する方法

で取得して、result.xml というファイルに保存しておく。

下のプログラムの肝は、動画と連動してコメントを表示できることである。もちろん臨場感では本家ニコ動にかなわないが、それなりに楽しめるようになった。

コード

# -*- coding: utf-8 -*-
# For Python 2
import xml.etree.ElementTree as ET
from operator import itemgetter
import time

def vpos_str(vpos):
    sec = int(vpos / 100)
    return "%02d:%02d:%02d" % (int(sec / 60), sec % 60, vpos % 100)

tree = ET.parse('result.xml')
root = tree.getroot()
chats = [(int(c.attrib["vpos"]), c.text) for c in root.findall("chat") if c.text]
chats = sorted(chats, key=itemgetter(0))
st = int(time.time() * 100)
i = 0
while i < len(chats):
    time.sleep(0.01)
    t = int(time.time() * 100) - st
    while i < len(chats) and chats[i][0] <= t:
        print(u"%s %s" % (vpos_str(chats[i][0]), chats[i][1]))
        i += 1

実行結果

00:01:18 見納め
00:01:55 買った。
00:06:31 !
00:10:91 課金した!
00:11:83 うまるーん
00:14:39 だれ
00:15:79 誰だお前!?
2
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
2
3