Вдруг, кому-то еще пригодится
import yt_dlp, json, os
VIDEO_ID = 'dQw4w9WgXcQ'
VIDEO_URL = f'https://www.youtube.com/watch?v={VIDEO_ID}'
def download_chat(video_url):
ydl_opts = {
'skip_download': True,
'writeinfojson': True,
'writethumbnail': True,
'writesubtitles': True,
'subtitlesformat': 'json',
'outtmpl': f'{VIDEO_ID}.json',
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([video_url])
return f"{VIDEO_ID}.json.live_chat.json"
def parse_chat_json(chat_filename):
if not os.path.exists(chat_filename):
return []
messages = []
with open(chat_filename, 'r', encoding='utf-8') as f:
for line in f:
data = json.loads(line)
try:
print(data['replayChatItemAction']['actions'][0]['addChatItemAction']['item']['liveChatTextMessageRenderer'])
author_name = data['replayChatItemAction']['actions'][0]['addChatItemAction']['item']['liveChatTextMessageRenderer']['authorName']['simpleText']
message_parts = data['replayChatItemAction']['actions'][0]['addChatItemAction']['item']['liveChatTextMessageRenderer']['message']['runs']
message = ''.join(part.get('text', '') for part in message_parts)
timestamp = data['replayChatItemAction']['actions'][0]['addChatItemAction']['item']['liveChatTextMessageRenderer']['timestampText']['simpleText']
messages.append(f"{author_name}: {message} ({timestamp})")
except KeyError:
continue
return messages
def save_chat_to_txt(chat_filename, video_id):
chat_messages = parse_chat_json(chat_filename)
output_filename = f'output_{video_id}.txt'
with open(output_filename, 'w', encoding='utf-8') as out_file:
for message in chat_messages:
out_file.write(message + '\n')
def clean_up_files(video_id):
for ext in ['json.webp', 'json.live_chat.json', 'json.info.json']:
filename = f"{video_id}.{ext}"
if os.path.exists(filename):
os.remove(filename)
if __name__ == '__main__':
chat_file = download_chat(VIDEO_URL)
if chat_file:
save_chat_to_txt(chat_file, VIDEO_ID)
clean_up_files(VIDEO_ID)