Advertisement
Guest User

Скачиваем Youtube плейлист в формате mp3 одним bash-скриптом

a guest
Jun 13th, 2013
1,600
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.97 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. usage='usage:
  4.  ./get_youtube_playlist <playlist_id> <target_folder> <num_songs>
  5.    target_folder: (default: songs will be downloaded in current folder)
  6.    num_songs:     number of songs to get (default: 50)
  7.  
  8. examples:
  9.  ./get_youtube_playlist RD02HIkZaLeuF9k
  10.  ./get_youtube_playlist RD02HIkZaLeuF9k "instrumental hip-hop beats" 10
  11. '
  12.  
  13. playlist_id=$1
  14. target_folder=$2
  15. num_songs=$3
  16.  
  17. if [ -z "$playlist_id" ]; then
  18.     echo "$usage"
  19.     exit 1
  20. fi
  21.  
  22. if ! [[ "$num_songs" =~ ^[0-9]+$ ]] ; then
  23.     num_songs=50
  24. fi
  25.  
  26. if [ -z "$target_folder" ]; then
  27.     target_folder='./'
  28. elif [ ! -d "$target_folder" ]; then
  29.     echo "Parameter target_folder is incorrect, $usage"
  30.     exit 1
  31. fi
  32.  
  33.  
  34. # используем Youtube API для получения списка песен
  35. # https://developers.google.com/youtube/2.0/developers_guide_protocol_playlist_search
  36. youtube_api="`wget -qO- https://gdata.youtube.com/feeds/api/playlists/$playlist_id\?max-results\=$num_songs`"
  37. if [ -z "$youtube_api" ]; then
  38.     echo "Playlist ID is incorrect, $usage"
  39.     exit 1
  40. fi
  41.  
  42. # cписок ID песен помещаем в массив songs
  43. songs=(
  44.     $(echo $youtube_api | \
  45.     grep -P -o "<media:player url='.*?&" | \
  46.     grep -P -o "(\w|-){11}")
  47. )
  48.  
  49. if [ -z "$songs" ]; then
  50.     echo "Nothing to do, $usage"
  51.     exit 1
  52. fi
  53.  
  54. # теперь работаем с каждой отдельной песней
  55. for (( i = 1 ; i <= ${#songs[@]} ; i++ ))
  56. do
  57.     youtube_id=${songs[$i-1]}
  58.     track_number=`printf "%0*d" 2 $i`
  59.     flv_path="$target_folder/$youtube_id.flv"
  60.     mp3_path="$target_folder/$track_number. $youtube_id.mp3"
  61.  
  62.     # 1. скачиваем flv
  63.     youtube-dl --audio-format=mp3 -o "$flv_path" "http://youtu.be/$youtube_id"
  64.    
  65.     if [ -f "$flv_path" ]
  66.     then
  67.         # 2. flv -> mp3
  68.         avconv -i "$flv_path" -y "$mp3_path" -acodec libmp3lame -ac 2 -ab 128k -vn
  69.  
  70.         # 3. удаляем flv
  71.         rm "$flv_path"
  72.     fi
  73. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement