kuasha420

youtube-dl script

Nov 7th, 2013
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.83 KB | None | 0 0
  1. #!/bin/bash
  2. echo "This is a simple (and low quality) script to download all videos from"
  3. echo youtube playlist and convert them to mp3. it use youtube-dl and ffmpeg
  4. echo binaries to achieve this. To use this you will need ID of a YouTube
  5. echo playlist and internet connection to download videos. This script will
  6. echo need autnentication via sudo once for installing or updating ffmpeg
  7. echo in the following steps it will ask for some info like playlisy id.
  8. echo working directory, prefered video formats etc. There is no default so
  9. echo you must answer all questions in the following steps!
  10. echo This script for now works only with ubuntu based Operating system!
  11. echo
  12. echo ------------------------------------------------------------------------
  13. echo
  14. echo Enter the playlist ID
  15. echo If your playlist url is http://www.youtube.com/playlist?list=PLpsNuha2_QyaGF2AX813SJP9Tqj4rjxls
  16. echo then playlist id is PLpsNuha2_QyaGF2AX813SJP9Tqj4rjxls
  17. echo example playlist id: PLpsNuha2_QyaGF2AX813SJP9Tqj4rjxls
  18. read PLAYLIST
  19. PLAYLISTURL=https://www.youtube.com/view_play_list?p=$PLAYLIST
  20. echo Ok, Playlist is $PLAYLISTURL
  21. echo Enter directory to use
  22. echo you can use relative directory like Videos or absulute path like /home/user/youtube-videos
  23. echo you do not need to create the directory manually.
  24. echo Script will create the folder automatically if not exist.
  25. echo IF YOU ARE USING EXISTING FOLDER IS SHOULD BE EMPTY!  
  26. echo example relative directory: Videos
  27. echo example relative directory: youtube/Videos
  28. echo example absulute path: /home/user/youtube/Videos
  29. read DIRT
  30. echo Ok, directory is $DIRT
  31. echo Checking if your prefered directory exist and creating automatically if not exist
  32. mkdir -p $DIRT
  33. echo Changing to working directory
  34. cd $DIRT
  35. echo please select your prefered video quality
  36. echo higher quality will result higher file size and take longer to download
  37. echo For your referance here is some common code for quality
  38. echo you can see all supported codes at https://en.wikipedia.org/wiki/YouTube#Quality_and_codecs
  39. echo
  40. echo "144p/3gp:  17 (Very Low Quality)"
  41. echo "240p/3gp:  36 (Low Quality)"
  42. echo "240p/flv:  5  (Low Quality)"
  43. echo "360p/mp4:  18 (Medium Quality) - Prefered For most videos"
  44. echo "480p/flv:  35 (High Quality)"
  45. echo "720p/mp4:  22 (High Quality HD resulation) - Prefered for Screen presentation, video tutorials"
  46. echo "1080p/mp4: 37 (Very High Quality FullHD resulation)"
  47. echo
  48. echo Input the appropriate code below
  49. echo for best results use mp4 formats
  50. echo you can prefer  two or three types at once.
  51. echo If first one is not avaulable next one will be used and so on
  52. echo "just make sure you are using same container/file format (mp4 or flv)"
  53. echo example of single code 18
  54. echo example of multiple preferance: 37/22/18
  55. read Q
  56. echo Please input file format of that code
  57. echo Your file format must match with quality code.
  58. echo for instance, if you chose code 18 your file time is mp4
  59. echo acceptable values mp4 flv 3gp
  60. echo example input: mp4
  61. read T
  62. echo "Ok, your selected quality code(s): $Q and file format: $T"
  63. echo please set a bitrate for converted audio/mp3 files
  64. echo higher bitrate will result better quality and larger file size
  65. echo you should input bitrate as a number and k in the end
  66. echo here are some most common example
  67. echo 24k 32k 64k 96k 128k 192k 256k
  68. echo only one is allowed
  69. echo example input 128k
  70. read AQ
  71. echo ok, your selected audio bitrate for converted mp3 is $AQ
  72. echo this script needs youtube-dl and ffmpeg binaries to work.
  73. echo checking if they are installed
  74. echo if they are not installed script will try to install them automatically
  75. echo this will also upgrade outdated binary if needed
  76. echo you may need to authenticate
  77. echo this may take some time
  78. # latest version of youtube-dl is important and generally
  79. # not available in distributions repository. So this check
  80. # will add addiional ppa for it if needed.
  81. if [ "$(which youtube-dl)" = '' ]; then
  82. echo youtube-dl binary not found. To install latest version of
  83. echo youtube-dl we will add webupd8 ppa
  84. echo please hit enter when asked to add this ppa and continue
  85. sudo add-apt-repository ppa:nilarimogard/webupd8
  86. fi
  87. # Even if system has youtube-dl and ffmpeg installed this
  88. # process will make sure they are latest version and/or
  89. # update them if needed.
  90. sudo apt-get -qq update
  91. sudo apt-get -y -qq install youtube-dl ffmpeg
  92. echo all required binaries are present, installed or upgraded as needed
  93. echo We are good to go!
  94. echo Have some coffee while we finish downloading and converting.
  95. echo Downloading videos with youtube-dl binary
  96. echo this will take some time according to your quality selection and internet speed!
  97. youtube-dl -citA -f $Q $PLAYLISTURL
  98. echo All videos have been downloaded.
  99. echo If there was any problem with one or two videos are were ignored
  100. echo Download complete!
  101. echo Converting to mp3 using ffmpeg binary
  102. echo this will take some time according to your processor power!
  103. for F in *.$T
  104. do
  105. ffmpeg -i "$F" -acodec libmp3lame -ab $AQ "${F%.$T}.mp3"
  106. echo all files have been converted.
  107. echo If there was any problem with one or two videos are were ignored
  108. echo Conversation complete!
  109. echo we will move mp3 files to a folder called mp3 inside your selected directory
  110. echo for easier access and seperation between video and audio files
  111. echo Creating mp3 directory to move mp3 files
  112. mkdir -p mp3
  113. echo Moving all mp3 files to mp3 folder
  114. mv -f -t mp3 *.mp3
  115. echo File moving complete!
  116. echo Everything done thanks to powerful youtube-dl, ffmpeg and bash!
  117. echo Thank you very much for using this script!
  118. echo You will find all mp4/video files in your specified $DIRT folder.
  119. echo You will find all mp3/audio files in mp3 folder inside your specified $DIRT folder.
  120. echo If you have any question, feedback or suggestion please email me at kuasha420@gmail.com
  121. echo Bye!
  122. done
Advertisement
Add Comment
Please, Sign In to add comment