C#获取视频文件播放长度

添加引用SHELL32.DLL

根据网上代码改编

    public static class GetVideoLength
    {
        public static string GetMediaTimeLen(string path)
        {
            try
            {
                Shell32.Shell shell = new Shell32.Shell();
                //文件路径               
                Shell32.Folder folder = shell.NameSpace(path.Substring(0, path.LastIndexOf("\\")));
                //文件名称             
                Shell32.FolderItem folderitem = folder.ParseName(path.Substring(path.LastIndexOf("\\") + 2));
                if (Environment.OSVersion.Version.Major >= 6)
                { 
                    return folder.GetDetailsOf(folderitem, 27);
                }
                else 
                { 
                    return folder.GetDetailsOf(folderitem, 21);
                }
            }
            catch (Exception ex) { return null; }
        }

        public static int GetMediaTimeLenSecond(string path)
        {
            try
            {
                Shell32.Shell shell = new Shell32.Shell();
                //文件路径               
                Shell32.Folder folder = shell.NameSpace(path.Substring(0, path.LastIndexOf("\\")));
                //文件名称             
                Shell32.FolderItem folderitem = folder.ParseName(path.Substring(path.LastIndexOf("\\") + 2));
                string len;
                if (Environment.OSVersion.Version.Major >= 6)
                {
                    len =  folder.GetDetailsOf(folderitem, 27);
                }
                else
                {
                    len = folder.GetDetailsOf(folderitem, 21);                    
                }

                string[] str = len.Split(new char[] { ':' });
                int sum = 0;
                sum = int.Parse(str[0]) * 60 * 60 + int.Parse(str[1]) * 60 + int.Parse(str[2]);

                return sum;
            }
            catch (Exception ex) { return 0; }
        }
    }

 

转载于:https://www.cnblogs.com/futao/archive/2012/05/31/2528724.html

你可以使用FFmpeg库中的AVFormatContext和AVCodecContext来获取视频文件长度。以下是一个示例代码: ```c# using UnityEngine; using FFmpeg.AutoGen; using System; public class VideoInfo : MonoBehaviour { void Start() { string filePath = "path/to/your/video.mp4"; AVFormatContext* formatContext = null; AVCodecContext* codecContext = null; ffmpeg.av_register_all(); ffmpeg.avcodec_register_all(); int result = ffmpeg.avformat_open_input(&formatContext, filePath, null, null); if (result != 0) { Debug.LogError("Failed to open video file: " + result); return; } result = ffmpeg.avformat_find_stream_info(formatContext, null); if (result < 0) { Debug.LogError("Failed to find video stream information: " + result); ffmpeg.avformat_close_input(&formatContext); return; } int videoStreamIndex = -1; for (int i = 0; i < formatContext->nb_streams; i++) { if (formatContext->streams[i]->codec->codec_type == AVMediaType.AVMEDIA_TYPE_VIDEO) { videoStreamIndex = i; break; } } if (videoStreamIndex == -1) { Debug.LogError("Failed to find video stream in file"); ffmpeg.avformat_close_input(&formatContext); return; } codecContext = formatContext->streams[videoStreamIndex]->codec; double videoLengthInSeconds = (double)formatContext->duration / ffmpeg.AV_TIME_BASE; Debug.Log("Video length: " + TimeSpan.FromSeconds(videoLengthInSeconds).ToString(@"hh\:mm\:ss")); ffmpeg.avformat_close_input(&formatContext); } } ``` 该代码使用FFmpeg库打开指定的视频文件,找到视频流并从中提取信息,最后计算出视频长度。需要注意的是,该代码使用了一些不安全的指针操作,请确保你的代码环境支持使用指针。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值