ffmpeg合并视频
命令行:
ffmpeg -f concat -i filelist.txt -c copy out\new.mp4
filelis.txt与ffmpeg.exe同一目录, 内容如下:
file 'F:\Download\【日剧_11集全】\1.EP01-1_Av438702220_P1_.mp4' file 'F:\Download\【日剧_11集全】\2.EP01-2_Av438702220_P2_.mp4' file 'F:\Download\【日剧_11集全】\3.EP01-3_Av438702220_P3_.mp4' file 'F:\Download\【日剧_11集全】\4.EP01-4_Av438702220_P4_.mp4' file 'F:\Download\【日剧_11集全】\5.EP01-5_Av438702220_P5_.mp4' file 'F:\Download\【日剧_11集全】\6.EP01-6_Av438702220_P6_.mp4'
如果想创建的视频在out目录下, out目录要自己先创建, ffmpeg不能创建目录. 上面也可以用new.mp4, 拼合的新视频在当前目录.
filelist.txt中的文件名可以用相对路径, 名字或路径中不能包含,(), 即逗号,左括号, 右括号, 不然提示警告拼接不了.
ffmpeg最后输出的路径只能用相对路径, 已试过用绝对路径的, 执行不了.
使用C#代码调用ffmpeg, 例子:
/// <summary>
/// 拼接视频
/// </summary>
/// <param name="files">有序的文件列表</param>
/// <param name="destName">拼接后的名字如out\abc.mp4</param>
private void ConcatVideo(List<string> files, string destName)
{
string dir = Path.GetDirectoryName(files[0]);
if (!Directory.Exists(dir + "\\out"))
{
Directory.CreateDirectory(dir + "\\out");
}
if (!Directory.Exists(dir + "\\源"))
{
Directory.CreateDirectory(dir + "\\源");
}
try
{
StringBuilder sb = new StringBuilder();
foreach (string file in files)
{
sb.Append($"file '{Path.GetFileName(file)}'\r\n");
}
File.WriteAllText(dir + "\\filelist.txt", sb.ToString());
string paras = $"-f concat -i filelist.txt -c copy {destName}";
string destExe = dir + "\\ffmpeg.exe";
if(!File.Exists(destExe))
{
string exePath = AppDomain.CurrentDomain.BaseDirectory + "ffmpeg.exe";
File.Copy(exePath, destExe, true);
}
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.WorkingDirectory = dir;
processStartInfo.FileName = destExe;
processStartInfo.WindowStyle = ProcessWindowStyle.Normal;
processStartInfo.Arguments = paras;
processStartInfo.UseShellExecute = true;
Process? p = Process.Start(processStartInfo);
p?.WaitForExit();
//处理完整的源文件切到新目录
foreach(string file in files)
{
FileInfo fileInfo= new FileInfo(file);
fileInfo.MoveTo(dir + "\\源\\" + Path.GetFileName(file), true);
}
}
catch(Exception ex)
{
WriteLog(ex.Message);
}
}
浙公网安备 33010602011771号