Project background
Because the company needs to do some operations on audio and video, such as synthesising the pronunciation and background video of system users, and synthesising multiple audio and videos, and inserting a customer pronunciation in the specified source background audio in a number of seconds of the video according to the corresponding rules. This article mainly explains the simple audio and video operations of using C# process calls to perform video merging, audio merging, audio and video merging into video, and some complex audio and video operations are also available for later time to make up for it.
Introduction to FFmpeg
Source of Baidu Encyclopedia 👉
FFmpeg is an open source computer program that can be used to record, convert digital audio, video, and convert them into streams. Use an 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 under the 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 Fabrice Bellard and was mainly maintained by Michael Niedermayer from 2004 to 2015. Many FFmpeg developers come from MPlayer projects, and FFmpeg is currently placed on the server of the MPlayer project team. The name of the project comes from the MPEG video encoding standard, and the previous "FF" stands for "Fast Forward". The FFmpeg encoding library can use GPU acceleration.
FFmpeg related tutorials
At the beginning, you must first understand what FFmpeg is, what are the commonly used commands and practical functions.
- FFmpeg official website document
- The most complete tutorial on FFmpeg
- FFmpeg video processing tutorial
- Getting started with FFMPEG commands, one article is enough
Blog sample source code
/YSGStudyHards/FFmpegAudioAndVideoMerge
Download the installation package
First, put the downloaded ones in the directory folder you specified to facilitate the call of the C# process.
Installation package:/ffmpeg/builds/(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>(); (physicalPath + "music1.mp3"); (physicalPath + "music2.mp3"); (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 audios into one audio file output /#amix //ffmpeg -i INPUT1 -i INPUT2 -i INPUT3 -filter_complex amix=inputs=3:duration=first:dropout_transition=3 OUTPUT //Merge two audios //ffmpeg -i input1.mp3 -i input2.mp3 -filter_complex amerge -ac 2 - c:a libmp3lame -q:a 4 output.mp3 //Get audio in video //ffmpeg -i input.mp4 -vn -y -acodec copy output.m4a //Remove the audio from the video //ffmpeg -i input.mp4 -an output.mp4 // /simadi/p/ // ffmpeg -i "concat:123.mp3|124.mp3" -acodec copy output.mp3 // Explanation: -i represents input parameters // contact: 123.mp3 | 124.mp3 represents the audio file that needs to be connected to -acodec copy output.mp3 Recode and copy to a new file string mergeCommandStr = $"-i \"concat:{("|", ())}\" -acodec copy {physicalPath}AudioMerge.mp3 -y"; CommandManager(mergeCommandStr); } #endregion #region audio and video merge into video /// <summary> /// Merge audio and video into video /// </summary> /// <param name="physicalPath">PhysicalPath</param> public static void AudioAndVideoMerge(string physicalPath) { //1. There is no audio in the video file. //ffmpeg -i video.mp4 -i -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 is the video and audio to be merged respectively, and output.mp4 is the audio and video file output after the merge. //2. The following command is to replace the audio in video with audio ffmpeg -i video.mp4 -i -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 and video merging (if there is no audio in the video file) //"ffmpeg -i /tmp/mergeMp3/392118469203595327/ -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 process call /zh-cn/dotnet/api/?view=net-5.0 /// </summary> /// <param name="commandStr">Execute the command</param> public static void CommandManager(string commandStr) { try { using (Process process = new Process()) { = "D:\\FFmpeg\\bin\\";//The name of the program to be executed (properties, get or set the application or document to be started. The FileName property does not need to represent an executable file. It can be any file type whose extension has been associated with the application installed on the system.) = " " + commandStr;//Command line parameters passed when starting the process = false; = false;//May accept input information from the calling program = false;//The output information is obtained by the calling program = false;//Redirection standard error output = false;//The program window is not displayed ();//Start the program ();//Waiting for the program to execute and exit the process (avoiding the process occupying files or the synthetic file has not been generated yet)* } } catch (Exception e) { (); } } } }
This is the article about the implementation example of C# calling FFmpeg to operate audio and video. For more related C# FFmpeg audio and video content, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!