C#使用ffmpeg对画面进行旋转

命令参考自:https://blog.csdn.net/lhtzbj12/article/details/53538857

 

本地ffmpeg文件下载:https://ffmpeg.zeranoe.com/builds/win64/static/

解压后将ffmpeg.exe拷贝到文件夹下,在cmd命令提示符中定位到所在目录,执行以下语句及即可实现功能

-vf transpose 旋转视频
0:逆时针旋转90°然后垂直翻转
1:顺时针旋转90°
2:逆时针旋转90°
3:顺时针旋转90°然后水平翻转
hflip 水平翻转
vflip 垂直翻转

--视频顺时针旋转90度
ffmpeg -y -i "D:\VideoText\video.mp4" -vf transpose=1 -acodec copy "D:\VideoText\videoRight90.mp4"

--视频顺时逆旋转90度
ffmpeg -y -i "D:\VideoText\video.mp4" -vf transpose=2 -acodec copy "D:\VideoText\videoLeft90.mp4"

--顺时针旋转90°然后水平翻转
ffmpeg -y -i "D:\VideoText\video.mp4" -vf transpose=3 -acodec copy "D:\VideoText\video1.mp4"

--逆时针旋转90°然后垂直翻转
ffmpeg -y -i "D:\VideoText\video.mp4" -vf transpose=0 -acodec copy "D:\VideoText\video2.mp4"

--垂直翻转
ffmpeg -y -i "D:\VideoText\video.mp4" -vf vflip -acodec copy "D:\VideoText\videovflip.mp4"

--水平翻转
ffmpeg -y -i "D:\VideoText\video.mp4" -vf hflip -acodec copy "D:\VideoText\videohflip.mp4"

 

以下为C#代码实现:

//ffmpeg执行文件的路径
        private static string ffmpeg = System.AppDomain.CurrentDomain.BaseDirectory + "ffmpeg\\ffmpeg.exe";

#region 视频旋转
        /// <summary>
        /// 视频旋转
        /// </summary>
        /// <param name="videoFilePath">视频绝对路径</param>
        /// <param name="dealVideFilePath">视频旋转后保存路径</param>
        /// <param name="flag">1=顺时针旋转90度  2=逆时针旋转90度</param>
        /// <returns>true  成功  false  失败</returns>
        public static bool VideoRotate(string videoFilePath,string dealVideFilePath,string flag)
        {
            //ffmpeg -i success.mp4 -metadata:s:v rotate="90" -codec copy output_success.mp4
            string output;
            string error;
            //执行命令
            ExecuteCommand("\"" + ffmpeg + "\"" + " -y -i " + "\"" + videoFilePath + "\"" + " -vf transpose=" + flag + " -acodec copy " + "\"" + dealVideFilePath + "\"", out output, out error);
            if (File.Exists(dealVideFilePath))
                return true;
            else
                return false;
        }
        #endregion


#region 让ffmpeg执行一条命令
        /// <summary>
        /// 让ffmpeg执行一条command命令
        /// </summary>
        /// <param name="command">需要执行的Command</param>
        /// <param name="output">输出</param>
        /// <param name="error">错误</param>
        private 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 ex)
            {
                output = null;
                error = null;
            }
        }
        #endregion

 

C#使用ffmpeg对画面进行旋转

上一篇:rest之ftl页面中文到后台代码乱码解决


下一篇:Flink学习笔记——SocketWindowWordCount