If you are looking for a free and open-source Linux-based solution for handling streams and multimedia files like videos, audios, and images, then you should strongly consider what FFmpeg has to offer.
The FFmpeg tool requires that its users are familiar with the Linux command-line environment usage in terms of syntax reference, command implementation, and execution.
As for its installation of the Linux operating system distribution you are using, you need to have root/sudoer user privileges to successfully invoke the associated installation command.
Problem Statement
Supposing we have the following video file (rock.mp4) stored on our Linux OS file system.
Let us for a moment assume that the video file contains an audio segment that we need on another video animation project we are running on the sideline. Before we consider the solutions under the FFmpeg tool for handling multimedia and stream files, we must make sure this tool is installed on our Linux systems.
Installing FFmpeg in Linux
Depending on the Linux operating system distribution you are using, reference one of the following command to successfully have this tool installed on your Linux OS.
Install FFmpeg in RHEL
On RHEL-based distributions like RHEL, CentOS Stream, Rocky Linux, and AlmaLinux:
$ sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm $ sudo dnf upgrade $ sudo subscription-manager repos --enable "rhel-*-optional-rpms" --enable "rhel-*-extras-rpms" $ sudo yum update $ sudo yum install snapd $ sudo systemctl enable --now snapd.socket $ sudo ln -s /var/lib/snapd/snap /snap $ sudo snap install ffmpeg
On Fedora Linux distribution:
$ sudo dnf makecache $ sudo dnf install ffmpeg-free
On Debian distributions like Ubuntu and Linux Mint:
$ sudo apt update && sudo apt upgrade -y $ sudo apt install ffmpeg
On Arch Linux and Manjaro:
$ sudo pacman -Syu $ sudo pacman -S ffmpeg
On OpenSUSE Leap:
$ sudo zypper refresh $ sudo zypper install ffmpeg
On OpenSUSE Tumbleweed:
$ sudo zypper refresh $ sudo zypper install ffmpeg-4
Using FFmpeg to Extract Audio from Video Files
The final extracted audio needs a container format. For instance, let us go with mp4 format for this tutorial guide.
Before we start extracting the input video file’s audio tracks, we should list the audio streams associated with the video file using the ffprobe tool. This tool is installed together with FFmpeg.
$ ffprobe rock.mp4
We have identified aac as a viable audio stream format. Now, our output audio file will have a name like rock.aac.
To extract all audio stream from video file, use the following command.
$ ffmpeg -i rock.mp4 -map 0:a -acodec copy rock.aac
-i
points to the input video file.-map 0:a
picks available audio streams.-acodec copy
copies the picked audio streams (without re-encoding).
To extract partial audio stream (based on time). Here, we need to be specific with the start time (-ss)
of the video file to the end time (-t)
of the video file to get an audio fragment/fraction of the video file based on a specific playable duration.
$ ffmpeg -i rock.mp4 -map 0:a -ss 00:03:00 -t 00:00:30.0 -acodec copy audio_fraction.aac
You might also like to read the following articles:
- How to Convert Videos to GIFs Using FFmpeg
- How to Check Video Files Formats in Linux
- How to Find Video Resolution (Width and Height) in Linux
We can now fully or partially extract audio streams from video files using FFmpeg in Linux.