将 Youtube 视频发送到电报

send Youtube videos to telegram

我写了这个程序:

import scrapetube
import hashlib
import telegram
import requests
import json
from pytube import YouTube

from time import sleep
import os

bot_token = 'my token'
bot_chatID = '@PinkPanthersShow'

bot = telegram.Bot(token=f'{bot_token}')
c_videos = scrapetube.get_channel("UC1fIyfhQtm1fSljyKBf2uKA")
videos = list()
for video in c_videos:
   videos.append(f"https://www.youtube.com/watch?v={str(video['videoId'])}")
videos.reverse()
def upload_video(title, desc):
    bot.send_video(chat_id=bot_chatID, video=open(fr'{title}.mp4', 'rb'),caption=f"{desc}", supports_streaming=True)
for i in list(range(len(videos))):
    yt = YouTube(videos[i])
    print(f"{yt.title} : {videos[i]}")
    ys = yt.streams.get_highest_resolution()
    ys.download()
    upload_video(str(yt.title), str(yt.description))
#print (list(videos)[0])
#links.reverse()
#print(links)
#print(title)
#print(videos)
input()

当我的程序进入视频 The Pink Panther Show 第 4 集 - 为 Pink 拨“P”:https://www.youtube.com/watch?v=tOaakSDwuZE 时,出现此错误:

Traceback (most recent call last):
  File "g:\yta\bot.py", line 43, in <module>
    upload_video(str(yt.title), str(yt.description))
  File "g:\yta\bot.py", line 37, in upload_video
    bot.send_video(chat_id=bot_chatID, video=open(fr'{title}.mp4', 'rb'),caption=f"{desc}", supports_streaming=True)
OSError: [Errno 22] Invalid argument: 'The Pink Panther Show Episode 4 - Dial "P" for Pink.mp4'

我该怎么做才能避免这个错误?

如果你想将 Youtube 频道的所有视频发送到电报,你可以使用 ytt:

https://nimafanniasl.github.io/py_youtube_to_telegram/

如果你想使用一个应用程序,你可以使用ytt应用程序,但是如果你想在python中使用它,你可以使用ytt模块,例如:

pip install ytt

python 应用程序:

from ytt import ytt

ytt.start_all(YouTube_channel_id,telegram_bot_token,bot_chatID,send_channel_or_group_id=None)

# :)

更多信息:https://github.com/nimafanniasl/py_youtube_to_telegram/tree/main/module