FFMPEG的.Net封装,FFmpeg.NET

0 篇文章 0 订阅
原文地址:  http://www.open-open.com/lib/view/open1354088827449.html

FFMPEG的.Net封装,FFmpeg.NET

这是FFMPEG库的一个.Net封装,设计为易于使用,并尽可能完整。具有以下功能:

  • Decode of video frames (can save them as BMPs or whatever format GDI+ supports, returns a Bitmap)
  • Decode of audio into PCM (which can then be saved into a WAV file)
  • The release will build into a single assembly - no FFMPEG dlls or any other files! Everything is linked in.
  • Encoding support
  • Some utilitiy classes to wrap WAV files.
  • Weed out the memory leaks.

项目主页:http://www.open-open.com/lib/view/home/1354088827449

相关文章: http://www.cnblogs.com/xiaotie/archive/2013/01/05/2845576.html


视频格式转Flv

using System;
using System.Web;
namespace JumboTCMS.Utils
{
    /// <summary>
    /// ffmpeg.exe调用
    /// </summary>
    public static class ffmpegHelp
    {
        /// <summary>
        /// 视频格式转为Flv
        /// </summary>
        /// <param name="vFileName">原视频文件地址</param>
        /// <param name="WidthAndHeight">宽和高参数,如:480*360</param>
        /// <param name="ExportName">生成后的FLV文件地址</param>
        /// <returns></returns>
        public static bool Convert2Flv(string vFileName, string WidthAndHeight, string ExportName)
        {
            try
            {
                vFileName = HttpContext.Current.Server.MapPath(vFileName);
                ExportName = HttpContext.Current.Server.MapPath(ExportName);
                string Command = " -i \"" + vFileName + "\" -y -ab 32 -ar 22050 -b 800000 -s " + WidthAndHeight + " \"" + ExportName + "\""; //Flv格式   
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo.FileName = @"ffmpeg.exe";
                p.StartInfo.Arguments = Command;
                p.StartInfo.WorkingDirectory = HttpContext.Current.Server.MapPath("~/bin/tools/");
                #region 方法一
                //p.StartInfo.UseShellExecute = false;//不使用操作系统外壳程序 启动 线程
                //p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中(这个一定要注意,FFMPEG的所有输出信息,都为错误输出流,用 StandardOutput是捕获不到任何消息的...
                //p.StartInfo.CreateNoWindow = false;//不创建进程窗口
                //p.Start();//启动线程
                //p.BeginErrorReadLine();//开始异步读取
                //p.WaitForExit();//等待完成
                //p.Close();//关闭进程
                //p.Dispose();//释放资源
                #endregion
                #region 方法二
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                p.Start();//启动线程
                p.WaitForExit();//等待完成
                p.Close();//关闭进程
                p.Dispose();//释放资源
                #endregion
            }
            catch (System.Exception e)
            {
                throw e;
            }
            return true;
        }
        /// <summary>
        /// 生成FLV视频的缩略图
        /// </summary>
        /// <param name="vFileName">视频文件地址</param>
        /// <param name="FlvImgSize">宽和高参数,如:240*180</param>
        /// <returns></returns>
        public static string CatchImg(string vFileName, string FlvImgSize, string Second)
        {
            if (!System.IO.File.Exists(HttpContext.Current.Server.MapPath(vFileName)))
                return "";
            try
            {
                string flv_img_p = vFileName.Substring(0, vFileName.Length - 4) + "_thumb.jpg";
                string Command = " -i \"" + HttpContext.Current.Server.MapPath(vFileName) + "\" -y -f image2 -ss " + Second + " -t 0.1 -s " + FlvImgSize + " \"" + HttpContext.Current.Server.MapPath(flv_img_p) + "\"";
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo.FileName = @"ffmpeg.exe";
                p.StartInfo.Arguments = Command;
                p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                p.StartInfo.WorkingDirectory = HttpContext.Current.Server.MapPath("~/bin/tools/");
                //不创建进程窗口
                p.StartInfo.CreateNoWindow = true;
                p.Start();//启动线程
                p.WaitForExit();//等待完成
                p.Close();//关闭进程
                p.Dispose();//释放资源
                System.Threading.Thread.Sleep(4000);
                //注意:图片截取成功后,数据由内存缓存写到磁盘需要时间较长,大概在3,4秒甚至更长;
                if (System.IO.File.Exists(HttpContext.Current.Server.MapPath(flv_img_p)))
                {
                    return flv_img_p;
                }
                return "";
            }
            catch
            {
                return "";
            }
        }
    }
}


  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值