VLC cannot get length/duration from .ts file
VLC cannot get length/duration from .ts file
If we use function libvlc_media_new_callbacks to create the play media, and then play the video. The VLC cannot get correct length or duration from .ts extension video file, it will display "0";
It works normally with function libvlc_media_new_path.
libvlc_media_new_callbacks is a very useful function that allows users to read .ts video files directly from the memory.
Affected VLC versions:
VLC 3.0
VLC 4.0
Test Code Version:
VLC Library Version: LibVLCSharp.WinForms 3.5.1
VLC Version: 3.0.14
Visual Studio 2019 (C# .net 5.0)
Windows 10 (64 bit)
using LibVLCSharp.Shared;
using LibVLCSharp.WinForms;
using System;
using System.IO;
using System.Windows.Forms;
namespace VideoLengthTest
{
static class Program
{
/// <summary>
/// Modify the folder where the VLC component is stored
/// </summary>
private const string VlcInitPath = @"C:\Program Files\VideoLAN\VLC";
/// <summary>
/// Modify the file location of the video that needs to be played
/// </summary>
private const string PlayFilePath = @"D:\test-file.ts";
[STAThread]
static void Main()
{
var form = new Form()
{
Width = 800,
Height = 600,
StartPosition = FormStartPosition.CenterScreen,
};
Core.Initialize(VlcInitPath);
var libVLC = new LibVLC();
var player = new VideoView() { Dock = DockStyle.Fill };
player.MediaPlayer = new MediaPlayer(libVLC);
form.Shown += (object sender, EventArgs e) =>
{
/*
* If you change the variable to false, you cannot get the video file length/duration.
* If you change the variable to true, you can get the video file length/duration.
*/
var useFilePath = false;
if (useFilePath)
{
player.MediaPlayer.Play(new Media(libVLC, PlayFilePath));
}
else
{
player.MediaPlayer.Play(new Media(libVLC, new StreamMediaInput(File.OpenRead(PlayFilePath))));
}
};
form.Controls.Add(player);
if (true)
{
var button = new Button()
{
Text = "Click to get .ts file length/duration",
Height = 50,
Dock = DockStyle.Top,
};
button.Click += (object sender, EventArgs e) =>
{
MessageBox.Show("Get Length:" + player.MediaPlayer.Length);
};
form.Controls.Add(button);
}
Application.Run(form);
}
}
}
I checked the source code and found that after the following modifications, libvlc_media_new_callbacks can return the correct length. This may not be the best solution, but it does make the program return the correct result.
Changed file:
\vlc-3.0.14\modules\demux\mpeg\ts.c
Function name:
static int Demux( demux_t *p_demux )
Code:
if (true)
{
ts_es_t* p_es = p_pid->u.p_stream->p_es;
ts_pmt_t* p_pmt = p_es->p_program;
if (p_pmt->i_last_dts < 1)
{
int i_program = 0;
ProbeEnd(p_demux, i_program);
}
}
Changed position (picture attachment):