ffmpeginteropx / FFmpegInteropX

FFmpeg decoding library for Windows 10 UWP and WinUI 3 Apps
Apache License 2.0
205 stars 52 forks source link

Remove audiotrack from stream #425

Closed lrohrmann closed 3 months ago

lrohrmann commented 4 months ago

Hello,

I'm not sure if it is possible or not: I'm playing a rtsp stream and searching for a way to remove the audio track from it. With ffmpeg it seems to be the -an parameter to remove all audio, but I can't find a way to pass this parameter with FmpegInteropX and also I cant find another way to do this. Currently I just mute the mediaplayer, butthis feels more like a kind of workaround.

Does anybody have an idea how to solve this in the right way?

Thanks and best regards Lorenz

brabebhin commented 4 months ago

There are overloads which takes a property set to pass special parameters to ffmpeg. If that doesn't work, then we will need a special implementation for it.

lrohrmann commented 4 months ago

I have tried to add it to the ffmpeg config options list in this way:

FFmpegOptions = new Windows.Foundation.Collections.PropertySet {
    { "an", 1 } 
...

But this hasn't any effect. Can you tell me the way to do it with overloads, then I will test it.

Thanks!

brabebhin commented 4 months ago

You need to pass that MediaSourceConfig with ffmpeg options you want in the CreateFromStream or CreateFromUri methods. Setting those afterwards will not have an effect in this case.

lrohrmann commented 4 months ago

I have passed it in CreateFromUri, here is the relevant code part:

                        Config.FFmpegOptions["timeout"] = 5000000;
                        Config.FFmpegOptions["reconnect"] = 1;
                        Config.FFmpegOptions["reconnect_streamed"] = 1;
                        Config.FFmpegOptions["reconnect_on_network_error"] = 1;
                        Config.FFmpegOptions["max_delay"] = 500;
                        Config.FFmpegOptions["an"] = 1;

                        Task.Run(async () =>
                        {
                            try
                            {
                                FFmpegMSS = await FFmpegMediaSource.CreateFromUriAsync(sourceUri.AbsoluteUri, Config);

                                var playBackItem = FFmpegMSS.CreateMediaPlaybackItem();

                                MediaPlayer.Source = playBackItem;
                                MediaPlayer.Volume = 0;
                                MediaPlayer.IsMuted = true;
                                MediaPlayer.CommandManager.IsEnabled = false;
                                MediaPlayer.Play();

                            }
                            catch (Exception ex)
                            {
                                isPlaying = false;
                                log.Error($"Error in FFmpegCamera.Play Task.Run DebugId {debugId}", ex);
                            }

                        }).Wait();

But if I don't mute the MediaPlayer the Audio is still present.

brabebhin commented 4 months ago

If you set the SelectedIndex of MediaPlaybackItem AudioTracks property to - 1, does it fix the issue? I suspect MediaPlayer will forcefully select the first stream on media open, but if you set AutoPlay to false, you will be able to set the selected audio stream before playback starts.

lrohrmann commented 4 months ago

When I try to set SelectedIndex of MediaPlaybackItem AudioTracks property to -1 I get an exception "Value does not fall within the expected range." But also I can see that the SelectedIndex is allready set to -1 after CreateMediaPlaybackItem().

brabebhin commented 4 months ago

Alright, this will require some new development. I will think of how this can be done.

lrohrmann commented 4 months ago

I think I have found another solutiuon for this. Adding Config.FFmpegOptions["allowed_media_types"] = "video"; seems to fix the issue. But maybe your exclude filter improvement is also helpful for other use cases.

brabebhin commented 4 months ago

Thank you for sharing the solution. Much appreciated.