gram-js / gramjs

NodeJS/Browser MTProto API Telegram client library,
MIT License
1.33k stars 181 forks source link

Best approach for uploading videos? #150

Open happymaskterriblefate opened 3 years ago

happymaskterriblefate commented 3 years ago

Hi there,

I'm wondering the best way to upload an mp4 video using gramjs. Right now I can successfully upload a video via:

sendFile(client, entity, { file });

where the entity is a Channel instance and the file is a filepath to the video on my hard drive. However, when the video appears in the channel, it has no thumbnail, and it says the length of the video is 0:00:00.

If I drag and drop the same file through the telegram desktop app, the thumbnail appears and the video length is correct. Are there extra steps or a different approach I need to take when uploading a video using gramjs?

Thank you in advance, and thank you for the great module!

painor commented 3 years ago

for files under 10mb you'll need to set those attributes yourself. For thumbnail there is a thumbnail property that you can use.

For video length/heigh/width as well you can set those attributes in the attributes field. Telegram desktop does this when you start uploading client side.

happymaskterriblefate commented 3 years ago

Thank you -- I'll give it a shot and see if I can get it working. Will followup.

painor commented 3 years ago

good luck. it will look something like this

    client.sendFile(entity,{
        file,
        thumb:"path to thumb",
        attributes:[new Api.DocumentAttributeVideo({
            duration:duration,
            h:height,
            w:width,
        })]
    })
happymaskterriblefate commented 3 years ago

@painor That worked! Thank you so much. Is there a place in the docs where I can send a PR to add the solution to the recipe?

painor commented 3 years ago

@painor That worked! Thank you so much. Is there a place in the docs where I can send a PR to add the solution to the recipe?

you can add an example to the doc string of sendFile (in this repo) and they will be updated here https://gram.js.org/beta/classes/client_telegramclient.telegramclient.html#sendfile

adhil72 commented 1 year ago

I have a image ArrayBuffer. can i use it as thumbnail. how to do it. i tried like this

` const toUpload = new CustomFile("video.mp4", video.byteLength, "", video as any); const thumbTopUpload = new CustomFile("img.jpg", thumbBuffer.byteLength, "", thumbBuffer as any);

callback('Uploading Video')
const file = await client.uploadFile({
    file: toUpload,
    workers: 3,
    onProgress: (e) => {
        console.log(e);

        callback(e)
    }
});

callback('Uploading thumbnail')

const thumb = await client.uploadFile({
    file: thumbTopUpload,
    workers: 3,
    onProgress: (e) => {
        console.log(e);

        callback(e)
    }
});

console.log(thumb);

client.sendFile(channel, { file, caption: msg, thumb: thumb}).then((res) => console.log(res)).catch((res) => console.log(res))`

But it says error like Error: Could not create file from [object Object] if i did send file without thumb like this client.sendFile(channel, { file, caption: msg, thumb: thumb}).then((res) => console.log(res)).catch((res) => console.log(res)) then it is working well. How to fix it