(Last Updated On: August 12, 2017)

Everyone at some point wanted or would like to convert mp4 to mp3 on Linux. If you would like to achieve that on any Linux system, don’t worry because I’ve a good solution for you.

I wrote a simple bash script to convert mp4 to mp3 on Linux. The script will convert mp4, mkv and webm video formats to mp3 audio format.


  1. FFmpeg
  2. Lame

You’ll have small assignment of finding out how to get ffmpeg and lame on your distribution.

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

# pacman -S lame ffmpeg

If you believe you’re a Linux guru, you can install ffmpeg from source using link below.


Convert mp4 to mp3 on Linux:

Download the script from github

Script github link: https://github.com/NerdJK23/dotfiles/blob/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], [email protected]

# Requires
# ffmpeg installed
# lame installed

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 mp3con.sh

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 successful conversion, the resulting audio files will be saved under ~/Music directory with folder name being the name of previous folder containing video files with the extension -mp3 at the end.


If you do an ls in your ~/Music directory, you should see new folder added. This was a short tutorial on how to convert mp4 to mp3 on Linux. hope it was helpful.