Closed devoxin closed 1 year ago
Keyframes are used by the video decoder to assemble a full picture. if you space them out, for example, discord would show a black screen for a while until the first key frame arrives but audio would still play fine.
Not sure what's causing the desync audio/video I'll have to test. I've often played m3u8 live streams and from my experience if you get desynched audio you could simply stop it and start it again and usually it fixes it. That's not a real fix though because ideally it would work every time
Oh and btw, the original code uses a single ffmpeg process already without any tweaks
the original code uses a single ffmpeg process already without any tweaks
My bad! I suppose I misread the code to be spawning two instances of ffmpeg (my excuse is that it was late and I was trying to make TypeScript work and figure out how fluent-ffmpeg works haha).
If if helps with debugging, some additional information:
ffplay
I just tested with two different m3u8 streams and only one of those is having desync issues. Not sure what's different between them (they're the exact same audio and video codecs (AAC audio/ H264 video). I also tested VP8 and it's having the same desync issue as h264. Also played with the fps and didn't change anything. This is weird as hell
Ok I think I might have found the problem
Broken stream:
Functional stream:
Notice the EXT-X-DISCONTINUITY-SEQUENCE tag missing from the functional stream.
Discussion about ffmpeg's issues handling that tag is still ongoing (last message about it was 5 months ago) but I don't know if we can expect a fix anytime soon since issue has been open for 7+ years now https://trac.ffmpeg.org/ticket/5419
However, I could be mistaken and this could not be the issue at all.
You said that you had
a handful of mkv and mp4 local files
that were also broken. Can you share any details about these files?
However, I could be mistaken and this could not be the issue at all.
Just checked my source, there doesn't appear to be EXT-X-DISCONTINUITY tag in there. The link, for reference, is https://moviesphere-plex.amagi.tv/playlist.m3u8
that were also broken. Can you share any details about these files?
I can share ffprobe readings but cannot share the files themselves as they are full length movies unfortunately.
I had tested some short mp4 files as well but I can't remember which ones they were
Ok I think I might have figured out the fix. If you remove the -re
flag then it fixes the m3u8 stream you sent as well as the broken one I was testing yesterday. Not sure if this will fix your mkv files but for non-livestreams this could make the program consume a lot more RAM since ffmpeg is going to read the file as fast as it can and push it into our output streams
Update: Nvm about the RAM, just tested it with a local file and it looks the same as before hmm
After a lot of tweaking, I have been able to produce usable audio and video from a single ffmpeg process by changing my ffmpeg arguments to the following:
This is great, except when streaming via
play-live
followed by an m3u8 URL, there is about 1-1.5 seconds of audio delay (I have only tested with m3u8 URLs so far). I wondered if this was perhaps related to the keyframe options, right now the fps is set to24
to represent the typical fps of a movie. The video plays before the audio, so if ~24 frames have elapsed before the audio plays, that would suggest a 1 second delay which could indicate that keyframes need to be set. I don't have significant experience with h264 and keyframes, hence this issue -- have you any potential suggestions on how to reduce this desync?Edit: adding
'-vf tpad=start_duration=1100ms',
tooutputOptions
gets it very close but I don't think it's perfect. Possibly within 50-100ms. This also seems somewhat hacky, runningffplay <m3u8 url>
yields a delay-free stream; audio and video are synced perfectly. Discord's screen-share can sound pretty spot on so I'm optimistic that the same can be achieved here.