亲宝软件园·资讯

展开

C#实现为视频添加水印

Csharp小记 人气:0

文章描述

以下主要还是使用到了ffmpeg命令,分别实现了给视频添加图片水印以及文字水印。

开发环境

.NET Framework版本:4.5

开发工具

Visual Studio 2013

实现代码

 public static void Run(string cmd)
        {
            try
            {
                string ffmpeg = AppDomain.CurrentDomain.BaseDirectory + "ffmpeg.exe";
                ProcessStartInfo startInfo = new ProcessStartInfo(ffmpeg);
                startInfo.UseShellExecute = false;
                startInfo.CreateNoWindow = true;
                startInfo.WindowStyle = ProcessWindowStyle.Hidden;
                startInfo.Arguments = cmd;
                Process process = Process.Start(startInfo);
                process.WaitForExit(3000);
                process.Kill();
            }
            catch { }
        }
 /// <summary>
        /// 按时间获取某帧图片
        /// </summary>
        /// <param name="videoPath">视频路径</param>
        /// <param name="outPath">输出路径</param>
        /// <param name="frameTime">时间(格式:00:00:01)</param>
        public static void GetFrame(string videoPath, string outPath, string frameTime)
        {
            Run(string.Format("-ss 00:00:01 -i {1} {2}", frameTime, videoPath, outPath));
        }

        /// <summary>
        /// 批量添加图片水印
        /// </summary>
        /// <param name="videoPath"></param>
        /// <param name="outPath"></param>
        /// <param name="listImg"></param>
        public static void AddImageMark(string videoPath, string outPath, List<ImgMark> listImg)
        {
            string imgs = "", postions = "";
            foreach (ImgMark mark in listImg)
            {
                imgs += " -i " + mark.ImgPath;
                postions += "overlay=" + mark.Postion.X + ":" + mark.Postion.Y+",";
            }
            postions = postions.Remove(postions.Length - 1);
            Run(string.Format("-i {0}{1} -filter_complex \"{2}\" {3}", videoPath, imgs, postions, outPath));
        }

        /// <summary>
        /// 添加文字水印
        /// </summary>
        /// <param name="videoPath">视频路径</param>
        /// <param name="outPath">输出路径</param>
        /// <param name="textMark">水印属性</param>
        public static void AddTextMark(string videoPath, string outPath, TextMark textMark)
        {
            Run(string.Format(" -i {0}  -vf \"drawtext=fontfile={1}: text='{2}':x={3}:y={4}:fontsize={5}:fontcolor={6}\" {7}", videoPath, textMark.FontFile, textMark.Text, textMark.X, textMark.Y, textMark.FontSize, textMark.FontColor.Name.ToLower(), outPath));
            //@"%{localtime\:%Y\-%m\-%d %H-%M-%S}"
        }
 private void btn_select_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "视频|*.mp4;*.avi";
            ofd.Title = "请选择视频文件";
            ofd.InitialDirectory = Application.StartupPath;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                axWindowsMediaPlayer1.URL = ofd.FileName;
            }
        }

        private void btn_text_Click(object sender, EventArgs e)
        {
            if (!File.Exists(axWindowsMediaPlayer1.URL))
            {
                MessageBox.Show("未选择视频");
                return;
            }
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "视频|*.mp4";
            sfd.AddExtension = true;
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                TextMark mark = new TextMark
                {
                    Text = "这里是水印",
                    FontColor = Color.Red,
                    FontFile = "simsun.ttc",
                    FontSize = 100,
                    X = 80,
                    Y = 60
                };
                FFmpegUtil.AddTextMark(axWindowsMediaPlayer1.URL, sfd.FileName, mark);
                axWindowsMediaPlayer2.URL = sfd.FileName;
            }
        }

        private void btn_img_Click(object sender, EventArgs e)
        {
            if (!File.Exists(axWindowsMediaPlayer1.URL))
            {
                MessageBox.Show("未选择视频");
                return;
            }
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "视频|*.mp4";
            sfd.AddExtension = true;
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                FFmpegUtil.AddImageMark(axWindowsMediaPlayer1.URL, sfd.FileName, new List<ImgMark>{
                    new ImgMark {
                    ImgPath=@"C:\Users\Zero\Desktop\a\\1.png",                   Postion=new Point(60,60)},
                    new ImgMark {ImgPath=@"C:\Users\Zero\Desktop\a\\1.png",             Postion=new Point(60,200)}});
                axWindowsMediaPlayer2.URL = sfd.FileName;
            }
        }

实现效果

代码解析:着重介绍下添加文字水印的,由于很多时候我们需要添加的是中文,所以需要将字体包先放入到ffmepg的同级目录,然后指定该字体。

加载全部内容

相关教程
猜你喜欢
用户评论