1

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?

Shivang Kakkar
  • 421
  • 3
  • 15

1 Answers1

2

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

Laky 64
  • 36
  • 1
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 28 '22 at 04:31
  • thanks! i will try to figure it out how to do this! :) do you have some links that can help me? – lucabinotti May 28 '22 at 19:10