How to download and convert Youtube videos to mp3, in Ubuntu Linux

This post describes how to download and convert Youtube videos to mp3, in Ubuntu Linux. Please note that this is not an encouragement to copyright infringement, I use these tools for downloading and listening to videos on my long commute with the London Underground, where no GSM or Wifi signal are available.

Requirements:

youtube-dl  – "is a small command-line program to download videos from YouTube.com and a few more sites."

ffmpeg – "is a complete, cross-platform solution to record, convert and stream audio and video.", required by youtube-dl.

libavcodec-extra-53 – Libav codec library, required by youtube-dl for converting files to mp3.

Install:

sudo apt-get install youtube-dl

sudo apt-get install ffmpeg

sudo apt-get install libavcodec-extra-53

Script:

The following script (fetch.sh), will download videos based on URLs stored in a text file (tracks.lst):

#!/bin/bash
tracks=()
while read line
do
  	echo -e "Downloading '$line'..."
	youtube-dl -q --console-title --extract-audio --audio-format=mp3 -o "%(title)s.%(ext)s" $line
done < tracks.lst

Usage:

  1. Upon first-time use, set the executable file permission: chmod +x fetch.sh
  2. Edit tracks.lst, and add URLs, one per line. E.g.:
    http://www.youtube.com/watch?v=wZZ7oFKsKzY
    http://www.youtube.com/watch?v=jI-kpVh6e1U
  3. Execute: ./fetch.sh

Resulting files should be stored with the name [Youtube Title].mp3, residing in the same folder as the script.

NOTE: If you do not wish to convert files to MP3, remove the "–extract-audio –audio-format=mp3" parameters in the above script.

Leave a Reply

Your email address will not be published. Required fields are marked *