从媒体播放器控件获取宽度和高度

时间:2014-04-28 17:36:08

标签: c# winforms video wmplib

我有一个C#应用程序,它使用Winforms中的Windows Media Player控件(WMPLib)显示视频。

我可以正确显示视频,但我必须手动插入父控件的宽度和高度,以便视频看起来不会失真。我的用例已经发展到我不再事先知道视频尺寸是什么的程度,因此我需要找到一种方法来获取视频的实际宽度和高度。

我一直在研究是否可以在视频加载到要播放的播放列表时执行此操作,然后将这些值传递给父控件的宽度和高度参数,但我已经做得很短。 ..

这甚至可能吗?或者是否只能在播放视频时获取该信息?我应该从哪里开始?

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以在C#应用程序中使用FFmpeg工具从视频文件中获取元数据信息,包括宽度和高度信息。不知道您的应用程序工作流程,我无法建议您何时应该阅读元数据信息。但有一种可能的情况就是这样。

  1. 用户从磁盘中选择一堆视频文件,告诉您应用程序将它们加载到播放列表中进行播放。
  2. 在显示加载动画时,您可以通过FFmpeg工具运行每个视频,以获取元数据信息,您可以将这些信息与文件名一起存储在内存或数据库中。
  3. 完成第2步后,用户可以看到播放列表中的视频。
  4. 当用户选择要播放的视频时,您可以加载相应的宽度和高度信息,并在应用程序中设置媒体播放器控件的属性。
  5. 当用户移动到其他视频时,请重复步骤4.
  6. 现在FFmpeg有很多C#库。一些付费,一些免费。但是我一直在使用一些我从同事那里获得的代码。它不仅仅是获取元数据。它还执行视频到FLV格式的转换。我正在将其上传到github here。提取元数据的实际方法如下所示。

        public void GetVideoInfo(VideoFile input)
        {
            //set up the parameters for video info
            string Params = string.Format("-i {0}", input.Path);
            string output = RunProcess(Params);
            input.RawInfo = output;
    
            //get duration
            Regex re = new Regex("[D|d]uration:.((\\d|:|\\.)*)");
            Match m = re.Match(input.RawInfo);
    
            if (m.Success)
            {
                string duration = m.Groups[1].Value;
                string[] timepieces = duration.Split(new char[] { ':', '.' });
                if (timepieces.Length == 4)
                {
                    input.Duration = new TimeSpan(0, Convert.ToInt16(timepieces[0]), Convert.ToInt16(timepieces[1]), Convert.ToInt16(timepieces[2]), Convert.ToInt16(timepieces[3]));
                }
            }
    
            //get audio bit rate
            re = new Regex("[B|b]itrate:.((\\d|:)*)");
            m = re.Match(input.RawInfo);
            double kb = 0.0;
            if (m.Success)
            {
                Double.TryParse(m.Groups[1].Value, out kb);
            }
            input.BitRate = kb;
    
            //get the audio format
            re = new Regex("[A|a]udio:.*");
            m = re.Match(input.RawInfo);
            if (m.Success)
            {
                input.AudioFormat = m.Value;
            }
    
            //get the video format
            re = new Regex("[V|v]ideo:.*");
            m = re.Match(input.RawInfo);
            if (m.Success)
            {
                input.VideoFormat = m.Value;
            }
    
            //get the video format
            re = new Regex("(\\d{2,3})x(\\d{2,3})");
            m = re.Match(input.RawInfo);
            if (m.Success)
            {
                int width = 0; int height = 0;
                int.TryParse(m.Groups[1].Value, out width);
                int.TryParse(m.Groups[2].Value, out height);
                input.Width = width;
                input.Height = height;
            }
            input.infoGathered = true;
        }
    

    代码可能会使用一些优化,包括IDisposable的实现和dispose模式。不过,它应该是一个很好的起点。您还需要从here下载FFmpeg可执行文件,并使用FFmpeg可执行文件的路径更新App.config文件。

相关问题