给视频添加文字,可以使用添加文字的方法,但是想添加中文字,那只能使用微软雅黑了
那我们想:文字可以被印到图片上,然后图片可以被轻而易举的添加到视频中
借助GDI+完成这一转换
Public Function AddText(s As String, input As String, location As Point, font As Font, c As Color, out As String)
Dim pic = New PictureBox
Dim g As Graphics = pic.CreateGraphics()
Dim sf As New StringFormat
g.DrawString(s, font, New SolidBrush(c), 0, 0, sf)
Dim stringSize As SizeF = g.MeasureString(s, font, 1000, sf)
Dim bmp As New Bitmap(CInt(stringSize.Width), CInt(stringSize.Height))
Dim gb As Graphics
gb = Graphics.FromImage(bmp)
gb.DrawString(s, font,New SolidBrush(c),location)
bmp.Save(Application.StartupPath & "\tmp.png", Imaging.ImageFormat.Png)
SharedP(ffmpeg, "-i " & input & " -i " & Application.StartupPath & "\tmp.png -filter_complex " & Chr(34) & "overlay=" & location.X & ":" & location.Y & Chr(34) & " -y " & out, True)
Return vbNull
End Function
Public Shared Function SharedP(s As String, Arguments As String, nowindow As Boolean)
Dim p As New Process
p.StartInfo.FileName = s
p.StartInfo.Arguments = Arguments
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardError = True
p.StartInfo.CreateNoWindow = nowindow
p.Start()
p.StandardError.ReadToEnd()
Return p.StandardError.ReadToEnd()
p.WaitForExit()
p.Kill()
p.Close()
p.Dispose()
End Function
调用:
GDIvid.AddText("HOOOOOOO", "C:\Users\Administrator\Desktop\feca881102ec9c42603125354dbe7d19.mp4", New Point(0, 0), New Font("宋体", 50, FontStyle.Bold), Color.Blue, "C:\Users\Administrator\Desktop\test.mp4")
讲解:(s As String, input As String, location As Point, font As Font, c As Color, out As String)
s:是添加的文字
input是视频的输入路径
location是文字在视频中的位置,不要和代码中“g.DrawString(s, font, New SolidBrush(c), 0, 0, sf)”搞混
font:千万注意!参数要全(New Font("宋体", 50, FontStyle.Bold))
font也可以使用未安装的字体格式,具体装载请见vb.net大佬的文章:vb.net 教程 5-3 使用未安装的字体
color 和 out 不需要我讲了……(其实这很容易理解)
效果: