Convert mp4/mkv/WebM to mp3 on Linux using FFmpeg

Posted on 19 views

Everyone at some point wanted or would like to convert mp4/mpv/WebM files to mp3 on Linux. If you would like to achieve that on any Linux system, don’t worry because I have a good solution for you. FFmpeg allows you to convert any media file from one format to another.

We have a simple bash script that can be used to convert mp4/mkv/WebM media files to mp3 on Linux. The script can convert mp4, mkv and WebM video formats to mp3 audio format in few seconds.


  1. FFmpeg
  2. Lame

You’ll have a small assignment of finding out how to get ffmpeg and lame on your distribution. But for common Linux distributions, check next section.

Step 1: Install FFmpeg

For Arch Linux users, install Prerequisites; ffmpeg and lame by:

sudo pacman -S lame ffmpeg

For Ubuntu / Debian:

sudo apt -y install ffmpeg lame

For Fedora:

sudo dnf -y install$(rpm -E %fedora).noarch.rpm
sudo dnf -y install$(rpm -E %fedora).noarch.rpm
sudo dnf -y install ffmpeg ffmpeg-devel lame

For CentOS 7:

sudo yum -y install epel-release
sudo rpm -v --import
sudo rpm -Uvh
sudo yum install ffmpeg ffmpeg-devel lame

CentOS / RHEL 8: Install FFmpeg on CentOS / RHEL 8

Installation can also be done by manually compiling ffmpeg from source.

Step 2: Download Conversion script

Download the script from Github


You can as well copy below script snippet.

#!/usr/bin/env bash

# My bash Script to convert mp4 to mp3
# By Josphat Mutai
# web:
# email: [email protected]

# Requires
# ffmpeg installed
# lame installed
# Check

echo -ne """
1: Current directory
2: Provide directory
echo ""
echo -n "Selection : "
read selection

case $selection in
	echo "Okay.."
	echo ""
	echo "Current dir is `pwd` "
    echo ""
    echo -n "Give diretory name: "
    read dir_name

# Check if given directory is valid
if [ -d $dir_name ]; then
    cd "$$dir_name"
    echo "Current directory is `pwd` "
    echo "Invalid directory, exiting.."
    echo ""
    exit 10

       echo "Wrong selection"
       exit 11

echo ""

# Create dir to store mp3 files if it doesn't exist
# First get the current directory name

base_name=` basename "$current_dir"`

if [[ ! -d "$base_name"-mp3 ]]; then
echo "$base_name" | xargs  -d "\n" -I  mkdir -mp3
    echo ""
echo ""

# Bigin to covert videos to mp3 audio files
# -d "\n" > Change delimiter from any whitespace to end of line character 

find . -name "*.mp4" -o -name "*.mkv" -o -name "*.webm" | xargs  -d "\n"  -I  ffmpeg -i  -b:a 320K -vn "$base_name"-mp3/.mp3 

# remove video extensions

cd "$base_name"-mp3

for file_name in *; do      
    mv "$file_name" "`echo $file_name | sed  "s/.mp4//g;s/.mkv//g;s/.webm//g"`";

# Move audio directory to ~/Music

if [[ ! -d ~/Music ]]; then
    mkdir ~/Music
cd ..

mv  "$base_name"-mp3 ~/Music/

# Check if conversion successfull

echo ""

if [[ $? -eq "0" ]];then
    echo " All files converted successfully"
    echo "Conversation failed"
    exit 1

Make the script executable:

chmod +x

Step 3: Convert mp4/mkv/WebM media files to mp3 on Linux:

Execute script, ( You can do it in the folder containing mp4 videos)


or just


You’ll see something similar to below


Since I’m running the script inside the directory with Video files I’ll like to convert to mp3, I’ll go with option 1. Press enter and wait for it to finish.


After a successful conversion, the resulting audio files will be saved under ~/Music directory with folder name being the name of the previous folder containing video files with the extension -mp3 at the end.

$ ls ~/Music/Gospel-mp3
'Broken Vessels (Amazing Grace) Hillsong UNITED from the Sea of Galilee-MlPuR-0XxiA.mp3'
'Hillsong United - '\''Oceans'\'' (Live show at Caesarea)-HVAR85rorvU.mp3'
'Hosanna - From HOLY LAND in Jerusalem - Hillsong United-mqF1EhsBz0A.mp3'
'Jonathan Nelson - I Believe (Island Medley) (LIVE)-vDLByAnQ93Q.mp3'
'No Longer Slaves (Official Lyric Video) - Jonathan David & Melissa Helser - W....mp3'
'Travis Greene - Intentional (Official Music Video).mp3'


You have learned how to easily convert mp4/mkv and WebM video files to mp3 on a Linux system using FFmpeg. If you do an ls in your ~/Music directory, you should see new folder added. Thanks for checking our short tutorial on how to convert mp4/mkv/WebM media files to mp3 on Linux. hope it was helpful.


Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.