日期:2014-05-18  浏览次数:21048 次

c#提取视频缩略图问题
想做个东西,主要就是想提取视频文件的缩略图,使其显示在网页上,分不多望见谅

------解决方案--------------------
ffmepg
------解决方案--------------------
我把项目中用的ffmepg的类给你吧。。自己看
C# code

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.IO;
using System.Web;
namespace AUV5.Common
{
    public class FormatConverter
    {
        //FFmpeg配置信息
        private string ffmpegpath = "/FFmpeg/ffmpeg.exe";//FFmpeg的服务器路径
        private string imgsize = "400*300";     //视频截图大小
        private string videosize = "480*360"; //视频大小
        #region 也可将信息添加到配置文件中
        //public static string ffmpegpath = ConfigurationManager.AppSettings["ffmpeg"];
        //public static string imgsize = ConfigurationManager.AppSettings["imgsize"];
        //public static string videosize = ConfigurationManager.AppSettings["videoize"];
        #endregion

        private string destVideo = "";

        /// <summary>
        /// 视频路径
        /// </summary>
        public string DestVideo
        {
            get { return destVideo; }
            set { destVideo = value; }
        }
        private string destImage = "";

        /// <summary>
        /// 图片路径
        /// </summary>
        public string DestImage
        {
            get { return destImage; }
            set { destImage = value; }
        }

        /// <summary>
        /// 视频长度
        /// </summary>
        public string VideoLength { get; set; }

        //文件类型
        public enum VideoType
        {
            [Description(".avi")]
            AVI,
            [Description(".mov")]
            MOV,
            [Description(".mpg")]
            MPG,
            [Description(".mp4")]
            MP4,
            [Description(".flv")]
            FLV
        }
        /// <summary>
        /// 返回枚举类型的描述信息
        /// </summary>
        /// <param name="myEnum"></param>
        /// <returns></returns>
        private string GetDiscription(System.Enum myEnum)
        {

            System.Reflection.FieldInfo fieldInfo = myEnum.GetType().GetField(myEnum.ToString());
            object[] attrs = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), true);
            if (attrs != null && attrs.Length > 0)
            {
                DescriptionAttribute desc = attrs[0] as DescriptionAttribute;
                if (desc != null)
                {
                    return desc.Description.ToLower();
                }
            }
            return myEnum.ToString();
        }
        //将GetDesCription定义为扩展方法,需.net3.5
        //public static string Description(this Enum myEnum)
        //{
        //    return GetDiscription(myEnum);
        //}

        //构造函数
        //创建目录
        public FormatConverter()
        {
        }

        #region 使用FFmpeg进行格式转换

        /// <summary>
        /// 运行格式转换
        /// </summary>
        /// <param name="sourceFile">要转换文件绝对路径</param>
        /// <param name="destPath">转换结果存储的相对路径</param>
        /// <param name="videotype">要转换成的文件类型</param>
        /// <param name="createImage">是否生成截图</param>
        /// <returns>
        /// 执行成功返回空,否则返回错误信息
        /// </returns>
        public string Convert(string sourceFile, string destPath,string uniquename, VideoType videotype, bool createImage,bool getDuration)
        {
            //取得ffmpeg.exe的物理路径
            string ffmpeg = System.Web.HttpContext.Current.Server.MapPath(ffmpegpath);
            if (!File.Exists(ffmpeg))
            {
                return "找不到格式转换程序!";
            }