How to set total (i.e., constant/target/planned) duration of the video that is being streamed (live-like VoD) in the video player? #6073
Replies: 1 comment 2 replies
-
Right. This is an on-demand encoding issue, not solved by the client library (hls.js). Duration is based on the HLS Media Playlist and not set arbitrarily by the client. Only a VOD Playlist will give you a finite duration in HLS.js. I don't know of any open-source solutions to producing a VOD playlist where segments can be encoded on the fly, ideally in parallel and prioritized when requested by the client. This takes some coordination between encoder and file server. I don't think having a (ffmpeg live) Live->VOD HLS Playlist (where duration is unknown until encoding ended) and telling the client what the expected duration is would help unless you want to constrain playback to the time range that has been encoded. If that is what you want, you’ll need to build that application logic around HLS.js. You would need to get, deliver, and handle that duration value from your server somehow with custom logic. It's not a feature or flag you can turn on with ffmeg or HLS.js as far as I know. Another option is to set users expectations rather that solve on the fly encoding. Show a notification that video is being encoded when encountering a Live Playlist; The final duration will remain unknown until encoding is complete and they can see the progress and watch from the beginning in real-time. |
Beta Was this translation helpful? Give feedback.
-
I don't know where to ask this, but since browsers can't play HLS videos natively (without JS shenanigans) and I was able to stream a video using this library, I think this is a good place to ask.
Context
I have a video stored somewhere in a (server's) FS for now and in a DB later (PostgreSQL's large objects). This video was uploaded by a user (when I'll set up the DBMS), so it can have almost any format (including almost any codecs for video/audio tracks). The user's video now must be playable in any webview (browser, on any platform), so I have to convert it to something like AVC + AAC which are widely supported codecs (in the web). For this matter, I tried using
ffmpeg
but it didn't work (only withmpv
player; see this), so I tried to find some more sources of how to useffmpeg
for real-time video file conversion (+ streaming). Then I have found this. And then I modified (and fixed) it like so:FFmpeg command
(I putted it into
.justfile
underdefault
recipe.) I'm not fluent in FFmpeg, so I didn't change anything (to minimize the example command). Then I created anindex.html
using the example JS code from here:index.html
Then I used
serve
npm package CLI to serve the local dir (serve .
) on alocalhost:3000
. Now I run theffmpeg
command (usingj
forjust
) in a shell in the PWD whereindex.html
andinput.mkv
are present. Then I hit the play button on the video in the loaded localhost tab. And after a second or two it starts playing the video that is being converted (created) in real time. So I achieved "video streaming" (I also need to figure out how to enable back subtitle file creation and how to use/show subtitles in the video player).I've checked https://www.rfc-editor.org/rfc/rfc8216 and https://github.com/video-dev/hls.js/blob/master/docs/API.md for "how to set the total video duration" so that in the video player it will show the actual duration of the whole video (not the sum of the already downloaded segments). But changing different
DURATION
tags didn't affect anything and in this library's API doc I only found this: https://github.com/video-dev/hls.js/blob/master/docs/API.md#livedurationinfinity. This removes the total duration altogether, so it only shows current time since the first received segment.One fundamental problem I have is that I chose the "live"
ffmpeg
command instead of "VoD" variant. This means that if I start loading/fetching video data too late then it will start playback not from the first segment and if I start too late, then it will fail (if I removed the "temporary" files and new ones haven't been created yet). But if I instead pick the "VoD" command, then it will only create the playlist files (M3U8) after all the segments were created. With this approach, I will have everything already converted, and the full duration will be available and shown in the video player (I assume). But the client then has to wait as they first click the play button, then wait for the full conversion and only then the playback can be started. So I need to find a middle ground:input.mkv
);I really need guidance as this topic is pretty heavy (in "terms" and "knowledge") and I'm not a pro at it. Maybe there is another and better approach to stream videos on demand that I don't know of?
P.S. I'm using Firefox 119. I tried this in private tab, and it did work at least twice. Then I tried it again, and now it shows logs about loading new stuff, but there is nothing playing (it shows gray background with the spinner). Clearing any sort of data didn't help. I then tried normal tab, and suddenly it started working again. Does anyone know how this can be? Update: after I killed
ffmpeg
it now shows 3 or 4 seconds long (segment) video in both normal and private tab. Very strange indeed.Beta Was this translation helpful? Give feedback.
All reactions