寻寻觅觅,觅觅寻寻,刚刚查了一堆资料,小编的头都大了,如何给视频添加水印呢?小编最初想的是使用css添加,可后来查看图片后发现这种根本不可行,最终还是使用了C#实现来看教程
第一,下载ffmpeg.exe
文件资源:https://www.gyan.dev/ffmpeg/builds/
小编下载的是https://www.gyan.dev/ffmpeg/builds/ffmpeg-git-full.7z
怎么安装呢?
参考博客https://blog.csdn.net/chy466071353/article/details/54949221
代码实现
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
//string fileName = "E:\\BaiduNetdiskDownload\\下午06-图标字体回顾.web_recv.avi";
string imgFile = "E:\\BaiduNetdiskDownload\\true.png";
//string outputFile = "E:\\BaiduNetdiskDownload\\outputFile.avi";
//int a=WaterMark(fileName, imgFile, outputFile);
//Console.WriteLine(a);
//Console.ReadKey();
string outputFile = "E:\\BaiduNetdiskDownload\\b.mp4";
string fileName = "E:\\BaiduNetdiskDownload\\a.mp4";
//string mingling = "-i "+fileName+" -y -b 1024k -acodec copy -f mp4 "+ outputFile;
string mingling = "-i "+ fileName + " -i "+imgFile+" -filter_complex \"overlay=10:10\" "+outputFile;
string b = RunProcess(mingling);
Console.WriteLine(b);
Console.ReadKey();
}
public static int WaterMark(string fileName, string imgFile, string outputFile)
{
//取得ffmpeg.exe的路径,路径配置在Web.Config中,如:<add key="ffmpeg" value="E:\aspx1\ffmpeg.exe" />
fileName = "E:\\BaiduNetdiskDownload\\下午06-图标字体回顾.web_recv.avi";
imgFile = "E:\\BaiduNetdiskDownload\\true.png";
outputFile = "E:\\BaiduNetdiskDownload\\outputFile.avi";
string ffmpeg = "C:\\Users\\Administrator\\Downloads\\ffmpeg-2021-04-25-git-d98884be41-full_build\\ffmpeg-2021-04-25-git-d98884be41-full_build\\bin\\ffmpeg.exe";
if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(fileName)))
{
return 0;
}
//建立ffmpeg进程
System.Diagnostics.ProcessStartInfo WaterMarkstartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
//后台运行
WaterMarkstartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
//运行参数
string config = " -i " + fileName + " -vf \"movie=" + imgFile + " [watermark]; [in][watermark] overlay=0:0 [out]\" " + outputFile;
WaterMarkstartInfo.Arguments = config;
try
{
//开始加水印
System.Diagnostics.Process.Start(WaterMarkstartInfo);
}
catch
{
return 0;
}
return 1;
}
//添加水印,fileName视频地址,imgFile水印图片地址,outputFile输出地址
/// <summary>
/// 视频处理器ffmpeg.exe的位置
/// </summary>
public string FFmpegPath { get; set; }
/// <summary>
/// 调用ffmpeg.exe 执行命令
/// </summary>
/// <param name="Parameters">命令参数</param>
/// <returns>返回执行结果</returns>
private static string RunProcess(string Parameters)
{
string FFmpegPath = "C:\\Users\\Administrator\\Downloads\\ffmpeg-2021-04-25-git-d98884be41-full_build\\ffmpeg-2021-04-25-git-d98884be41-full_build\\bin\\ffmpeg.exe";
//创建一个ProcessStartInfo对象 并设置相关属性
var oInfo = new ProcessStartInfo(FFmpegPath, Parameters);
oInfo.UseShellExecute = false;
oInfo.CreateNoWindow = true;
oInfo.RedirectStandardOutput = true;
oInfo.RedirectStandardError = true;
oInfo.RedirectStandardInput = true;
//创建一个字符串和StreamReader 用来获取处理结果
string output = null;
StreamReader srOutput = null;
try
{
//调用ffmpeg开始处理命令
var proc = Process.Start(oInfo);
proc.WaitForExit();
//获取输出流
srOutput = proc.StandardError;
//转换成string
output = srOutput.ReadToEnd();
//关闭处理程序
proc.Close();
}
catch (Exception)
{
output = string.Empty;
}
finally
{
//释放资源
if (srOutput != null)
{
srOutput.Close();
srOutput.Dispose();
}
}
return output;
}
}
}
效果图
原视频
水印图片
效果图
参考博客https://www.cnblogs.com/jayshsoft/p/csharp_ffmpeg.html
如何给视频添加文字呢?小编还未实现,先参考一下,下次小编试试能否实现,给视频添加文字参考地址https://www.cnblogs.com/jhlong/p/5669695.html