Recent

Author Topic: PasLibVlc how to get current duration of video file?  (Read 322 times)

BosseB

  • Sr. Member
  • ****
  • Posts: 473
PasLibVlc how to get current duration of video file?
« on: November 09, 2024, 10:22:57 pm »
About 5 years ago I have created a video player/editor tool with Lazarus/Fpc and using PasLibVlc for the video handling functionality.
This has worked fine for handling downloaded mp4 videos.
I have added support for *any* video file type via a a fileopen source item "other media files" so I can actually load for example ts files (transport stream), which unlike mp4 files can be viewed while the download is on-going.

This works very well for static ts files but if the file is being written at the same time as it is being viewed in the player I need to set the player's media length dynamically as it is being written by another process.

How can this be accomplished?

I have two event handlers in the application which update the screen as the video plays:
Code: Pascal  [Select][+][-]
  1. //Update display of current video file duration
  2. procedure TfrmMain.vlcPlayerMediaPlayerLengthChanged(Sender: TObject; time: Int64);
  3. var
  4.   P: int64;
  5. begin
  6.   P := vlcPlayer.GetVideoLenInMs();
  7.   pgbProgress.Max := P div 1000;
  8.   lblTime.Caption := vlcPlayer.GetVideoLenStr('hh:mm:ss');
  9.   lblLength.Caption := vlcPlayer.GetVideoLenStr('hh:mm:ss');
  10. end;
  11.  
  12. //Update display of the current playing position of the video
  13. procedure TfrmMain.vlcPlayerMediaPlayerTimeChanged(Sender: TObject; time: Int64);
  14. var
  15.   P, L: int64;
  16. begin
  17.   if FUserAction = 0 then// not dragging with mouse
  18.   begin
  19.     P := vlcPlayer.GetVideoPosInMs() div 1000;
  20.     L := vlcPlayer.GetVideoLenInMs() div 1000;
  21.     L := L - P;
  22.     pgbProgress.Position := P + 1;
  23.     pgbProgress.Position := P ;
  24.     lblTime.Caption := vlcPlayer.GetVideoPosStr('hh:mm:ss.ms');
  25.     lblSeconds.Caption := IntToStr(P);
  26.     lblRemaining.Caption := FormatTimeDiff(L);
  27.   end;
  28. end;
  29.  

Unfortunately the vlcPlayerMediaPlayerLengthChanged() proc seems not to be triggered or else it does not work properly when the media file being played changes its size as new content is written to the end of the file.

Have I misunderstood the way these events work and is there something else I have to do in order to make it monitor the changing length of the video file being written and played at the same time?
It has to change the progressbar's max value dynamically in order for the progress to display properly for example.

I wonder if the PasLibVlc library actually triggers the event when the file is written or if it is only ever triggered when the file is initially being loaded from disk?

Any ideas, please?

PS: I am using an HP workstation laptop running Windows10 and the latest version of VLC (just checked). DS
« Last Edit: November 10, 2024, 12:02:16 pm by BosseB »
--
Bo Berglund
Sweden

BosseB

  • Sr. Member
  • ****
  • Posts: 473
Re: PasLibVlc how to get current duration of video file?
« Reply #1 on: November 10, 2024, 02:35:41 pm »
Well, it seems like VLC is not keeping track of the fact that the file it is playing can be written to as it is playing it....
Therefore my program also has no way to use existing functions based on VLC to update the end position.

So the only way forward I think is to create a restart function that would work like this:

- When initially starting playback memorize the length of the video in ms
- When playing gets within say 10s of the saved length then re-open the same file and set the new end time
- Jump to the saved play position and continue playing after resetting all admin stuff:

Procedure:
- Open file for playing and if it is a ts file do:
- Make note of the duration of the video in milliseconds
- While playing, when the video playback passes the memorized duration minus say 10s do the following:
  - Memorize the current play position in ms
  - Reopen the same file in the player
  - Position the playing location to the memorized play position
  - Read the new video length
  - Change the memorized video duration to the new length
  - Set the new trigger point for the next reload
  - Update any player visual controls according to new data (progress bar etc)

I believe that this functionality can be built into the current software using existing PasLibVlc functions.
--
Bo Berglund
Sweden

BosseB

  • Sr. Member
  • ****
  • Posts: 473
Re: PasLibVlc how to get current duration of video file?
« Reply #2 on: November 10, 2024, 06:43:59 pm »
I am further along now, but struggling with a strange "caching" problem....
If I want to update the end time of the player I have tried stopping it and then opening the same file again, but then it shows the video length as the one from the previous session...
It looks like it caches some data in case the same file is reopened and then uses the OLD erroneous data.
If I quickly open another file or two and then returns to the previous file then the length gets updated!!!!

So my question now regarding PasLibVlc is:
What is the proper procedure to purge all cached data from a played file? Totally and absolutely close and forget it.

If there is such a method then hopefully a close-file followed by that purge and then reopening the file would correctly update the length to the new value....

That should be possible to automate as described above.
--
Bo Berglund
Sweden

 

TinyPortal © 2005-2018