Roman
Roman

Reputation: 1711

How can I change the videotrack in libVlcSharp when recording video?

I am trying to record a live stream video into a file using libVlcSharp. That live stream contains two different video tracks, and by default when I record the file, the second track is being recorded instead of the first one. Strangely, when I play the video instead of recording it, the first track is selected. This is a snippet of my code:

var id = Guid.NewGuid();
using var libvlc = new LibVLC(enableDebugLogs: true);
var path = Path.Combine(_persistenceSettings.DataPath, id + ".ts");
using var media = new Media(libvlc, new Uri(source));
media.AddOption(":sout=#file{dst=" + path + "}"); // If I remove these two options, then the first track is played. With them, the second track is recorded
media.AddOption("sout-keep");

_mediaPlayer = new MediaPlayer(media);
if (!_mediaPlayer.Play())
{
    throw new CameraRecordingException($"Camera {source} could not be initialized");
}

// I know that the second track (the one I don't want) has less than 1000px, whereas the first one is 1920x1080
var track = media.Tracks.FirstOrDefault(t => t.TrackType == TrackType.Video && t.Data.Video.Height > 1000);
_mediaPlayer.SetVideoTrack(track.Id);

After setting the new VideoTrack id (which is correctly set, because I have checked property _mediaPlayer.VideoTrack and it is changed), I have also tried to stop and play again the playback, and nothing happens, the recorded video is still the second track.

I have also tried to parse media instead of playing it (in order to get the tracks, otherwise tracks list is empty), and play it after SetVideoTrack call, but if I don't call Play the tracks list is always empty, this way:

var id = Guid.NewGuid();
using var libvlc = new LibVLC(enableDebugLogs: true);
var path = Path.Combine(_persistenceSettings.DataPath, id + ".ts");
using var media = new Media(libvlc, new Uri(source));
media.AddOption(":sout=#file{dst=" + path + "}"); // If I remove these two options, then the first track is played on a new window. With them, the second track is recorded
media.AddOption("sout-keep");

await media.Parse(MediaParseOptions.ParseNetwork);
while(!media.isParsed) { }
_mediaPlayer = new MediaPlayer(media);
var track = media.Tracks.FirstOrDefault(t => t.TrackType == TrackType.Video && t.Data.Video.Height > 1000); // Here, media.Tracks is empty
_mediaPlayer.SetVideoTrack(track.Id);
if (!_mediaPlayer.Play())
{
    throw new CameraRecordingException($"Camera {source} could not be initialized");
}

Question: How can I select the video track before starting to record the file?

Upvotes: 0

Views: 965

Answers (1)

Roman
Roman

Reputation: 1711

Actually, I have checked that video is recorded with both video tracks. The problem is that when trying to select the video track using libVlcSharp, the Ids are incorrect, that is:

  • If you want to change the video track, you need to call MediaPlayer.SetVideoTrack passing the track id as an argument
  • Accessing to Media.Tracks.Id returns an incorrect Id (in my case, there were two video tracks and one audio track, and their ids were 0, 1 and 2), so when calling SetVideoTrack the Id that I set was 1.
  • I know the Ids are incorrect because opening the ".ts" file with VLC program, and going to Tools --> Multimedia Info (I think that is the option, my VLC is in Spanish) --> Codec, you can see a property called "Original Id". In my case, they were 100, 101 and 200 for the three different tracks. Calling SetVideoTrack with Id 101 (the one I wanted) opened a new window and played the right track. That is another problem for me, instead of reusing the same window, but that is another problem...

Upvotes: 0

Related Questions