AmarnathCJD / gogram

Full-native implementation of MTProto protocol on Golang.
GNU General Public License v3.0
207 stars 41 forks source link

Videos uploaded from a subdirectory are not playable #127

Closed dani3l0 closed 3 months ago

dani3l0 commented 3 months ago

Describe the bug Uploading large videos to a channel does not make them playable, they are treated as documents. To watch, I need to download them locally and then play with native video player.

Smaller (and shorter) videos work as expected, I can directly play them in my tg clients.

To Reproduce Steps to reproduce the behavior:

  1. Just upload some big video somewhere (channel in my case)

  2. Code snippet I am using:

    
    import (
    "myproject/config"
    
    tg "github.com/amarnathcjd/gogram/telegram"
    )

var client *tg.Client

func main() { client, _ = tg.NewClient(tg.ClientConfig{ AppID: config.TelegramApiId, AppHash: config.TelegramApiHash, })

client.ConnectBot(config.TelegramBotToken)

// ChannelId is provided as int
client.SendMedia(config.ChannelId, "./output.mp4", &tg.MediaOptions{
    Caption:       "*bold* _italic_ one two three",
    ParseMode:     "markdown",
})

}



**Expected behavior**
Large, long videos are playable in Telegram clients.

**Screenshots**

This is a large MP4 video. Duration: 40 minutes. Can't be played directly in Telegram client.
![image](https://github.com/user-attachments/assets/475e7481-57cc-4091-8ebb-dccfa1021044)

This is a 9MB video. Duration: 40 seconds. **Works as expected.**
![image](https://github.com/user-attachments/assets/03b02f5e-6b83-4df3-929e-3315a0bd3938)

**Desktop:**
 - OS: Fedora Workstation 40, Golang from Fedora dnf repos (go1.22.5 linux/amd64)
 - Telegram client: Materialgram 5.2.1.1 flatpak
 - gogram v0.0.0-20240803081110-76b510013b18

**Additional context**

Tried also re-transcoding video with ffmpeg to various formats and repacking containers.
To be precise, I tried with transcoding to h264 (mp4 v1 and v2) and hevc. Unfortunately, neither helps.

**BTW, thanks for your great project!**
AmarnathCJD commented 3 months ago

Screenshot 2024-08-05 005055

working as expected from my side

can you send the mediainfo output of the file you are trying to send.?

dani3l0 commented 3 months ago

Here it is:

General
ID                                       : 1 (0x1)
Complete name                            : KATOWICE  Pt. 2.08.2024r. W. Olszański, M. Osadowski NPTV.pl Rodacy Kamraci [6VNRAcrSR].mp4
Format                                   : MPEG-TS
File size                                : 871 MiB
Duration                                 : 2 h 49 min
Overall bit rate mode                    : Variable
Overall bit rate                         : 720 kb/s
FileExtension_Invalid                    : ts m2t m2s m4t m4s tmf ts tp trp ty

Video
ID                                       : 257 (0x101)
Menu ID                                  : 1 (0x1)
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : High@L4
Format settings                          : CABAC / 1 Ref Frames
Format settings, CABAC                   : Yes
Format settings, Reference frames        : 1 frame
Format settings, GOP                     : M=1, N=60
Codec ID                                 : 27
Duration                                 : 2 h 49 min
Width                                    : 1 920 pixels
Height                                   : 1 080 pixels
Display aspect ratio                     : 16:9
Standard                                 : NTSC
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Color range                              : Limited
Color primaries                          : BT.601 PAL
Transfer characteristics                 : BT.601
Matrix coefficients                      : BT.601

Audio
ID                                       : 256 (0x100)
Menu ID                                  : 1 (0x1)
Format                                   : AAC LC
Format/Info                              : Advanced Audio Codec Low Complexity
Format version                           : Version 4
Muxing mode                              : ADTS
Codec ID                                 : 15-2
Duration                                 : 2 h 49 min
Bit rate mode                            : Variable
Channel(s)                               : 2 channels
Channel layout                           : L R
Sampling rate                            : 48.0 kHz
Frame rate                               : 46.875 FPS (1024 SPF)
Compression mode                         : Lossy
Delay relative to video                  : 43 ms

And to give more background, it's a video downloaded from https://dlive.tv/ via yt-dlp

AmarnathCJD commented 3 months ago

Its maybe issue with telegram's video formats?

for one file which uploaded as document i ran this

ffmpeg -hwaccel nvdec -i input.mp4 -vf "scale=w=1280:h=720:force_original_aspect_ratio=decrease" -c:v h264_nvenc -preset p1 -b:v 2000k -maxrate 2000k -bufsize 4000k -c:a aac -b:a 128k -movflags +faststart output.mp4

and got uploaded as video. try and experiement, I think its not issue with lib

please update on this.

AmarnathCJD commented 3 months ago

Any updates?

dani3l0 commented 3 months ago

Not yet, I've been kinda busy today :<

Will let you know tomorrow

dani3l0 commented 3 months ago

Okay, I guess I've discovered the real issue. My observations were a bit misleading, actually it doesn't matter if a video is big or not. For testing, I've been copying small videos to the same directory I run my project in, that's why they worked.

I think, the pattern is when video file is served from a different directory. Here's an updated code snippet:

package main

import (
    "myproject/config"

    tg "github.com/amarnathcjd/gogram/telegram"
)

var client *tg.Client

func main() {
    client, _ = tg.NewClient(tg.ClientConfig{
        AppID:   config.TelegramApiId,
        AppHash: config.TelegramApiHash,
    })

    client.ConnectBot(config.TelegramBotToken)

    // Serve video from a subdirectory
    client.SendMedia(config.ChannelId, "./downloads/output.mp4", &tg.MediaOptions{
        Caption:       "*bold* _italic_ one two three",
        ParseMode:     "markdown",
    })
}

Uploading video this way will result in this: image

Still I'm not 100% sure, coz the problem may be somewhere else. Please take a look :>

RoseLoverX commented 3 months ago

@dani3l0 maybe ffmpeg commands hardcoded only works for current directory.?

dani3l0 commented 3 months ago

It has nothing to do with ffmpeg. Video formats are ok

RoseLoverX commented 3 months ago

@dani3l0 are the document attributes ok too.?

dani3l0 commented 3 months ago

Must be. Video uploaded from current working dir is playable and everything is ok. Problem happens only when serving the file from different directory.

RoseLoverX commented 3 months ago

@dani3l0 thats so strange

dani3l0 commented 3 months ago

Check the snippet.

AmarnathCJD commented 3 months ago

oh subdriectory or diffrent directory?

dani3l0 commented 3 months ago

Both

AmarnathCJD commented 3 months ago

can you come @rosexchat .

AmarnathCJD commented 3 months ago

Screenshot 2024-08-06 232142

from diff directory

AmarnathCJD commented 3 months ago

same for ./blah/bx/video_2024-08-06_21-32-07_339.mp4

AmarnathCJD commented 3 months ago

Okay, I guess I've discovered the real issue. My observations were a bit misleading, actually it doesn't matter if a video is big or not. For testing, I've been copying small videos to the same directory I run my project in, that's why they worked.

I think, the pattern is when video file is served from a different directory. Here's an updated code snippet:

package main

import (
  "myproject/config"

  tg "github.com/amarnathcjd/gogram/telegram"
)

var client *tg.Client

func main() {
  client, _ = tg.NewClient(tg.ClientConfig{
      AppID:   config.TelegramApiId,
      AppHash: config.TelegramApiHash,
  })

  client.ConnectBot(config.TelegramBotToken)

  // Serve video from a subdirectory
  client.SendMedia(config.ChannelId, "./downloads/output.mp4", &tg.MediaOptions{
      Caption:       "*bold* _italic_ one two three",
      ParseMode:     "markdown",
  })
}

Uploading video this way will result in this: image

Still I'm not 100% sure, coz the problem may be somewhere else. Please take a look :>

I think the output.mp4 might be incomplete? can u share the exact file in some place?

AmarnathCJD commented 3 months ago

even tried videos from https://dlive.tv/p/loupdi+XCGWSkXIg

dani3l0 commented 3 months ago

Okay, something reeeally weird is happening here: If the output.mp4 file is incomplete, then why is it working when uploaded from working dir? wtf

I'll do further investigation and update on this

Anyways, there's a workaround by creating a symlink in working dir

AmarnathCJD commented 3 months ago

Ah your OS is Linux based, I did all test on Windows, maybe its Linux specific.

eos175 commented 3 months ago

use filepath.Abs()

AmarnathCJD commented 3 months ago

use filepath.Abs()

irrelevent ig, file path exists, gets uploaded

dani3l0 commented 3 months ago

I've played tday a bit with my project on a work pc with OpenSUSE Tumbleweed, with the same Go version. Now, even the symlink workaround doesn't work. Must be sth wrong on my side.

ig it's a hwaccel or codec issue, as mentioned earlier (also I have very poorly configured hwaccel on this pc)

If I finally get this to work, I'll update xd

AmarnathCJD commented 3 months ago

so its not an issue with the lib? :

dani3l0 commented 3 months ago

dunno. I tested other videos as well and some of them were working properly, some of them got sent as documents. Finally, idk where the problem is.

AmarnathCJD commented 3 months ago

dunno. I tested other videos as well and some of them were working properly, some of them got sent as documents. Finally, idk where the problem is.

did you test with another client for eg-: telethon (python)

dani3l0 commented 3 months ago

nope, I will do it now

dani3l0 commented 3 months ago

Telethon is ok

AmarnathCJD commented 3 months ago

Wow, ok, your system is Linux right Ill try out.

eos175 commented 3 months ago

Wow, ok, your system is Linux right Ill try out.

Why would the OS affect it?

AmarnathCJD commented 3 months ago

Wow, ok, your system is Linux right Ill try out.

Why would the OS affect it?

If the same code works in win, then why not.

eos175 commented 3 months ago

I don't understand how the same video would cause something to change. It must be something else. Could you share a zip file of the project to try to reproduce the issue?

dani3l0 commented 3 months ago

Tried to reproduce with an example code, but weird things keeps happening. On sample, one-file code video gets preview correctly. Must be a mistake somewhere in my project.

Sauce code to my project is here (made it public temporarily). You don't have to put ur own video, just set debug_mode: true in config.yml file and the whole problematic process will begin.

Oh, make sure u upload video from cache dir, this is when the issue happens.

AmarnathCJD commented 3 months ago

Found the issue; how about now/

dani3l0 commented 3 months ago

Seems like it's working flawlessly now :tada:

Thanks, good job :+1: