Reputation: 1711
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
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:
Upvotes: 0