为了更加方便合成视频,可以使用FFmpeg
它的合成需要:
1 文件夹下有FFmpeg
2 要合成的视频,没有人会合成一个视频。
3 一个文本文件:格式为file ‘<videoPath>’ //这里有单引号
我们现在只要在cmd中调用FFmpeg:输入ffmpeg -f concat -i <文本文件> -c copy <导出后的路径>
借助代码可以更加轻松的合成:写入
Public Function CraftVideo(ffmpeg As String, out As String)
Dim open As New OpenFileDialog
Dim s As String
open.Filter = "视频文件|*.mp4"
open.Title = "选择多个视频"
open.Multiselect = True
open.ShowDialog()
For i = 0 To open.FileNames.Count - 1
s &= "file '" & open.FileNames(i) & "'" & vbCrLf
Next
IO.File.WriteAllText(Application.StartupPath & "\a.txt", s)
Process.Start(ffmpeg, "-f concat -i " & Application.StartupPath & "\a.txt" & " -c copy " & out)
End Function
参数:ffmpeg 是FFmpeg的绝对路径,out是视频输出的绝对路径
最简单的可以使用Process.Start的方式调用,当然也可以写复杂一些:
我们写一个重载:
Public Overridable Function CraftVideo(ffmpeg As String, out As String, CreateNoWindow As Boolean)
Dim open As New OpenFileDialog
Dim s As String
open.Filter = "视频文件|*.mp4"
open.Title = "选择多个视频"
open.Multiselect = True
open.ShowDialog()
For i = 0 To open.FileNames.Count - 1
s &= "file '" & open.FileNames(i) & "'" & vbCrLf
Next
IO.File.WriteAllText(Application.StartupPath & "\a.txt", s)
Dim p As New Process
p.StartInfo.FileName = ffmpeg
p.StartInfo.Arguments = "-f concat -i " & Application.StartupPath & "\a.txt" & " -c copy " & out
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardError = True
p.StartInfo.CreateNoWindow = CreateNoWindow
p.Start()
p.WaitForExit()
p.StandardError.ReadToEnd()
p.Close()
p.Dispose()
'Process.Start(ffmpeg, "-f concat -i " & Application.StartupPath & "\a.txt" & " -c copy " & out)
End Function
主窗体:
Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.Click
GDIvid.CraftVideo(Application.StartupPath & "\FFmpeg.exe", "C:\Users\Administrator\Desktop\example.mp4", True)
'GDIvid.CraftVideo(Application.StartupPath & "\FFmpeg.exe", "C:\Users\Administrator\Desktop\example.mp4")
End Sub
第一段是调用的重载函数,第二段是调用的简单的函数,参数多一个CreateNoWindow=Boolean,不要调用错误