C#實(shí)現(xiàn)為視頻添加水印
文章描述
以下主要還是使用到了ffmpeg命令,分別實(shí)現(xiàn)了給視頻添加圖片水印以及文字水印。
開發(fā)環(huán)境
.NET Framework版本:4.5
開發(fā)工具
Visual Studio 2013
實(shí)現(xiàn)代碼
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> /// 按時(shí)間獲取某幀圖片 /// </summary> /// <param name="videoPath">視頻路徑</param> /// <param name="outPath">輸出路徑</param> /// <param name="frameTime">時(shí)間(格式: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 = "請(qǐng)選擇視頻文件"; 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; } }
實(shí)現(xiàn)效果
代碼解析:著重介紹下添加文字水印的,由于很多時(shí)候我們需要添加的是中文,所以需要將字體包先放入到ffmepg的同級(jí)目錄,然后指定該字體。
到此這篇關(guān)于C#實(shí)現(xiàn)為視頻添加水印的文章就介紹到這了,更多相關(guān)C#視頻添加水印內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)IP代理池調(diào)度的示例代碼
這篇文章主要為大家介紹了C#實(shí)現(xiàn)IP代理池調(diào)度的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的參考與學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2023-07-07C#模擬MSN窗體抖動(dòng)的實(shí)現(xiàn)代碼
這篇文章主要介紹了C#模擬MSN窗體抖動(dòng)的實(shí)現(xiàn)代碼,非常實(shí)用的一個(gè)功能,需要的朋友可以參考下2014-08-08C#創(chuàng)建SQLite控制臺(tái)應(yīng)用程序詳解
這篇文章主要為大家詳細(xì)介紹了C#創(chuàng)建SQLite控制臺(tái)應(yīng)用程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07