Guide: FFmpeg Add Audio to Video

Whether a professional or a beginner, merging audio with video is significant for a lasting impact on the audience. FFmpeg, an open-source software, is a robust command-line tool that perfectly adds audio to video. It offers flexibility by handling diverse audio and video formats.

So, this step-by-step guide enables you to seamlessly add desired audio to the video for a unique experience. It will give you the necessary skills to make your content look great. 

How to add audio to the video with FFmpeg?

FFmpeg is the leading open-source video and audio processing software that requires minimal experience. Its command-line interface allows you to incorporate audio seamlessly into your videos.  

Step 1- Install FFmpeg

Ensure that you have installed the compatible and latest version of FFmpeg and is working smoothly. Installing the software from the manufacturer’s website or using a package manager for Linux is essential. Here is the process for the safe installation of FFmpeg:

Installing FFmpeg on Windows

Before beginning the installation, ensure you have “Zip utility” to extract the zip files. Here is the process:

  1. Extract Downloaded File
  • Go to the “Manufacturer’s website” and click on the compatible FFmpeg zip version.
  • After downloading, extract the files and copy and paste the extracted files to the desired location.
  1. Store extracted files
  • Open the extracted folder and copy and paste all the contents to the root folder of your device.
  1. System Properties
image 437
  • Go to the search tab and type “Environment Properties.” Clicking it will open the “System Properties.”
  1. Create the Path
image 438
  • Click on the “System variables” and select “Path,” then click “Edit” to change the path.
image 439
  • Enter the “New” path of the files where you have stored.

Installing FFmpeg on Linux

It might already be installed on your device if you are using Linux. So, go to the command section, enter the “FFmpeg” command, and if an error message pops up, you have to install FFmpeg. 

  • If you are on Ubuntu, open the “Terminal” from applications and enter the command “sudo apt-get install FFmpeg.” 

Installing Ffmpeg on macOS

  • Download the latest and most compatible Ffmpeg version on your macOS.
  • Extract the downloaded file using “Archive Utility” and move the FFmpeg executable file from the downloads to the newly created folder on the “File” menu.
image 440
  • Open the file and press “OK” if an “unidentified developer” error appears on the screen.

Step 2- Open Terminal or Command Prompt

  1. On Windows
image 236
  • Go to the “search bar” of Windows from the bottom left, type “cmd,” and select the “Command Prompt.”
  • Type “ffmpeg,” and it will start working smoothly.
  1. On macOS
image 237
  • From the “Finder” window, click the “Application” and select “Utilities. It will open the terminal window.
image 441
  • Alternatively, click the “Launchpad icon” from the “Dock” and type “Terminal” in the search bar. Then select it to open the terminal window. 
  1. On Linux
  • The common and shortest way to open the terminal is using the “Ctrl + Alt + T” shortcut key.
  • Go to the “Applications” menu and click the “terminal” to open it.

Step 3- Navigate to the File Directories

After opening the terminal or command window, navigate to the video or audio file folder using the “cd” change directory command. On the terminal, type “cd” followed by the path of your downloaded file. 

Once you are in the correct directory, press enter to execute your command referencing the directory file name. For instance, use the command following the path as hereunder:

  • cd C:\Users\YourUsername\Downloads

 You can also drag and drop the files to the terminal, automatically adding the file path. You can also use the full path of both video and audio files in your directory.

Step 4- Combine Audio and Video

Type “ls” to view the audio and video files inside the Ffmpeg. Then use the following command to add an external audio track to the video with perfection:

ffmpeg -i video.mp4 -i audio.mp3 -c:v copy -c:a aac -strict experimental output.mp4

The components describe:

  • ‘–i video.mp4’ specifies the input video file.
  • ‘-i audio.mp3’ specifies the input audio file.
  • ‘-c:v copy’ instructs FFmpeg to copy the video stream from the input to the output file to avoid unnecessary re-encoding. The output will contain only one stream type of all stream types in the file.
  • ‘-c:a aac’ specifies the audio codec. It defines the output that should be encoded using AAC format, which FFmpeg widely supports.
  • “-strict experimental” is essential when using codecs, particularly during audio encoding.
  • ‘output.mp4’ defines the name of the output file to save the combined audio and video streams.

Note: ensure to replace the “video.mp4” or “audio.mp3” with the actual video or audio file names.

FFmpeg allows you to cut the file to whichever file or stream is the shortest. For instance, if the video is shorter, it will cut the audio. Follow the hereunder Ffmpeg command:

ffmpeg -i video.mp4 -i audio.mp3 -c copy -c:a aac -shortest output.mp4

Enter “yes” in the next prompted window, and Ffmpeg will adjust the length of the file or stream.

Step 5- Adding two audio Inputs in One video

If you have already added the audio stream, then Ffmpeg won’t automatically add another audio stream. So, it can be done by the following command action:

ffmpeg -i video.mp4 -i audio1.mp3 –audio2.mp3 -c copy –map 0:0 –map 0:1 –map 1:0 -shortest output.mp4

  • “-map 0:0” Select the video stream from the first input and Ffmpeg copy the video without re-encoding.
  • “map 0:1” specifies the Ffmpeg to select the audio stream from the second input file. 
  • “map 1:0” select the audio from the third input file. 

Use “-c:a aac” when there is a compatibility issue with the audio and video stream and re-encode. Before combining the audio tracks, consider adjusting the audio volume for the tracks.

Note: Ffmpeg automatically adjusts one audio file as “default,” you can change this status using the “-disposition default” option. 

 Step 6- Adjust Audio Volume 

FFmpeg provides several filters to adjust the volume, such as “volume,” “loudnorm,” and “compand.” The commonly used filter is the “Volume filter,” which lets you increase or decrease the volume or set it to a constant value. Use “-filter:a” or “-fa” option for the audio stream with the “volume” filter. 

ffmpeg -i video.mp4 -i audio.mp3 -filter:a “volume=1.5” -c:v copy -c:a aac output.mp4

Here, 1.5 indicates the volume is 150% of the current volume level. Values above 1.0 increase the volume level, while less than 1.0 decreases it. Also, 0.5 is half the input volume, and 2.0 is double the input volume level. 

You can use “decibels” to specify the increase or decrease in volume levels. For instance, use “volume=10dB” to increase the volume by 10 dB or “volume=-10dB” to decrease the volume by 10 dB. 

Note: Be cautious when increasing the volume levels because increasing will introduce audio clipping or distortions.

Step 7- Replace Audio in Video

If you want to replace the audio in the video with the new audio track, then follow the hereunder command without re-encoding:

ffmpeg -i video.mp4 -i audio.mp3 -map 0 -map 1:a -c:v copy -shortest output.mp4

  • ‘-map 0:v’ Selects the video stream from the first input file (video.mp4). It ensures that Ffmpeg copies the stream without re-encoding.
  • ‘-map 1:a’ selects the audio stream from the second input file (audio.mp3) and replaces the original audio in the video.
  • If the input format is compatible with the output, change “-c:v copy” to “-c copy” to stream the audio and video.
  • The “-shortest” will trim the video or make the output of the same duration according to the shortest input. 

The map option allows you to manually select between the audio or video tracks. However, if there are compatibility issues, re-encoding is essential for replacing the audio in video using specific audio codecs such as “-c:a aac.”

ffmpeg -i video.mp4 -i audio.mp3 -map 0:v -map 1:a -c:v copy -c:a aac output.mp4

So, you can easily apply the same methods to different audio or video formats to replace the audio in video without worrying about compatibility issues. 

Step 8- Synchronize Audio and Video

Synchronization is effective when audio is delayed by a few seconds, and you can adjust the audio positively or negatively. So, if the audio is out of sync, change the offset using the “-itsoffset” option.

ffmpeg -i video.mp4 -itsoffset 2 -i audio.mp3 -c:v copy -c:a aac output.mp4

  • “-itsoffset 2” instructs the Ffmpeg to add a delay of 2 seconds to the input stream. The “2” means that the second input should start after 2 seconds of the first input. You can use a negative offset value to advance the audio. 

If the offset is unknown, multiple adjustments will be required before the final output. In addition to syncing the audio and video, use the “-shortest” to trim or adjust the length of the video with the audio. 

FAQs 

How can I Install FFmpeg on Windows, macOS, and Linux?

Here is the safe approach to download and install the Ffmpeg on your specific operating systems:

  • Download compatible and latest version of FFmpeg from the official website. 

On Windows

  • Unzip the downloaded files, rename the folder to FFmpeg, and move it to the C drive.
  • Set the “environment path variables” for FFmpeg.

On Mac

  • Extract the file and move FFmpeg to the newly created folder.
  • Go to Security and Privacy >General and tap “Open anyway.”

On Linux

  • Use the package manager on Linux and open the terminal, then run the command “-sudo apt-get install ffmpeg.”

How can I Extract Audio from a Video Using FFmpeg?

FFmpeg offers a versatile way to extract the audio track from the video and use the hereunder process on your Terminal:

ffmpeg -i video.mp4 -vn -y output.mp3

  • “-vn” specifies no video
  • “-y output.mp3” specifies the output file name

If the video contains multiple audio tracks, use “-map” to extract the specific audio file.

ffmpeg -i input_video.mp4 -map 0:a:0 output_audio_1.mp3

  • “-map 0:a:0” copies the first audio stream from the input. 

How can I Mute the Original Audio of a Video in FFmpeg?

There are two ways to remove the audio, such as using “-an” and “-map” options, and here are the commands:

  1. Using “-an”

ffmpeg -i video.mp4 -an output.mp4

  • “-an” filters out all audio streams and turns off the inclusion of audio to the output.
  1. Using “-map”

ffmpeg -i video.mp4 -map 0:v -c:v copy output.mp4

  • “-map 0:v” specifies the video stream to be copied from the first input without re-encoding.
  • “-c:v copy” instructs “FFmpeg” to copy the audio file directly from the video.

Can FFmpeg Combine Multiple Audio Files into One?

Yes, FFmpeg allows you to combine multiple audio files into one without re-encoding if the audio formats are compatible. It can be done by concatenating multiple audio files using the following command:

ffmpeg -i audio1.mp3 -i audio2.mp3 -i audio3.mp3 -filter_complex “[0:a][1:a][2:a]concat=n=3:v=0:a=1” output.mp3

  • “-i audio1.mp3 -i audio2.mp3 -i audio3.mp3” specifies the first, second, and third input audio file
  • “[0:a][1:a][2:a]” represents the audio streams
  • “n=3” specifies the number of audio streams to be concatenated. “v=0” specifies zero video files.

Note: Use “-c:a aac” if codec incompatibilities exist with multiple audio formats.

How to Loop an Audio File Until the Video Ends?

Use “stream loop” and “shortest” to loop the audio until the video ends by the following command:

ffmpeg  -i input.mp4 -stream_loop -1 -i input.mp3 -shortest -map 0:v:0 -map 1:a:0 -y out.mp4

  • “-stream_loop -1” is followed by the “-i input.mp3” so FFmpeg effectively loops the audio files.
  • “-shortest” means finish the encoding when the shorter input stream ends. So, here, the shortest input stream is the video file.
  • “-map 0:v:0” selects the first video stream from the first input file and specifies it for the output.
  • “-map 1:a:0” selects the first audio stream from the second input file and specifies the stream “0” to ensure the audio looping.

What Are the Best Audio Formats to Use with FFmpeg?

The best audio format depends on your specific needs and priorities. However, FFmpeg supports audio codecs such as MP3, AAC, OPUS, FLAC, WAV, and PCM. A lossless codec such as PCM is essential for the best audio quality. However, lossless audio codecs result in larger audio files. 

How do you fix common synchronization issues with FFmpeg?

Common synchronization issues arise if the audio lags, the audio is ahead, and the audio needs to be faster or faster. So, FFmpeg addresses the out-of-sync issues by the following command:

ffmpeg -i video.mp4 -itsoffset 1 –i audio.mp3 –c:v copy -c:a aac output.mp4

  • “-Itsoffset 1” is an important parameter because it tells the FFmpeg to add a delay of 1 second to the input stream. 
  • “-c:a aac” instructs the FFmpeg to encode the audio stream using the AAC audio codec.

Use “-itsoffset -1” if audio is leading the video, and it will introduce a delay to the final output.

Can FFmpeg Add Audio to GIFs?

No, FFmpeg does not allow adding audio to the GIFs. GIFs are lossless image formats designed for animation and typically don’t contain audio. However, a simple workaround is converting the GIFs to video and adding the audio file. So, first, convert the GIFs to the compatible video format using FFmpeg and then combine the video with audio. 

What to Do If FFmpeg Fails to Recognize an Audio Codec?

The error message will appear during processing if FFmpeg fails to recognize the audio codec. Here are some steps to address the issues:

  • FFmpeg fails to recognize if the audio codec is set to “unknown” or if it is associated with an unrecognizable audio format. 
  • It supports three AAC-LC encoders and two HE-AAC encoders. So, ensure that you are using a suitable encoder.
  • FFmpeg relies on external libraries to support multiple formats by decoding the audio codecs. So, install the missing libraries from authentic sources for your audio codecs.

How can I Batch Process Multiple Videos with FFmpeg?

Using the batch process with FFmpeg enables you to apply the same effect to multiple files in a single step. Here’s the method to achieve this:

for f in videos/*; do ffmpeg -i “$f” “compressed/$(basename “${f%.*}”).mkv”; done

  • “for f in videos/*;” This initiates a “for” loop and iterates files (*) within the directory “videos.” Here, “f” represents the filename of each file in the directory.
  • -i “$f” specifies the input file name within the loop.
  • “Compressed” specifies the output directory as compressed.
  • $(basename “${f%.*}”) here removes the extension from the filename, and “basename” will output only the filename.

Paste Type Rotor Production Line

Paste Type Rotor Production Line,Dc Refrigeration Compressor Rotor Production,Rotor Production Line,Automatic Adhesive Rotor Production Line

Suzhou Mountain Industrial Control Equipment Co., Ltd , https://www.szmountain.com