How to extract audio from video on Linux

The purpose of this tutorial is to extract the audio contents from a video file on a Linux system. This can be done from the command line after installing the ffmpeg software package, if you do not already have it. The audio can be extracted into a variety of formats like mp3 or ogg, and the video types supported range from mp4, mkv, avi, and others.

In this tutorial you will learn:

  • How to install ffmpeg on all major Linux distros
  • How to extract audio from a video on Linux
How to extract audio from video on Linux
How to extract audio from video on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software ffmpeg
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Install ffmpeg on major Linux distros



Twitter icon Follow LinuxConfig.org on Twitter for the latest tips and tricks about Linux!


Before we dive into the examples below on how to extract audio from a video file, you will need to install ffmpeg on your system. The software is available on all major Linux distros and can be easily installed using your system’s package manager. Use the appropriate command below to install it on your own computer.

To install ffmpeg on Ubuntu, Debian, and Linux Mint:

$ sudo apt install ffmpeg

To install ffmpeg on Fedora:

$ sudo dnf install https://mirrors.rpmfusion.org/free/fedora/rpmfusion-free-release-$(rpm -E %fedora).noarch.rpm https://mirrors.rpmfusion.org/nonfree/fedora/rpmfusion-nonfree-release-$(rpm -E %fedora).noarch.rpm
$ sudo dnf install ffmpeg

To install ffmpeg on CentOS, AlmaLinux, and Red Hat:

$ sudo dnf install --nogpgcheck https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
$ sudo dnf install --nogpgcheck https://mirrors.rpmfusion.org/free/el/rpmfusion-free-release-8.noarch.rpm https://mirrors.rpmfusion.org/nonfree/el/rpmfusion-nonfree-release-8.noarch.rpm
$ sudo dnf install ffmpeg

To install ffmpeg on Arch Linux and Manjaro:

$ sudo pacman -S ffmpeg

Extract audio from video – command examples

Check out some of the audio extraction examples below to see how ffmpeg can produce an audio file from your video file.

  1. Extract audio into an mp3 file from an mp4 video:
    $ ffmpeg -i video.mp4 audio.mp3
    



  2. Extract audio into an ogg file from an mp4 video:
    $ ffmpeg -i video.mp4 -vn -acodec libvorbis audio.ogg
    

    Notice the syntax has changed a little from the first example, because using the first syntax will create an ogg video file instead of an audio file.

  3. Alternatively, ffmpeg also allows you to adjust the audio output sampling rate to eg. 44100, 22050, or 11025. The following Linux command will change to output audio sampling rate to 22050 Hz:
    $ ffmpeg -i video.mp4 -ar 22050 audio.mp3
    
  4. Or to change the sampling rate when extracting audio into an ogg file:
    $ ffmpeg -i video.mp4 -vn -acodec libvorbis -ar 22050 audio.ogg
    
  5. The process works the same for other video file types like avi, mkv, and mov.
    $ ffmpeg -i video.avi audio.mp3
    $ ffmpeg -i video.mkv audio.mp3
    $ ffmpeg -i video.mov audio.mp3
    
  6. You can also extract audio into wav format, which is an uncompressed audio format:
    $ ffmpeg -i video.mp4 audio.wav
    

Closing Thoughts




In this tutorial, we saw how to extract audio from a video file on a Linux system. This is facilitated by the ffmpeg command, which can extract audio into various formats and codecs. Our examples contained some of the most common formats, but many more exist, and the ffmpeg software is packed with options. You can adapt our examples to your own needs, while customizing your commands with further options found in the ffmpeg man pages.



Comments and Discussions
Linux Forum