from pyrogram import Client
from pytube import YouTube
import os
import asyncio
app = Client("qaserd_ll", api_id=..., api_hash='...', bot_token='...')
@app.on_message()
async def send_video(client, message):
URL = message.text
yt = YouTube(URL)
ys = yt.streams.get_highest_resolution()
video_path = 'video.mp4'
try:
ys.download(filename='video.mp4') # Download the video using the specified filename
print("Video downloaded successfully")
except Exception as e:
print(f"An error occurred while downloading the video: {e}")
return
attempts = 0
while not os.path.exists('video.mp4') and attempts < 10: # Wait for the video to be downloaded
await asyncio.sleep(1)
attempts += 1
if attempts == 10:
print("Video download timed out")
return
duration = yt.length
print(duration)
try:
await client.send_video(message.chat.id, video_path, duration=duration)
print("Video sent successfully")
except Exception as e:
print(f"An error occurred while sending the video: {e}")
app.run()
视频下载完毕,时间也显示正确,但最后几行代码没有执行。 bot token、api_id、api_hash 写入正确
您的文件大小是多少?我花了大约 20 秒才发送从上面的链接下载的 1.5 MB 视频。 这是我的 MRE,它有效 -
from pyrogram import Client
import os
import asyncio
app = Client("qaserd_ll", api_id=123, api_hash='123', bot_token='123')
@app.on_message()
async def send_video(client, message):
try:
x= await client.send_video(message.chat.id, "file_example_MP4_480_1_5MG.mp4", duration=39)
print("Video sent successfully")
except Exception as e:
print(f"An error occurred while sending the video: {e}")
app.run()