Last active
April 10, 2025 04:08
-
-
Save kylemsguy/dbdc3af9a88ff0079d40d8d39059b2b5 to your computer and use it in GitHub Desktop.
A quick script to render live chat json's downloaded from YT-DLP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
def render_chat(chats): | |
chat_rendered = [] | |
for chat in chats: | |
item = chat['replayChatItemAction']['actions'][0] | |
if 'addChatItemAction' not in item: | |
continue | |
item = item['addChatItemAction']['item'] | |
if 'liveChatTextMessageRenderer' in item: | |
# handle chat message | |
chat_renderer = item['liveChatTextMessageRenderer'] | |
author = chat_renderer['authorName']['simpleText'] | |
message_runs = chat_renderer['message']['runs'] | |
timestamp_text = chat_renderer['timestampText']['simpleText'] | |
parsed_runs = [] | |
for run in message_runs: | |
if 'text' in run: | |
parsed_runs.append(run['text']) | |
elif 'emoji' in run: | |
parsed_runs.append(run['emoji']['shortcuts'][0]) | |
message = ' '.join(parsed_runs) | |
chat_rendered.append(f"{timestamp_text} {author}: {message}") | |
elif 'liveChatViewerEngagementMessageRenderer' in item: | |
# handle banner or whatever | |
# we probably don't care either | |
pass | |
else: | |
# other types, we don't care | |
pass | |
return chat_rendered | |
if __name__ == "__main__": | |
filename = "somelivechat.live_chat.json" | |
chats = [] | |
with open(filename, encoding="utf-8") as infile: | |
for line in infile.readlines(): | |
chats.append(json.loads(line)) | |
chat_rendered = render_chat(chats) | |
with open("outfile.txt", 'w', encoding='utf-8') as outfile: | |
for chat in chat_rendered: | |
print(chat, file=outfile) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment