FFmpeg给视频添加图片,文字(vb.net,类库——11)

20 篇文章 1 订阅
17 篇文章 1 订阅

给视频添加文字,可以使用添加文字的方法,但是想添加中文字,那只能使用微软雅黑了

那我们想:文字可以被印到图片上,然后图片可以被轻而易举的添加到视频中

借助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 不需要我讲了……(其实这很容易理解)

 效果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用FFmpeg视频图片水印,需要先安装FFmpeg并设置环境变量。然后,可以使用Java调用FFmpeg的命令行工具来实现添加图片水印的功能。以下是一个示例代码: ```java public class VideoUtils { /** * 给视频添加图片水印 * @param videoPath 视频文件路径 * @param imagePath 水印图片路径 * @param outputPath 输出文件路径 * @throws Exception */ public static void addImageWatermark(String videoPath, String imagePath, String outputPath) throws Exception { // 构造FFmpeg命令 List<String> command = new ArrayList<>(); command.add("ffmpeg"); command.add("-i"); command.add(videoPath); command.add("-i"); command.add(imagePath); command.add("-filter_complex"); command.add("[0:v][1:v]overlay=10:10"); command.add("-codec:a"); command.add("copy"); command.add(outputPath); // 执行FFmpeg命令 ProcessBuilder builder = new ProcessBuilder(command); Process process = builder.start(); process.waitFor(); } } ``` 在上面的代码中,我们使用了FFmpeg的overlay过滤器来实现将水印图片叠加在视频上的功能。其中,`[0:v]`表示第一个输入文件的视频流,`[1:v]`表示第二个输入文件的视频流,`overlay=10:10`表示在视频的左上角叠加水印图片,并且水印图片的左上角坐标为(10, 10)。如果要调整水印图片的位置,可以修改`overlay`参数的值。 使用时,可以调用`VideoUtils.addImageWatermark()`方法来给视频添加水印,例如: ```java try { VideoUtils.addImageWatermark("input.mp4", "watermark.png", "output.mp4"); } catch (Exception e) { e.printStackTrace(); } ``` 其中,`input.mp4`是要添加水印的视频文件,`watermark.png`是水印图片文件,`output.mp4`是输出文件路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值