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

发布于 2024-09-05 05:56:47 字数 720 浏览 3 评论 0原文

using Microsoft.DirectX.AudioVideoPlayback;

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

“已安装对应用程序域进行访问尝试。”

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

但是向下找到了 cods 我在 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);

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

using Microsoft.DirectX.AudioVideoPlayback;

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

“Installed an access attempt was made to the application domain.”

“vvideo” instance is created error msj ://

But down found cods I found a working code in c #. but asp.net does not work

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);

I DON'T TAKE VIDEO DURATION BOYS :/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小清晰的声音 2024-09-12 05:56:47

我在任何使用 Function 的地方都创建了 Function ;)

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);
    }
}

I make Function everywhere it uses ;)

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);
    }
}
水水月牙 2024-09-12 05:56:47

首先,我尝试了 FFMPEG Wrapper,但在找到一个简单的解决方案后出现错误。

您可以使用此块包:

Install-Package Microsoft.WindowsAPICodePack-Shell -Version 1.1.0

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

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

ShellFile so = ShellFile.FromFilePath("your file path");
double.TryParse(so.Properties.System.Media.Duration.Value.ToString(), out var 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 中,因此它直接给出小时:分钟:秒。

First, I try FFMPEG Wrapper but it gets an error after found an easy solution.

You can use this nugget package:

Install-Package Microsoft.WindowsAPICodePack-Shell -Version 1.1.0

In your project add two namespaces

using Microsoft.WindowsAPICodePack.Shell;
using.Microsoft.WindowsAPICodePack.Shell.PropertySystem;

ShellFile so = ShellFile.FromFilePath("your file path");
double.TryParse(so.Properties.System.Media.Duration.Value.ToString(), out var 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;
}

Here i store seconds in TimeSpan so its directly give hours:minutes:seconds.

我的痛♀有谁懂 2024-09-12 05:56:47

您可以使用我们的 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;

You can use our wrapper for ffprobe Alturos.VideoInfo.
You can use it simply by installing the nuget package. Also the ffprobe binary is required.

PM> install-package Alturos.VideoInfo

Example

var videoFilePath = "myVideo.mp4";

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

var duration = analyzeResult.VideoInfo.Format.Duration;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文