Youtube mp3 downloader for linux with graphical interface

About the youtube mp3 downloader script

It’s a simple script with graphical interface that you can use to download mp3 files from youtube.

Advantages to other software of this kind:

  • It can be changed very easily with minimal programming knowledge
  • The script contains many comments to make it easy to understand
  • Can be run on your computer without the need for other online services
  • you can choose the file with the youtube link, but also the name of the author or audio track
  • consumes few resources

Program Disadvantages:

  • it is not click & run, you also need other installation commands
  • it’s not perfect because I’m not a good programmer
    The program is written in BASH and is very easy to modify / complete with any linux text editor.

How to install?

This program uses the curl and youtube-dl command in linux and you need yad to display the graphical interface.

To install this program you need curl, yad, youtube-dl and, of course, the script on this page. I use ubuntu 18.04 and the commands in this tutorial refer to this linux distribution. To install YAD and youtube-dl in other distributions, you must look for installation methods yourself.


Curl is a command line program that lets you download a html web page to your computer. Curl is not only used for this, it has multiple uses: in cars, TV sets, routers, printers, audio equipment, mobile phones, tablets, setop boxes, media players.
To install curl in ubuntu 18.04, follow these steps:
Open a linux terminal:
Ctrl + Alt + t
Write the following commands:
sudo apt update
sudo apt install curl

YAD is a programming tool that can help you create a graphical interface for scripts written in BASH. With yad you can create windows, menus, cassettes, lists, display pictures, etc.
I chose YAD because it is small and powerful. The binary file has only 144kb but has many features included. Yad is very easy to use even for beginners (like me). An interesting and very comprehensive YAD tutorial page is here.
For installation on Ubuntu 18.04 yad follow these steps:
Open a terminal:
Ctrl + Alt + t
Run the following commands in the terminal:
sudo apt update
sudo apt install yad

After installation, you can test the program with the following command in the terminal:
yad --width=300 --height=100 --title "This is my first yad test"
If everything is correct, a window will appear with the title you have selected and two OK and Cancel buttons. Click any of these buttons to close the window.

yad test

Youtube-dl is a program for linux console with which you can download audio or video files from youtube or other streaming sites. To install youtube-dl you need Python interpreter.
I’ve tried installing youtube-dl in the ubuntu 18.04 with the classical sudo apt install youtube-dl command. It was installed, but it did not work. For a trouble-free installation I’ve run the following commands:
sudo apt update
sudo apt-get install python-pip
sudo pip install youtube-dl

That’s how it worked.

Install youtube mp3 downloader script


Open a text editor for linux (eg, Geany).
Copy the text below and paste it into the text editor.

Youtube mp3 downloader GUI script
#!/bin/bash
cd ~/Music #working folder
exit0=0
while [ $exit0 -eq 0 ]
do
declare -a aname #array for names
declare -a alinks #array for links
echo -n "" > temp.tmp #first temp file
echo -n "" > temp1.tmp #second temp file
#first step: search window
frmdata=`yad --title "Youtube Downloader v2" --width=600 --text="Type search terms here..." --form --field="" --button="Cancel":2 --button="Search":0`
foo=$?
#if click on Cancel, exit
if [[ $foo -eq 2 ]]; then rm temp.tmp; rm temp1.tmp; killall -9 youtube-dl; exit; fi
frmaddr=$(echo $frmdata | awk 'BEGIN {FS="|" } { print $1 }')
search1=${frmaddr//" "/"+"}
#search on youtube for your terms
curl -s https://www.youtube.com/results\?search_query\=$search1 | grep -o 'watch?v=[^"]*"[^>]*title="[^"]*'|grep -v ";list" >> temp.tmp
file=temp.tmp
i=0
while IFS= read line
do
#extract links/names and put in array
line1="${line/\"/|}"
IFS='|' read -ra LINK <<< "$line1"
line2="${line/\" title=\"/|}"
IFS='|' read -ra NAMES <<< "$line2"
if [ ! -z "${LINK[0]}" ] && [ ! -z "${NAMES[1]}" ]; then
nr[$i]=$nr
alinks[$i]=https://www.youtube.com/${LINK[0]}
aname[$i]=${NAMES[1]}
echo ${aname[$i]} >> temp1.tmp
((++i))
fi
done <"$file"
#second step: display results list
frmdata1=`yad --width=600 --height=500 --list --no-headers --column=Name < temp1.tmp`
foo1=$?
frmaddr1=$(echo $frmdata1 | awk 'BEGIN {FS="|" } { print $1 }')
#convert names in links
for k in {0..20}
do
names="$(echo -e "${frmaddr1}" | tr -d '[:space:]')"
names1="$(echo -e "${aname[$k]}" | tr -d '[:space:]')"
if [[ "$names" = "${names1}" ]]; then
adress=${alinks[$k]}
fi
done
#third step: download and convert to mp3
youtube-dl --output "%(title)s.%(ext)s" --extract-audio --audio-format mp3 --audio-quality 0 $adress 2>&1 |
yad --title "Downloading..." --progress --pulsate --center --text="________________________________________________________\nWait few seconds for download...\n<b>The window will close automatically after downloading</b>\n________________________________________________________\nAverage time: <b>~10 seconds for 5-10 MB mp3</b>\n________________________________________________________" --button="Abort":2 --auto-kill --auto-close
foo=$?
if [[ $foo -eq 2 ]]; then rm temp.tmp; rm temp1.tmp; killall -9 youtube-dl; exit; fi #if click on Cancel, exit
done

Modify line 2 of the script if needed. There is the path to the working folder (where you will download the mp3 files on youtube).
Save this file with the name ydownloader_en.sh into a folder you choose (eg in the Desktop folder).
Open a terminal
Ctrl + Alt + t
go to the folder you chose:
cd ~/Desktop/
Change the attributes of the file to make it executable
chmod +x ydownloader_en.sh
Start the program with the command:
./ydownloader_en.sh

How to use youtube mp3 downloader?


In some Linux distributions, the program can be double-clicked on its name in the file manager. If it does not work, the file will open in the default text editor.
The program can be started from the command line in the terminal.
Ctrl + Alt + t
cd Desktop
./ydownloader_en.sh

The program works in more stages: searches your terms on youtube, displays the list of youtube results and downloads the mp3 file.
While running, the program will write 2 temporary files (temp.tmp and temp1.tmp) into the work folder. These files are automatically deleted to a normal program output.
When you start the program, a search box will be displayed. In this box, you type the search term or you paste the link directly from youtube and click on the Search button. To close the program, at this step you must press the Cancel button.

If you have chosen a search term and you have pressed Search, the program will search for that term on youtube and will display the result list with youtube titles. Click on the name you want and click the Ok button.
It will start downloading the video from youtube and then it will be converted to mp3 file. At this step if you look into the program’s work folder you will see one or more temporary files. These are deleted automatically after the downloaded file is converted to mp3.
After downloading an mp3 file, the program automatically switches to the first step (Search).
Exiting the program: the first step (Search) click on Cancel button.

Leave a Reply

Your email address will not be published.