其他分享
首页 > 其他分享> > 关于Ffmpeg的使用

关于Ffmpeg的使用

作者:互联网

1.资源下载

下载资源路径:http://ffmpeg.org/download.html#build-windows

 

 https://github.com/BtbN/FFmpeg-Builds/releases

 

 下载完成后解压,在bin目录下打开控制台,可检测下载版本或查看帮助

2.工具介绍(具体可以查看https://www.cnblogs.com/fps2tao/p/14234396.html)

  ffmpeg.exe:音视频转码、转换器
  ffplay.exe:简单的音视频播放器
  ffserver.exe:流媒体服务器(这个下载并没有发现)
  ffprobe.exe:简单的多媒体码流分析器

3.单机命令及其参数(具体可以查看https://blog.csdn.net/wangyjfrecky/article/details/81109810)

 3.1音频

 

3.2视频

  1.将视频全部转换为图片

ffmpeg.exe -i testsource/e26d801cea1f4ecab544c3bf8078c6b2.mp4 -r 1 -f image2 testsource/image-%d.jpeg
-i testsource/e26d801cea1f4ecab544c3bf8078c6b2.mp4表示指定资源地址
-r 1表示设置每秒提取一帧
-f image2表示采用的是image2格式
testsource/image-%d.jpeg表示输出地址,%d是顺序数字

  2.设置截取时间和截取张数

ffmpeg.exe -i testsource/e26d801cea1f4ecab544c3bf8078c6b2.mp4 -r 1 -ss 00:00:24 -t 2 -f image2 -s 48x48 testsource/image-%d.jpeg
-ss 00:00:24表示设置起始时间
-t 2表示持续的时间 如果需要使用帧来设置持续时间可以使用 -vframes 2
-s 48x48表示设置输出图片的大小,单位像素
 

 

 

4.C#结合使用按钮

/// <summary>
        /// 根据视频截取封面(封装)
        /// </summary>
        /// <param name="ImagePath">视频路径</param>
        /// <param name="ScrrenTime"></param>
        /// <returns></returns>
        public static string GetScrrenImageFunc(string ImagePath, int ScrrenTime)
        {
            string ReStr = string.Empty;
            try
            {
                if (string.IsNullOrEmpty(ImagePath))
                {
                    return ReStr;
                }
                string ffmpeg = @"bin\ffmpeg.exe";
                ffmpeg = AppDomain.CurrentDomain.BaseDirectory + ffmpeg;
                ProcessStartInfo startInfo = new ProcessStartInfo(ffmpeg);  // 使用进程打开
                startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;  // 隐藏样式窗口
                ReStr = GetImageNameFunc(ImagePath);
                startInfo.Arguments = " -i " + ImagePath + " -r 1 -ss " + GetTimeStrFunc(ScrrenTime) + " -t 1 -s 1725x975 -f image2 " + AppDomain.CurrentDomain.BaseDirectory + ReStr;
                Process TPHandel = new Process();
                TPHandel.StartInfo = startInfo;
                TPHandel.Start();
                TPHandel.WaitForExit(3000);

            }
            catch (Exception err)
            {
                LogHelper.Exception(typeof(LGRES_Resources), "GetScrrenImageFunc", err);
                LogHelper.Exception(typeof(LGRES_Resources), "打印视频路径:"+ ImagePath, new Exception(""));
            }
            return ReStr;
        }
        
        /// <summary>
        /// iis回收清除上次的进程
        /// </summary>
        public static void FfmpegProcessKill()
        {
            foreach(Process TProcess in Process.GetProcesses())
            {
                if (TProcess.ProcessName == "ffmpeg") {
                    TProcess.Kill();
                }
            }
        }

        /// <summary>
        /// 获取图片名称
        /// </summary>
        /// <param name="ImagePath">资源路径</param>
        /// <returns></returns>
        private static string GetImageNameFunc(string ImagePath)
        {
            // 获取指定的名称
            if (ImagePath.IndexOf("/") > -1)
            {
                string[] ImagePathArr = ImagePath.Split('/');
                ImagePath = ImagePathArr[ImagePathArr.Length - 1];
            }
            if (ImagePath.IndexOf(".") > -1)
            {
                string[] ImagePathArr = ImagePath.Split('.');
                ImagePath = ImagePathArr[0];
            }
            // 设置指定的目录
            string FilePath = @"FrameImageUrl";
            if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + FilePath))
            {
                Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + FilePath);
            }
            return FilePath + @"\" + ImagePath + ".jpeg";
        }

        private static Random TRandom = new Random();

        /// <summary>
        /// 转换截取时间
        /// </summary>
        /// <param name="ScrrenTime"></param>
        /// <returns></returns>
        private static string GetTimeStrFunc(int ScrrenTime)
        {
            if (ScrrenTime>=5000)
            {
                ScrrenTime = TRandom.Next(5000, ScrrenTime);
            }
            else
            {
                ScrrenTime = TRandom.Next(0, ScrrenTime);
            }
            string ReStr = string.Empty;
            // 计算时
            if (ScrrenTime >= 1000 * 60 * 60)
            {
                int THours = Convert.ToInt32(Math.Floor(Convert.ToDouble(ScrrenTime) / (1000 * 60 * 60)));
                if (THours > 9)
                {
                    ReStr += THours.ToString() + ":";
                }
                else
                {
                    ReStr += "0" + THours.ToString() + ":";
                }
                ScrrenTime -= THours * 1000 * 60 * 60;
            }
            else
            {
                ReStr += "00:";
            }
            // 计算分
            if (ScrrenTime >= 1000 * 60)
            {
                int THours = Convert.ToInt32(Math.Floor(Convert.ToDouble(ScrrenTime) / (1000 * 60)));
                if (THours > 9)
                {
                    ReStr += THours.ToString() + ":";
                }
                else
                {
                    ReStr += "0" + THours.ToString() + ":";
                }
                ScrrenTime -= THours * 1000 * 60;
            }
            else
            {
                ReStr += "00:";
            }
            // 计算秒
            if (ScrrenTime >= 1000)
            {
                int THours = Convert.ToInt32(Math.Floor(Convert.ToDouble(ScrrenTime) / (1000)));
                if (THours > 9)
                {
                    ReStr += THours.ToString() + ".";
                }
                else
                {
                    ReStr += "0" + THours.ToString() + ".";
                }
                ScrrenTime -= THours * 1000;
            }
            else
            {
                ReStr += "00.";
            }
            // 计算毫秒
            ReStr += ScrrenTime;
            return ReStr;
        }

 

 

 

 

 

待续、、

标签:ffmpeg,ScrrenTime,ImagePath,Ffmpeg,ReStr,THours,关于,使用,string
来源: https://www.cnblogs.com/namejr/p/14737720.html