SoFunction
Updated on 2025-04-06

Detailed Guide to Extract Audio Using FFmpeg in Java

introduction

FFmpeg is an open source multimedia processing tool that supports video and audio encoding, decoding, conversion and other functions. With FFmpeg, extracting audio from video and saving it in various formats is very simple and efficient. This has a wide range of applications in audio and video editing, media processing, transcoding and other scenarios.

This article will explain in detail how to use FFmpeg to extract audio, including common audio format extraction, audio quality adjustment, advanced processing operations, etc. The content is easy to understand and is suitable for beginners to master it quickly.

1. Why extract audio?

Extracting audio is a common requirement when working with multimedia files. For example, extracting background music and audio tracks in videos for audio editing and mixing, or extracting conversation content in videos for speech recognition analysis, etc. FFmpeg can easily separate the audio parts in a video and save them as separate audio files.

2. Basic operations of FFmpeg extracting audio

The most basic way to extract audio is to save the audio tracks in the video file as an audio file separately. FFmpeg supports various audio formats, such as MP3, AAC, WAV, OGG, etc. When extracting audio, you usually only need to specify the input file, output file format, and encoding method.

2.1 Extract audio from video to MP3 format

One of the most common needs is to extract and save the audio from the video into MP3 format. Using FFmpeg can be done by:

ffmpeg -i input.mp4 -q:a 0 -map a output.mp3
  • -i input.mp4: Specify the entered video file.
  • -q:a 0: Specify audio quality,0Indicates the highest quality. This parameter can be adjusted as needed.
  • -map a: Specify the extracted audio track,aIndicates audio.
  • output.mp3: Save the output file in MP3 format.

This command willinput.mp4The audio tracks in  extract and save asoutput.mp3, the highest audio quality.

2.2 Extract audio to WAV format

If you need audio formats with lossless sound quality, you can extract them as WAV format. The WAV format is lossless and therefore suitable for scenarios requiring high sound quality:

ffmpeg -i input.mp4 -vn -acodec pcm_s16le -ar 44100 -ac 2 
  • -vn: means that the video track is not included.
  • -acodec pcm_s16le: Specify the encoding method commonly used in the WAV format for audio encoding as PCM.
  • -ar 44100: Set the audio sample rate to 44100 Hz.
  • -ac 2: Set to two-channel audio.
  • : Save the output file in WAV format.

WAV formats are often used in high-quality audio editing or scenarios where lossless storage is required.

2.3 Extract audio into AAC format

AAC is a widely used audio format with good compression ratio and sound quality balance. The command to extract audio in AAC format is as follows:

ffmpeg -i input.mp4 -vn -acodec aac -b:a 128k 
  • -acodec aac: Specify the audio encoding to AAC format.
  • -b:a 128k: Set the audio bit rate to 128 kbps, and the bit rate can be adjusted as needed.

This command extracts and encodes the audio from the video into AAC format, and is suitable for scenarios where file size and sound quality requirements are taken into account.

3. Advanced parameters in audio extraction

In addition to basic audio extraction operations, FFmpeg also provides many advanced parameters that can help you carefully control the quality, format and encoding of audio output according to different needs.

3.1 Control audio bit rate

When extracting audio, controlling the bit rate can affect the quality and size of the audio file. The higher the bit rate, the better the sound quality, but the file size also increases. The commonly used bit rate unit is kbps (kilobits per second), which can be passed-b:aParameter Specification:

ffmpeg -i input.mp4 -vn -b:a 192k output.mp3

This command extracts the audio to MP3 format and sets the bit rate to 192 kbps. Common audio bit rate settings are as follows:

  • 128 kbps: Suitable for general music or voice recording.
  • 192 kbps: better sound quality, suitable for scenes with slightly higher sound quality requirements.
  • 320 kbps: Close to CD sound quality, suitable for high-quality music or professional use.

3.2 Change the audio sample rate

The sampling rate refers to the number of audio samples collected per second, usually in Hertz (Hz). FFmpeg will use the sample rate of the input audio by default, but you can use-arParameters custom sampling rate. For example, set the audio sample rate to 48 kHz:

ffmpeg -i input.mp4 -vn -ar 48000 output.mp3

Common sampling rates are:

  • 44100 Hz: CD sound quality is standard, suitable for most scenarios.
  • 48000 Hz: Higher sound quality, often used in professional audio equipment or video dubbing.

3.3 Change the number of channels

FFmpeg allows you to change the number of channels when extracting audio. Common channels are provided with mono and dual channels. Can be used-acParameters specify the number of channels. For example, extract mono audio:

ffmpeg -i input.mp4 -vn -ac 1 output_mono.mp3

If you need two-channel audio, the command is as follows:

ffmpeg -i input.mp4 -vn -ac 2 output_stereo.mp3

For most music and videos, two channels are standard settings, while mono is suitable for simple voice recording or broadcast scenarios.

4. Extract the specified track

In multi-track videos, audio tracks may be included in different languages ​​(such as English, French, Japanese). With FFmpeg, you can select specific tracks for extraction instead of extracting all tracks by default.

4.1 View audio track information

First, you can use the following command to view the audio track information of the video file:

ffmpeg -i input.mp4

The output will display the audio track information in the video file, such as the audio track number, language, etc.

4.2 Extract specific sound tracks

Suppose the video file has two audio tracks: English and Japanese, you only want to extract Japanese audio tracks. You can pass-mapParameters specify the track number, for example, extracting the second track:

ffmpeg -i input.mp4 -map 0:a:1 -c copy output_japanese.mp3

The inside-map 0:a:1Indicates the second track that extracts the first input file.

5. Extract and convert audio formats

When processing audio and video files, it is often necessary to extract the audio and convert it to a different format. FFmpeg supports almost all mainstream audio formats, including MP3, AAC, WAV, OGG, etc.

5.1 Extract and convert to OGG format

OGG is an open audio format commonly used in network streaming. The following command extracts and saves the audio from the video to OGG format:

ffmpeg -i input.mp4 -vn -acodec libvorbis -q:a 4 
  • -acodec libvorbis: Specifies to use Vorbis encoding, suitable for OGG format.
  • -q:a 4: Set the sound quality level, the range is 0 to 10, the larger the number, the better the sound quality.

5.2 Extract and convert to FLAC format

FLAC is a lossless audio format suitable for scenes with high sound quality requirements. The following command extracts and converts the audio to FLAC format:

ffmpeg -i input.mp4 -vn -acodec flac 
  • -acodec flac: Specify the use of FLAC encoding.

Lossless audio is usually used to store high-quality music files, such as CD backup, master storage, etc.

6. Batch extraction of audio

If you have multiple video files that need to extract audio, you can use a script to batch process it. The following is a simple shell script example that extracts all the current directory.mp4Audio in the file:

#!/bin/bash
for file in *.mp4; do
  ffmpeg -i "$file" -q:a 0 -map a "${file%.mp4}.mp3"
done

This script will traverse each of the current directory.mp4File, extract its audio into MP3 format and save it.

7. Summary

Through this article's explanation, you should master the various operations of using FFmpeg to extract audio. From basic audio extraction to format conversion, sound quality control, and batch processing, FFmpeg provides powerful capabilities to make audio processing efficient and flexible.

Whether you need to extract the background music from the video or want to process multi-track audio files, FFmpeg is good at it.

This is the article about this detailed guide on Java's audio extraction using FFmpeg. For more related Java FFmpeg audio extraction content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!