I'm trying to send the stream of a usb webcam with pytgcalls
on Telegram. In the documentation there is nothing about that, but there are several example of streaming youtube video or local mp4 files. I have found this example of streaming a youtube video and I think that streaming a usb webcam is quite similar.
First of all, importing modules:
import asyncio
from pyrogram import Client
from pytgcalls import idle
from pytgcalls import PyTgCalls
from pytgcalls import StreamType
from pytgcalls.types.input_stream import AudioImagePiped, AudioVideoPiped
from pytgcalls.types.input_stream.quality import HighQualityVideo, HighQualityAudio, LowQualityAudio, LowQualityVideo
Now, start pyrogram and pytgcalls clients:
app = Client('py-tgcalls', api_id=API_ID, api_hash=API_HASH)
call_py = PyTgCalls(app)
call_py.start()
Declare the function that will convert the youtube link to a stream:
def get_youtube_stream():
async def run_async():
proc = await asyncio.create_subprocess_exec(
'youtube-dl',
'-g',
'-f',
'best[height<=?720][width<=?1280]',
# Some random YT link
'https://www.youtube.com/watch?v=AAAAAAA',
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
stdout, stderr = await proc.communicate()
return stdout.decode().split('\n')[0]
return asyncio.get_event_loop().run_until_complete(run_async())
Declare the function for sending the stream via telegram, in a group:
def youtube():
remote = get_youtube_stream()
call_py.join_group_call(
# Group ID
-10011111111,
AudioVideoPiped(
remote,
HighQualityAudio(),
HighQualityVideo(),
),
stream_type=StreamType().pulse_stream,
)
Now the function youtube()
can be executed:
youtube()
Using idle()
for blocking the script:
idle()
This is the script, can somebody help me in streaming the live of a usb webcam?
Recently by pytgcalls (0.9.0) was added the possibility to stream from an upd stream, you can use directly ffmpeg to capture the media device and make local UDP streaming