Script to download all the related videos from youtube with youtube-dl

2010-05-17 411 words 2 mins read

Lets cut the long discussion <a class="zem_slink freebase/en/short_subject" title="Short film" rel="wikipedia" href="http://en.wikipedia.org/wiki/Short_film">short and directly give you the script. The script is combination of all the posts that I did in the past about youtube <a class="zem_slink freebase/en/uploading_and_downloading" title="Uploading and downloading" rel="wikipedia" href="http://en.wikipedia.org/wiki/Uploading_and_downloading">downloads. You need to copy all the <a class="zem_slink freebase/en/uniform_resource_locator" title="Uniform Resource Locator" rel="wikipedia" href="http://en.wikipedia.org/wiki/Uniform_Resource_Locator">URL&#8217;s in a file called &#8221;~/youtube.urls&#8221; which you want to be downloaded. Or if you need to donwload the related videos, then save the html of the page and give it as the first argument for the script. So, here&#8217;s the scrit:

#!/bin/bash –
#===============================================================================

#          FILE:  youtube_releated.sh

#         USAGE:  ./youtube_releated.sh

#   DESCRIPTION:  Get the related urls from the youtube html

#       OPTIONS:  —
#  REQUIREMENTS:  —
#          BUGS:  —
#         NOTES:  —
#        AUTHOR:  Amit Agarwal (AKA), amit.agarwal@amit-agarwal.co.in
#       COMPANY:  Individual
#       VERSION:  1.0
#       CREATED:  05/02/2010 04:45:54 PM IST
#      REVISION:  —
#===============================================================================
user=raj77india
pass=Kukku1977
if [  $# -ge 1 ]
then
for i in $(<a class="zem_slink freebase/en/grep" title="Grep" rel="wikipedia" href="http://en.wikipedia.org/wiki/Grep">grep video-<a class="zem_slink freebase/en/html_element" title="HTML element" rel="wikipedia" href="http://en.wikipedia.org/wiki/HTML_element">list-item $1 |sed &#8217;s/&.*//&#8217;|sed &#8217;s/.*=&#8221;\(.*\)/\1/&#8217;)
do
echo &#8221;<a class="zem_slink freebase/en/writing" title="Writing" rel="wikipedia" href="http://en.wikipedia.org/wiki/Writing">Writing $i to youtube.urls file&#8221;
echo &#8221;http://www.youtube.com$i&#8221; » ~/youtube.urls
done
fi

read -p &#8221;Download all the urls :&#8221; ans

cd ~

if [ &#8221;$ans&#8221; = &#8221;y&#8221; ]
then
grep -v &#8221;^#&#8221; ~/youtube.urls > /tmp/youtube.urls
while read line
do
youtube-dl -u $user -p $pass -t $line
done < /tmp/youtube.urls
fi

read -p &#8221;Add title to the file :&#8221; ans1

if [ &#8221;$ans1&#8221; != &#8221;y&#8221; ]
then
exit
fi
rm -f /tmp/youtube.urls

while read line
do
echo $line|grep -c &#8221;^#&#8221;
if [ $(echo $line|grep -c &#8221;^#&#8221;) -eq 1 ]
then
echo &#8221;#$line&#8221; »/tmp/youtube.urls
echo &#8221;Skipping $line&#8221;
continue
else
title=$(youtube-dl -u $user -p $pass -e $line)
echo &#8221;title is $title&#8221;
fi
done <~/youtube.urls

mv /tmp/youtube.urls ~/youtube.urls

Since the script is quite simple, its not much documented. Anyways if you need help, feel free to contact me 🙂 (contact form or comment)<h6 class="zemanta-related-title">Related articles by Zemanta <ul class="zemanta-article-ul"> <li class="zemanta-article-ul-li"><a href="http://mashable.com/2010/05/12/youtube-unlisted-videos/">YouTube Introduces New Privacy Option for Videos (mashable.com) <li class="zemanta-article-ul-li"><a href="http://blog.amit-agarwal.co.in/2010/05/03/download-youtube-video-command-line-youtubedl/">Download youtube video directly from the command line with youtube-dl (amit-agarwal.co.in) <li class="zemanta-article-ul-li"><a href="http://www.mt-soft.com.ar/2010/05/08/bytubed-makes-it-easier-than-ever-to-download-multiple-youtube-videos-in-one-go/">BYTubeD Makes It Easier Than Ever To Download Multiple Youtube Videos In One Go (mt-soft.com.ar) <li class="zemanta-article-ul-li"><a href="http://news.cnet.com/8301-27076_3-20004804-248.html?part=rss&subj=news&tag=2547-1_3-0-20">YouTube gets a very useful &#8217;unlisted&#8217; video option (news.cnet.com) <li class="zemanta-article-ul-li"><a href="http://r.zemanta.com/?u=http%3A//www.telegraph.co.uk/technology/google/7718944/YouTube-to-allow-private-videos.html&a=17971522&rid=c0dd8728-f84d-4fa7-9713-82709d89e076&e=60f5225d824312c9b68b523582484d50">YouTube to allow &#8217;private&#8217; videos (telegraph.co.uk) <div class="zemanta-pixie"><a class="zemanta-pixie-a" title="Reblog this post [with Zemanta]" href="http://reblog.zemanta.com/zemified/c0dd8728-f84d-4fa7-9713-82709d89e076/"><img class="zemanta-pixie-img" src="http://blog.amit-agarwal.co.in/wp-content/uploads/2010/08/reblog_b88.png" alt="Reblog this post [with Zemanta]" /><span class="zem-script more-related more-info pretty-attribution paragraph-reblog">


author

Authored By Amit Agarwal

Amit Agarwal, Linux and Photography are my hobbies.Creative Commons Attribution 4.0 International License.

We notice you're using an adblocker. If you like our webite please keep us running by whitelisting this site in your ad blocker. We’re serving quality, related ads only. Thank you!

I've whitelisted your website.

Not now
This website uses cookies to ensure you get the best experience on our website. Learn more Got it