Wednesday, June 10, 2009

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


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

No comments:

Post a Comment