ffmpeg的简单使用

获取视频的旋转角度

//VideoName 视频文件,绝对路径
 public int RotateAngle(HttpServerUtilityBase Server,string VideoName)
        {
            ConvertVideo(Server, VideoName);
            int introtate = Convert.ToInt32(rotatestr);
            return introtate;
        }
        public void ConvertVideo(HttpServerUtilityBase Server,string VideoName)
        {
            string ffmpeg = (ffmpeg.exe的路径);
            string strArg = ffmpeg + " -i " + VideoName;
            Process p = new Process();//建立外部调用线程
            p.StartInfo.FileName = ffmpeg;//要调用外部程序的绝对路径
            p.StartInfo.Arguments = strArg;
            p.StartInfo.UseShellExecute = false;//不使用操作系统外壳程序启动线程(一定为FALSE,详细的请看MSDN)
            p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中(这个一定要注意,FFMPEG的所有输出信息,都为错误输出流,用StandardOutput是捕获不到任何消息的...mencoder就是用standardOutput来捕获的)
            p.StartInfo.CreateNoWindow = false;//不创建进程窗口
            p.ErrorDataReceived += new DataReceivedEventHandler(Output);//外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
            p.Start();//启动线程
            p.BeginErrorReadLine();//开始异步读取
            p.WaitForExit();//阻塞等待进程结束
            p.Close();//关闭进程
            p.Dispose();//释放资源
        }

     
        private  void Output(object sendProcess, DataReceivedEventArgs output)
        {
            if (!String.IsNullOrEmpty(output.Data))
            {
                string key = "rotate";
                 string temp = output.Data.ToString();
                bool isContains = temp.IndexOf(key, StringComparison.OrdinalIgnoreCase) >= 0;//true
                if (isContains)
                {
                    var rotate = output.Data.ToString();
                    int index = rotate.IndexOf(":")+1;
                    rotate = rotate.Substring(index, rotate.Length-index);
                    rotatestr = rotate;
                }
            }
        }

调用

 int introtate = RotateAngle(Server, VideoName);

获取视频的帧宽度和帧高度

  //ffmpegPath ffmpeg.exe的路径
  //videoFilePath 视频文件,绝对路径
  //null表示获取宽度或高度失败
 public string GetMovWidthAndHeight(string ffmpegPath, string videoFilePath, out int? width, out int? height)
        {
            try
            {
                //判断文件是否存在
                if (!Directory.Exists(videoFilePath))
                {
                    width = null;
                    height = null;
                }
                string output;
                string error;
                ExecuteCommand("\"" + ffmpegPath + "\"" + " -i " + "\"" + videoFilePath + "\"", out output, out error);
                if (string.IsNullOrEmpty(error))
                {
                    width = null;
                    height = null;
                }

                //通过正则表达式获取信息里面的宽度信息
                Regex regex = new Regex("(\\d{2,4})x(\\d{2,4})", RegexOptions.Compiled);
                Match m = regex.Match(error);
                if (m.Success)
                {
                    width = int.Parse(m.Groups[1].Value);
                    height = int.Parse(m.Groups[2].Value);
                }
                else
                {
                    width = null;
                    height = null;
                }
                return width + "*" + height;
            }
            catch (Exception)
            {
                width = null;
                height = null;
                return "";
            }
        }

执行一条command命令

/// <summary>
/// 执行一条command命令
 /// </summary>
 /// <param name="command">需要执行的Command</param>
 /// <param name="output">输出</param>
 /// <param name="error">错误</param>
public static void ExecuteCommand(string command, out string output, out string error)
        {
            try
            {
                //创建一个进程
                Process pc = new Process();
                pc.StartInfo.FileName = command;
                pc.StartInfo.UseShellExecute = false;
                pc.StartInfo.RedirectStandardOutput = true;
                pc.StartInfo.RedirectStandardError = true;
                pc.StartInfo.CreateNoWindow = true;
                //启动进程
                pc.Start();
                //准备读出输出流和错误流
                string outputData = string.Empty;
                string errorData = string.Empty;
                pc.BeginOutputReadLine();
                pc.BeginErrorReadLine();

                pc.OutputDataReceived += (ss, ee) =>
                {
                    outputData += ee.Data;
                };
                pc.ErrorDataReceived += (ss, ee) =>
                {
                    errorData += ee.Data;
                };
                //等待退出
                pc.WaitForExit();
                //关闭进程
                pc.Close();
                //返回流结果
                output = outputData;
                error = errorData;
            }
            catch (Exception e)
            {
                output = null;
                error = null;
            }
        }

调用

   string WidthAndHeight = GetMovWidthAndHeight(ffmpeg, VideoName, out width, out height);

获取视频的某一帧图片

  public string GetPicFromVideo(HttpServerUtilityBase Server, string VideoName)
        {
            int? width;
            int? height;
            string ffmpeg;//ffmpeg.exe路径
            string WidthAndHeight;//图片的宽高
            string CutTimeFrame = "0.1";//获取的0.1帧
            string PicName;//视频图片的名字,绝对路径
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            startInfo.Arguments = " -i " + VideoName
                                + " -y -f image2 -ss " + CutTimeFrame
                                + " -t 0.001 -s " + WidthAndHeight
                                + " " + PicName;  //設定程式執行參數
            try
            {
                System.Diagnostics.Process.Start(startInfo);
                Thread.Sleep(2000);//线程挂起,等待ffmpeg截图完毕
            }
            catch (Exception e)
            {
                return "";
            }

            //返回视频图片路径
                return PicName;
        }

ffmpeg参数中文详细解释

https://blog.csdn.net/leixiaohua1020/article/details/12751349

注:
ffmpeg是使用命令行工具的

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值