UWP笔记-使用FFmpeg编解码

在开发UWP媒体应用的时候,使用的MediaElement可以支持主流的格式,不过还是有些格式本地编解码器是不支持的,如.flv、.rmvb等,这里讲到的是第三方开源库FFmpeg,可以直接播放更多的格式。

安装Nuget:  FFmpegInterop

App.xmal.cs

using FFmpegInterop;

sealed partial class App : Application, ILogProvider
    {
        /// <summary>
        /// 初始化单一实例应用程序对象。这是执行的创作代码的第一行,
        /// 已执行,逻辑上等同于 main() 或 WinMain()。
        /// </summary>
        public App()
        {
            this.InitializeComponent();
            this.Suspending += OnSuspending;
            FFmpegInteropLogging.SetLogLevel(LogLevel.Info);
            FFmpegInteropLogging.SetLogProvider(this);
        }

        public void Log(LogLevel level, string message)
        {
            System.Diagnostics.Debug.WriteLine("FFmpeg ({0}): {1}", level, message);
        }
        /// <summary>
        /// 在应用程序由最终用户正常启动时进行调用。
        /// 将在启动应用程序以打开特定文件等情况下使用。
        /// </summary>
        /// <param name="e">有关启动请求和过程的详细信息。</param>
}

MainPage.xaml.cs:

using FFmpegInterop;

bool forceDecodeVideo_bool = videoDecode_toggleSwitch.IsOn;//为true时,会强制转换,软解
bool forceDecodeAudio_bool = audoDecode_toggleSwitch.IsOn;,//为false时,硬解

#pragma warning disable CS0618 // 类型或成员已过时
FFmpegInteropMSS FFmpegMss = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(video.Video_Stream, forceDecodeAudio_bool, forceDecodeVideo_bool);
#pragma warning restore CS0618 // 类型或成员已过时
var main_mediaSource = FFmpegMss.GetMediaStreamSource();
MediaElement element = new MediaElement();
element.SetMediaStreamSource(main_mediaSource);

 

Demo:  https://github.com/singhwong/LearnFFmpeg_Old.git

 官方源码地址:  https://github.com/Microsoft/FFmpegInterop.git 

有很多使用方法,还有不同的Nuget包,这只是其中一种,也是微软官方的实例demo,有些年头了。

 

 

 

 

posted @ 2019-04-02 01:52  singhwong  阅读(289)  评论(0编辑  收藏  举报