(Last Updated On: April 28, 2019)

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.

I wrote a simple bash script 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.

Prerequisites

  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 https://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-$(rpm -E %fedora).noarch.rpm
sudo dnf -y install https://download1.rpmfusion.org/nonfree/fedora/rpmfusion-nonfree-release-$(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 http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
sudo rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm
sudo yum install ffmpeg ffmpeg-devel lame

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

Step 2: Download Conversion script

Download the script from Github

wget https://raw.githubusercontent.com/jmutai/dotfiles/master/scripts/mp3con.sh

You can as well copy below script snippet.

#!/usr/bin/env bash

# My bash Script to convert mp4 to mp3
# By NerdJK23
# web: www.computingforgeeks.com
# email: [email protected]

# Requires
# ffmpeg installed
# lame installed
# Check https://computingforgeeks.com/how-to-convert-mp4-to-mp3-on-linux/

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

case $selection in
    1)
	echo "Okay.."
	echo ""
	echo "Current dir is `pwd` "
	;;
    2)
    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 
else
    echo "Invalid directory, exiting.."
    echo ""
    exit 10
fi

    echo
    ;;
    
   *)
       echo
       echo "Wrong selection"
       exit 11
       ;;
esac

echo ""

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

current_dir=`pwd`
base_name=` basename "$current_dir"`

if [[ ! -d "$base_name"-mp3 ]]; then
    
echo "$base_name" | xargs  -d "\n" -I {} mkdir {}-mp3
    echo ""
fi
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"`";
done

# Move audio directory to ~/Music

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

mv  "$base_name"-mp3 ~/Music/

# Check if conversion successfull

echo ""

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

Make the script executable:

chmod +x mp3con.sh

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

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

./mp3con.sh

or just

sh mp3con.sh

You’ll see something similar to below

convert mp4 to mp3

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'

Conclusion

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.

Similar Articles:

How to use ytdl-webserver Webserver for downloading youtube videos

Annie – Fastest YouTube Video Downloader for Linux