Project background
Because the company needs to do some operations on audio and video, such as synthesizing the pronunciation and background video of system users, synthesizing multiple audio and video, and inserting a segment of customer pronunciation in the number of seconds of the video according to the corresponding rules in the specified source background audio. This article mainly explains how to use C# Process to call ffmpeg Exe for video merging, audio merging, audio and video merging into video these simple audio and video operations, and some complex audio and video operations. Later, I have time to slowly make up.
Introduction to FFmpeg
FFmpeg is an open source computer program that can be used to record, convert digital audio and video, and convert them into streams. Use LGPL or GPL license. It provides a complete solution for recording, converting and streaming audio and video. It contains a very advanced audio / video codec library libavcodec. In order to ensure high portability and codec quality, many codes in libavcodec are developed from scratch.
FFmpeg is developed on Linux platform, but it can also be compiled and run in other operating system environments, including Windows, Mac and other platforms. This project was first initiated by fabric bellard and was mainly maintained by Michael Niedermayer from 2004 to 2015. Many FFmpeg developers are from the MPlayer project, and FFmpeg is currently placed on the server of the MPlayer project group. The name of the project comes from the MPEG video coding standard, and the front "FF" stands for "Fast Forward". The FFmpeg encoding library can be accelerated using GPU.
FFmpeg related tutorials
At the beginning, you should first understand what FFmpeg is, what common commands and practical functions it has.
- FFmpeg official website document
- FFmpeg most complete tutorial
- Introduction to FFmpeg video processing
- FFMPEG command entry to improve, an article is enough
Blog sample source code
https://github.com/YSGStudyHards/FFmpegAudioAndVideoMerge👉
Download ffmpeg Exe installation package
First, Download ffmpeg Exe is placed in the directory folder specified by you to facilitate C# process calling.
ffmpeg.exe installation package: https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-essentials.zip👉 (74MB)
C# process calls FFmpeg to operate audio and video
namespace FFmpegAudioAndVideoMerge { class Program { static void Main(string[] args) { var physicalPath = "E:\\FFmpegAudioAndVideoMerge\\FFmpegAudioAndVideoMerge\\files\\"; //Video merge VideoCombine(physicalPath + "video1.mp4", physicalPath + "video2.mp4", physicalPath + "merageVideoyy.mp4"); //Audio merge var audioMergeList = new List<string>(); audioMergeList.Add(physicalPath + "music1.mp3"); audioMergeList.Add(physicalPath + "music2.mp3"); audioMergeList.Add(physicalPath + "music3.mp3"); AudioMerge(physicalPath, audioMergeList); //Combine audio and video into video AudioAndVideoMerge(physicalPath); } #region Video merge /// <summary> /// Video merge /// </summary> /// <param name="video1">Merge Video 1</param> /// <param name="video2">Merge Video 2</param> /// <param name="saveFilePath">Save file name</param> /// <returns></returns> public static void VideoCombine(string video1, string video2, string saveFilePath) { string strTmp1 = video1 + ".ts"; string strTmp2 = video2 + ".ts"; string strCmd1 = " -i " + video1 + " -c copy -bsf:v h264_mp4toannexb -f mpegts " + strTmp1 + " -y "; string strCmd2 = " -i " + video2 + " -c copy -bsf:v h264_mp4toannexb -f mpegts " + strTmp2 + " -y "; string videoMerge = " -i \"concat:" + strTmp1 + "|" + strTmp2 + "\" -c copy -bsf:a aac_adtstoasc -movflags +faststart " + saveFilePath + " -y "; //1,Convert file types. Since not all types of video files support direct merging, you need to convert the format first CommandManager(strCmd1); CommandManager(strCmd2); //2,Video merge CommandManager(videoMerge); } #endregion #region Audio merge /// <summary> /// Audio merge /// </summary> public static void AudioMerge(string physicalPath, List<string> mergeFile) { //Mix multiple audio into one audio file output http://www.ffmpeg.org/ffmpeg-all.html#amix //ffmpeg -i INPUT1 -i INPUT2 -i INPUT3 -filter_complex amix=inputs=3:duration=first:dropout_transition=3 OUTPUT //Merge two audio //ffmpeg -i input1.mp3 -i input2.mp3 -filter_complex amerge -ac 2 - c:a libmp3lame -q:a 4 output.mp3 //Get audio from video //ffmpeg -i input.mp4 -vn -y -acodec copy output.m4a //Remove audio from video //ffmpeg -i input.mp4 -an output.mp4 // https://www.cnblogs.com/simadi/p/10649345.html // ffmpeg -i "concat:123.mp3|124.mp3" -acodec copy output.mp3 // Explanation:-i Represents the input parameter // contact: 123.mp3 | 124.mp3 Represents audio files that need to be connected together -acodec copy output.mp3 Recode and copy to new file string mergeCommandStr = $"-i \"concat:{string.Join("|", mergeFile.ToArray())}\" -acodec copy {physicalPath}AudioMerge.mp3 -y"; CommandManager(mergeCommandStr); } #endregion #region Combine audio and video into video /// <summary> /// Combine audio and video into video /// </summary> /// <param name="physicalPath">Physical path</param> public static void AudioAndVideoMerge(string physicalPath) { //1,There is no audio in the video file. //ffmpeg -i video.mp4 -i audio.wav -c:v copy -c:a aac -strict experimental output.mp4 //string mergeCommandStr = $"-i {physicalPath}video2.mp4 -i {physicalPath}music1.mp3 -c:v copy -c:a aac -strict experimental {physicalPath}output.mp4 -y"; //video.mp4,audio.wav The video and audio to be merged, output.mp4 Is the audio and video file output after merging. //2,The following command is audio Audio replacement video Audio in ffmpeg -i video.mp4 -i audio.wav -c:v copy -c:a aac -strict experimental -map 0:v:0 -map 1:a: 0 output.mp4 string mergeCommandStr = $"-i {physicalPath}video3.mp4 -i {physicalPath}AudioMerge.mp3 -c:v copy -c:a aac -strict experimental -map 0:v:0 -map 1:a:0 {physicalPath}AudioAndVideoMerge.mp4 -y"; //3,c++Audio video merge(When there is no audio in the video file) //"ffmpeg -i /tmp/mergeMp3/392118469203595327/392118469203595327.aac -i /tmp/mergeMp3/392118469203595327/bg.mp4 -c copy -bsf:a aac_adtstoasc /tmp/mergeMp3/392118469203595327/392118469203595327.mp4 -y" //string mergeCommandStr3 = $"-i {physicalPath}video5.mp4 -i {physicalPath}AudioMerge.mp3 -c copy -bsf:a aac_adtstoasc {physicalPath}AudioAndVideoMerge1.mp4 -y"; CommandManager(mergeCommandStr); } #endregion /// <summary> /// implement /// C# Process call https://docs.microsoft.com/zh-cn/dotnet/api/system.diagnostics.process?view=net-5.0 /// </summary> /// <param name="commandStr">Execute command</param> public static void CommandManager(string commandStr) { try { using (Process process = new Process()) { process.StartInfo.FileName = "D:\\FFmpeg\\bin\\ffmpeg.exe";//Name of program to execute(Property to get or set the application or document to launch. FileName Property does not need to represent an executable. It can be any file type whose extension is already associated with an application installed on the system.) process.StartInfo.Arguments = " " + commandStr;//Command line arguments passed when starting the process process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardInput = false;//May accept input from the caller process.StartInfo.RedirectStandardOutput = false;//Get the output information from the caller process.StartInfo.RedirectStandardError = false;//Redirect standard error output process.StartInfo.CreateNoWindow = false;//Do not display program window process.Start();//Start program process.WaitForExit();//Wait for the program to finish executing and exit the process(Avoid that the process occupies the file or the composite file has not been generated)* } } catch (Exception e) { Console.WriteLine(e.Message); } } } }