Download the Youtube playlist in mp3 format a bash-script

It so happened that at the moment my work laptop has only 2GB of RAM. In this regard, it was necessary to optimize the browser as when a large number of tabs open is not enough memory and used swap-section, leading to the brakes. This paper helps me to music, usually an open table with playlist Youtube. So this table in prosesse work eats up to 500MB (!) And even more (Google Chrome). This situation forced the write bash-script that gets input playlist ID, the output – mp3 files that you can listen to your favorite player, for example, MOC:

youtube mp3 download

Download mp3 from Youtube impossible, so the process of dividing into 3 simple steps:

  1. download the flv
  2. extract audio track
  3. remove the temporary flv

In any case, remember that this is the playlist ID get-parameter «list». Dependencies:

sudo apt-get install youtube-dl ffmpeg libavcodec-extra-53
  • youtube-dl to download the video
  • ffmpeg libavcodec-extra-53 for conversion to mp3

Actually the script, fully commented

#! / Bin / bash

Usage = 'Usage:
  . / Get_youtube_playlist <playlist_id> <target_folder> <num_songs>
    target_folder: (default: songs will be downloaded in current folder)
    num_songs: number of songs to get (default: 50)

examples: 
  . / Get_youtube_playlist RD02HIkZaLeuF9k
  . / Get_youtube_playlist RD02HIkZaLeuF9k "instrumental hip-hop beats" 10
'

playlist_id = $ 1 
= target_folder $ 2 
num_songs = $ 3

if  [-z " $ playlist_id " ] ; then 
    echo  " Usage $ " 
    exit 1
 fi

if ! [[ " $ num_songs " = ~ ^ [0-9] + $]] ; then
    num_songs = 50
fi

if  [-z " $ target_folder " ] ; then 
    target_folder = '. /' 
elif  [! -D " $ target_folder " ] ; then 
    echo  "Parameter is Incorrect target_folder, $ Usage " 
    exit 1
 fi

# Using Youtube API to get a list of songs 
# -QO-https://gdata.youtube.com/feeds/api/playlists/ $ playlist_id ? max-results = $ num_songs `" 
if  [-z " $ youtube_api " ] ; then 
    echo  "Playlist ID is Incorrect, $ Usage " 
    exit 1
 fi

ID # List of songs put into an array of songs
songs = ( 
    $ ( echo  $ youtube_api | \
    grep-o-P "<Media: Player url = '. *? &" | \
    grep-P-o "(\ W | -) {11}" )
)

if  [-z " $ Songs " ] ; then 
    echo  "Nothing to do, $ Usage " 
    exit 1
 fi

# Now work with each individual song 
for ((I = 1; I <= $ {# Songs [@]} ; I + +)) 
 do 
    youtube_id = $ {Songs [$ I-1]} 
    track_number = `printf "% 0 * d " 2 $ I `
    flv_path = " $ target_folder / $ youtube_id . FLV " 
    mp3_path = " $ target_folder / $ track_number . $ youtube_id . mp3 "

    # 1. download the FLV 
    youtube-DL - Audio-format = mp3-o " $ flv_path "  "http://youtu.be/ $ youtube_id "

    if  [-F " $ flv_path " ] 
    then 
        # 2. FLV -> mp3 
        avconv-I " $ flv_path " -y " $ mp3_path " -acodec libmp3lame-AC 2-ab 128k-vn

        # 3. remove FLV 
        rm " $ flv_path " 
    fi 
Done

I hope this script is useful not only for me and will save a lot of gigabytes of RAM habrazhiteley.

 

Leave a Reply