How to Bulk Download YouTube Videos on Linux and Convert to MPG and MP3

Tags:

This tutorial shows how to bulk download YouTube videos on Linux and then convert the FLV files to MPG video and MP3 audio files.

Required Software

I am using Ubuntu Linux, but the concept should be the same for other distros. Make sure you already have all of the multimedia codecs including ffmpeg and mplayer. You will also need to install youtube-dl:

sudo apt-get install youtube-dl

Downloading YouTube Videos on Linux

To download YouTube videos on Linux, use youtube-dl in the terminal like this:

youtube-dl [options] video_url

To see available options type man youtube-dl in a terminal.

Converting YouTube Videos to MPG on Linux

To convert YouTube movies from FLV to MPG on Linux use ffmpeg like this in the terminal:

ffmpeg -i old-file.flv new-file.mpg

Ripping MP3s from YouTube Videos on Linux

It is easy to extract MP3 audio from a YouTube movie on Linux with mplayer.

The method to rip MP3 audio from FLV files with ffmpeg from this site doesn't work on my computer so I use mplayer to do it:

mplayer -dumpaudio old-file.flv -dumpfile new-file.mp3

Automating the Process with a Shell Script

If you want to download a set of YouTube movies and convert them to MP3 audio, or save them as MPG movies on your hard drive, you can automate the process with a shell script.

This is just a quick script showing how it might be done. Use the following code at your own risk, and make sure you understand what it does before doing it. It will delete all the FLV files in the current directory after it processes them.

#!/bin/sh

# This script downloads YouTube videos and converts them
# You will end up with an MPG video and MP3 audio of each YouTube movie
# You need to install the following three programs:
# youtube_dl, ffmpeg, and mplayer

# Use this script at your own risk!
# Make sure that you understand how it works before you use it!

# USAGE:
# Make a file called videos.txt with the URL of 1 YouTube video on each line.
# Don't leave any blank lines in the file
# Put the videos.txt file in an empty folder along with this script
# run this script with the following commands:
# $ ./youtube_downloader.sh
# It will take a long time to run, depending on
# how many videos you have in your videos.txt file.

while read inputline
do
  youtube_url="$(echo $inputline)"
  youtube-dl $youtube_url
done < videos.txt

# Convert the flv files to mpg and mp3 files
for i in *.flv
do
  ffmpeg -i $i $i.mpg
  # comment out the next line if you don't want MP3's extracted
  mplayer -dumpaudio $i -dumpfile $i.mp3
  # The next line deletes all FLV files in the current directory to cleanup
  rm $i
done

See also pytube...

AttachmentSize
youtube_downloader.sh.txt971 bytes
Average: 3.8 (9 votes)

Comments

http://www.bashterritory.com/

Where is the repository?

When I type "sudo apt-get install youtube-dl" in console I get "E: couldn't find package youtube-dl"
This tells me the repository where the file is located is probably unknown to my dear EEEpc. Could you please tell me where to find it, so I can try this promising program.

Webmaster Tips's picture

Universe

Sorry, I forgot to mention that it's in the Universe Repository (Ubuntu).

There's an Easier way to do this..

You're doing it the messy and somewhat painful way. Just visit http://www.vidimonkey.uni.cc click on bulk,
then paste in all your video links (can be from any site).

Then click submit or convert or hit enter. Wait as it starts to do its thing and it spits out a whole lot of video links which you can just download directly.

Now use SUPER or WinAVI (my fav) or any other converting program and most allow you to batch convert the videos, just add them and click convert to whatever format. Whole process takes about 1-2 hrs including download and conversion time on a 500mhz comp and doing about 20 30-40 minute videos.

Thanks!!

Works excellent! Thanks for taking the time to share this with us!

mp3 conversion

Is there a way to get mplayer to put out a stereo mp3 from an flv file?

I'm pretty new and I got lost in the man - the internet didn't do much for me either.

Webmaster Tips's picture

mplayer

I don't know how to do it, but there is some info the man pages.

  1. open a terminal
  2. type man mplayer
  3. press the / (slash) key to search
  4. type stereo and hit enter
  5. use the n key to find the next occurrence of stereo in the man page.

I only had time to skim it — maybe mplayer -channel?

Automated download script

This is a great script! After using it for a while, I found a more sophisticated version (which does pretty much the same) here.

That variant allows downloading of videos in better quality (depending on whether the video has been uploaded to youtube in HQ or not of course).