telegram-apiwtelegramclient

How to Upload Streamable Video?


I want to ask. I upload video with UploadFileAsync and Post with SendMediaAsync but when I check chat there is show file without thumbnail and can't streaming (like common doc file). Can you show me how to use it for watch stream?

Here is my code:

WTelegram.Client.ProgressCallback progress = new WTelegram.Client.ProgressCallback((p,r) => {
     Console.Write(p*100/r+"%\r");
 });
SetLog("Uploading File...");
var inputFile = await client.UploadFileAsync("video\\myfile.mp4", progress);
var target = chats.chats[channelId];
SetLog($"Sending a message in chat {target.ID}: {target.Title}");
await client.SendMediaAsync(target, "Test", inputFile);

This is the above code result:

Result Above Code

What I want to achieve:

Image


Solution

  • When you send a small video (that can immediately autoplay without pressing the play button), SendMediaAsync is enough.

    However, for bigger mp4 videos, Telegram won't attempt to start downloading/analyze/play it until the user press the play or download button. Therefore, if you want the mp4 to appear as streamable, you need to call something like:

    await client.SendMessageAsync(targetPeer, "caption", new InputMediaUploadedDocument
    {
      file = mediaInputFile, mime_type = "video/mp4",
      attributes = new[] {
        new DocumentAttributeVideo { duration = 183, w = 1280, h = 720,
          flags = DocumentAttributeVideo.Flags.supports_streaming }
      }
    });
    

    That means you have to analyze the video yourself before sending, to be able to pass the correct duration/width/height of the video in above code. I tried passing 0 to these, it seemed to work too but the preview will appear square and duration is shown as "0:00" until the user press play.

    Also the preview image (thumbnail) might be blank/black unless you add the following to InputMediaUploadedDocument: thumb = photoInputFile, flags = InputMediaUploadedDocument.Flags.has_thumb (which means you have to have uploaded a thumbnail photo before)