Shell Script to download youtube files from playlist

32,438

Solution 1

OK so after further research and updating my version of youtube-dl, it turns out that this functionality is now built directly into the program, negating the need for a shell script to solve the playlist download issue on youtube. The full documentation can be found here: (http://rg3.github.com/youtube-dl/documentation.html) but the simple solution to my original question is as follows:

1) youtube-dl will process a playlist link automatically, there is no need to individually feed it the URLs of the videos that are contained therein (this negates the need to use grep to search for "watch?" to find the unique video id

2) there is now an option included to format the filename with a variety of options including:

  • id: The sequence will be replaced by the video identifier.
  • url: The sequence will be replaced by the video URL.
  • uploader: The sequence will be replaced by the nickname of the person who uploaded the video.
  • upload_date: The sequence will be replaced by the upload date in YYYYMMDD format.
  • title: The sequence will be replaced by the literal video title.
  • ext: The sequence will be replaced by the appropriate extension (like flv or mp4).
  • epoch: The sequence will be replaced by the Unix epoch when creating the file.
  • autonumber: The sequence will be replaced by a five-digit number that will be increased with each download, starting at zero.

the syntax for this output option is as follows (where NAME is any of the options shown above):

youtube-dl -o '%(NAME)s' http://www.youtube.com/your_video_or_playlist_url

As an example, to answer my original question, the syntax is as follows:

youtube-dl -o '%(title)s.%(ext)s' http://www.youtube.com/playlist?list=PL2284887FAE36E6D8&feature=plcp

Thanks again to those who responded to my question, your help is greatly appreciated.

Solution 2

If you want to use the title from youtube page as a filename, you could use -t option of youtube-dl. If you want to use the title from your "video list" page and you sure that there is exactly one watch? URL for every <span class="title video-title" title, then you can use something like this:

#!/bin/bash

TMPFILE=/tmp/downloader-$$

onexit() {
  rm -f $TMPFILE
}

trap onexit EXIT

curl -s "$1" -o $TMPFILE

i=0
grep '<span class="title video-title "' $TMPFILE | cut -d\> -f2 | cut -d\< -f1 | while read title; do
  titles[$i]=$title
  ((i++))
done

i=0
grep "watch?" $TMPFILE | cut -d\" -f4 | while read url; do
  urls[$i]="http://www.youtube.com$url"
  ((i++))
done

i=0; while (( i < ${#urls[@]} )); do
  youtube-dl -o "${titles[$i]}.%(ext)" "${urls[$i]}"
  ((i++))
done

I did not tested it because I have no "video list" page example.

Share:
32,438
wvandaal
Author by

wvandaal

Like a stepson of anarchy

Updated on July 09, 2022

Comments

  • wvandaal
    wvandaal almost 2 years

    I'm trying to write a bash script that will download all of the youtube videos from a playlist and save them to a specific file name based on the title of the youtube video itself. So far I have two separate pieces of code that do what I want but I don't know how to combine them together to function as a unit.

    This piece of code finds the titles of all of the youtube videos on a given page:

    curl -s "$1" | grep '<span class="title video-title "' | cut -d\> -f2 | cut -d\< -f1
    

    And this piece of code downloads the files to a filename given by the youtube video id (e.g. the filename given by youtube.com/watch?v=CsBVaJelurE&feature=relmfu would be CsBVaJelurE.flv)

    curl -s "$1" | grep "watch?" | cut -d\" -f4| while read video; 
    do youtube-dl "http://www.youtube.com$video";
    done
    

    I want a script that will output the youtube .flv file to a filename given by the title of the video (in this case BASH lesson 2.flv) rather than simply the video id name. Thanks in advance for all the help.

  • wvandaal
    wvandaal over 12 years
    Thanks for the answer praetorian, the -t option works for the time-being but the script you provided has some errors in it that I'll need to check out. Currently the files themselves do not download at all but I haven't yet had the time to test your script and see why. I'll keep this thread updated if I find anything out.
  • Admin
    Admin over 11 years
    I am learning bash script in downloading youtube videos this is based on latest youtube algorithm. It will display all the available video resolution.you can download .webm, .mp4, .flv, .3gpp file formats.
  • etuardu
    etuardu over 11 years
    Sorry but this has nothing to do with the original question, -1.
  • VertigoRay
    VertigoRay almost 6 years
    This is the best answer for sure. Thanks for sharing. Looks like 'stitle' isn't supported anymore. Use 'title' instead; I updated your answer.