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.

Prerequisites

  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.

https://trac.ffmpeg.org/wiki/CompilationGuide

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: kiplangatmtai@gmail.com, josepy@computingforgeeks.com

# Requires
# ffmpeg installed
# lame installed

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

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.

Conclusion

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.

SHARE
Previous articleInstall Arch Linux with LVM on UEFI system
I'm a fan of Linux and open source enthusiast with bachelor's degree in Electrical and Communications Engineering.The Swiss Army Knife syndrome makes me an expert in the fields of Virtualization, Cloud Computing,VoIP,Linux Containers and Server Hardening. Currently i have the following certifications: 1. LPI 2. SUSE Certified Linux Administrator ( CLA) 3. SUSE Certified Linux Professional ( CLP) 4. CCNA And working on RHCSA, RHCE, RHCVA, and Openstack Cloud Certification. I have a strong desire to learn, develop, and share knowledge with others.