C#实现为视频添加水印

文章描述

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

C#实现为视频添加水印_第1张图片

开发环境

.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 { }
        }
 /// 
        /// 按时间获取某帧图片
        /// 
        /// 视频路径
        /// 输出路径
        /// 时间(格式:00:00:01)
        public static void GetFrame(string videoPath, string outPath, string frameTime)
        {
            Run(string.Format("-ss 00:00:01 -i {1} {2}", frameTime, videoPath, outPath));
        }

        /// 
        /// 批量添加图片水印
        /// 
        /// 
        /// 
        /// 
        public static void AddImageMark(string videoPath, string outPath, List 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));
        }

        /// 
        /// 添加文字水印
        /// 
        /// 视频路径
        /// 输出路径
        /// 水印属性
        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{
                    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的同级目录,然后指定该字体。

到此这篇关于C#实现为视频添加水印的文章就介绍到这了,更多相关C#视频添加水印内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(C#实现为视频添加水印)