如何使用asp.net获取视频时长

时间:2010-06-10 08:49:49

标签: asp.net get video-encoding duration

    using Microsoft.DirectX.AudioVideoPlayback;

    Video vvideo = new Video(FileUpload.FileName.ToString());
    StringBuilder sb = new StringBuilder();
    sb.Append(duration.toString());
    error message;

“安装了对应用程序域的访问尝试。”

“vvideo”实例创建错误msj://

但是发现了鳕鱼,我在c#中发现了一个有效的代码。但asp.net不起作用

string file1 = "c://ds.mpeg"
    IMediaPosition m_objMediaPosition = null;
    FilgraphManager m_objFilterGraph = new FilgraphManager();
    m_objFilterGraph.RenderFile(filename);
    m_objMediaPosition = m_objFilterGraph as IMediaPosition;

    int s = (int)m_objMediaPosition.Duration;
    int h = s / 3600;
    int m = (s - (h * 3600)) / 60;
    s = s - (h * 3600 + m * 60);

我不接受视频持续时间:/

3 个答案:

答案 0 :(得分:1)

我在任何地方使用函数;)

public string f_VideoDuration((add path)parameters)
        {

            try
            {

                string sInputVideo = Page.MapPath(add path);

                string sPath = " -i " + sInputVideo ;

                Process ffmepg = new Process();
                ffmepg.StartInfo.FileName = (Server.MapPath(ffmpeg.exe"));
                ffmepg.StartInfo.UseShellExecute = false;
                ffmepg.StartInfo.RedirectStandardOutput = true;
                ffmepg.StartInfo.RedirectStandardError = true;
                ffmepg.StartInfo.CreateNoWindow = true;
                ffmepg.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                ffmepg.StartInfo.Arguments = sPath;
                ffmepg.EnableRaisingEvents = true;
                ffmepg.Start();
                string sDuration = ffmepg.StandardError.ReadToEnd().ToString();
                ffmepg.Close();
                ffmepg.Dispose();
                sDuration = sDuration.Remove(0, sDuration.LastIndexOf("Duration: ") + 10);
                sDuration = sDuration.Substring(0, sDuration.IndexOf(","));
                return sDuration;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

答案 1 :(得分:0)

首先,我尝试使用FFMPEG包装器,但是找到一个简单的解决方案后,它得到了一个错误。

您可以使用此块包:

Install-Package Microsoft.WindowsAPICodePack-Shell -Version 1.1.0

在您的项目中添加两个名称空间

  

使用Microsoft.WindowsAPICodePack.Shell;       using.Microsoft.WindowsAPICodePack.Shell.PropertySystem;

ShellFile so = ShellFile.FromFilePath(your file path);
double nanoseconds;
double.TryParse(so.Properties.System.Media.Duration.Value.ToString(),
out nanoseconds);

if (nanoseconds > 0)
{
     double seconds = Convert100NanosecondsToMilliseconds(nanoseconds) / 1000;
     int ttl_seconds = Convert.ToInt32(seconds);
     TimeSpan time = TimeSpan.FromSeconds(ttl_seconds);
} 

public static double Convert100NanosecondsToMilliseconds(double nanoseconds)
{           
        return nanoseconds * 0.0001;
}

我在这里将秒存储在 TimeSpan 中,因此它可以直接提供小时:分钟:秒。

答案 2 :(得分:0)

您可以将我们的包装用于ffprobe Alturos.VideoInfo。 您只需安装nuget软件包即可使用它。另外,ffprobe二进制文件也是必需的。

PM> install-package Alturos.VideoInfo

示例

var videoFilePath = "myVideo.mp4";

var videoAnalyer = new VideoAnalyzer("ffprobe.exe");
var analyzeResult = videoAnalyer.GetVideoInfo(videoFilePath);

var duration = analyzeResult.VideoInfo.Format.Duration;