FFmpeg Audio change

FFmpeg, a powerful multimedia processing tool, provides developers with a wide range of capabilities, including the ability to change or manipulate audio in videos. Whether you want to replace, extract, or modify audio tracks, FFmpeg offers a versatile solution. In this detailed guide, we will explore the steps to change audio in videos using FFmpeg, providing comprehensive instructions and customization options.

  1. Installing FFmpeg:
    Before starting, ensure that FFmpeg is installed on your system. Download the latest version from the official website (https://ffmpeg.org/download.html) or use package managers like Homebrew on macOS or APT on Linux.
  2. Navigate to the Video File Directory:
    Open a terminal or command prompt and navigate to the directory containing the video file from which you want to change the audio. Use the cd command to change the directory:
   cd /path/to/video/directory
  1. Execute FFmpeg Command – Replace Audio:
    To replace the existing audio in a video with a new audio file, use the following FFmpeg command:
   ffmpeg -i input_video.mp4 -i new_audio.mp3 -c:v copy -c:a aac -strict experimental -map 0:v:0 -map 1:a:0 output_video.mp4

Breakdown of the command:

  • -i input_video.mp4: Specifies the input video file.
  • -i new_audio.mp3: Specifies the new audio file that will replace the existing audio.
  • -c:v copy: Copies the video codec to the output without re-encoding.
  • -c:a aac: Specifies the audio codec for the output.
  • -strict experimental: Enables experimental codecs, necessary for using the AAC codec.
  • -map 0:v:0 -map 1:a:0: Maps the video stream from the first input and the audio stream from the second input to the output.
  • output_video.mp4: Defines the output filename with the changed audio.
  1. Adjusting Parameters:
  • Input Files (-i): Modify the input filenames based on your video and audio file names.
  • Output Codec (-c:a): Choose the desired audio codec for the output. AAC is commonly used for compatibility.
  • Output Filename Format: Customize the output filename based on your preferences.
  1. Executing the Command:
    Press Enter to execute the FFmpeg command. FFmpeg will process the video, replacing the existing audio with the specified new audio file. The progress will be displayed in the terminal.
  2. Execute FFmpeg Command – Extract Audio:
    To extract the audio from a video into a separate audio file, use the following FFmpeg command:
   ffmpeg -i input_video.mp4 -vn -acodec pcm_s16le -ar 44100 -ac 2 output_audio.wav

Breakdown of the command:

  • -i input_video.mp4: Specifies the input video file.
  • -vn: Disables video processing.
  • -acodec pcm_s16le: Sets the audio codec to PCM (uncompressed audio).
  • -ar 44100: Sets the audio sample rate to 44.1 kHz.
  • -ac 2: Sets the number of audio channels to 2 (stereo).
  • output_audio.wav: Defines the output audio filename.
  1. Viewing the Output:
    Once the process is complete, navigate to the output directory where the modified video or extracted audio is saved. Open the generated files using a media player or audio editing tool to inspect the changes.
  2. Customization and Additional Options:
    Explore FFmpeg’s extensive documentation for advanced options, such as adjusting audio volume, applying filters, or using specific codecs. Customizing the command allows you to change audio in videos with specific characteristics.

Audio filter: atempo

In FFmpeg to adjust the tempo of the audio, we can use the special atempo filter described in the table

DescriptionChanges audio tempo – the speed of the audio stream.

Description of parameter

tempofloat number from the range 0.5 – 2.0, values less than 1.0 slows down and values over 1.0
speed up the tempo, the default value is 1.0

For example to hear the input audio with a 2-times faster speed, we can use the command:

ffplay -i speech.mp3 -af atempo=2

For example to synchronize with timestamps the data in the file music.mpg we can use the command:

ffmpeg -i music.mpg -af asyncts=compensate=1 -f mpegts music.ts

Changing audio in videos using FFmpeg provides a versatile and efficient solution for various scenarios. By following this comprehensive guide, you can use FFmpeg to replace, extract, or modify audio in videos, tailoring the output to your specific requirements.

Leave a Comment