#!/bin/bash # Multi Youtube Get # Get multiple fils from youtube and converts them into mp3. # Requires youtube-dl and ffmpeg # FFmpeg settings. FFMPEG="-acodec libmp3lame -ab 128k" # Wget output WGOUT="/dev/stdout" TMPPLAY="/tmp/ytplaylist" if [ "$1" == "--help" ] || [ "$1" == "" ] then echo "Youtube playlist downloader." 1>&2 echo "Usage: multiytget [ output directory ]" 1>&2 echo "Usage: multiytget [ output directory ]" 1>&2 exit 2; fi OUTDIR="." if [ "$2" != "" ]; then OUTDIR="$2"; fi rm /tmp/ytlinks 2>/dev/null if [ -e "$1" ] then mv "$1" /tmp/ytlinks else page=0 while [ true ] do page=$(($page+1)); echo "Downloading playlist page $page..." wget -O /tmp/ytplaylist -o "$WGOUT" "$1&page=$page" for item in `cat /tmp/ytplaylist | grep '> /tmp/ytlinks; done; if [ "`cat $TMPPLAY | grep 'Next'`" == "" ]; then break; fi; done rm /tmp/ytplaylist fi echo "Retrived `cat /tmp/ytlinks | wc -l` videos..." for item in `cat /tmp/ytlinks` do echo "Retrieving video title ..." i=0; while [ True ] do i=$(($i+1)); title=`youtube-dl -g2 "$item" 2>/dev/null` if [ $? -eq 0 ]; then break; fi; echo "Failed... Trying again in 10 seconds..." sleep 10 if [ $i -eq 2 ]; then echo "Trying last time (this time verbose)..." title=`youtube-dl -g2 "$item"` if [ $? -eq 0 ]; then break; fi; echo "Failed 3 times. Skipping this song." echo "Failed: $item" 1>&2 continue 2; fi done title=`echo "$title" | head -n 1` echo "Title: $title" if [ -e "$OUTDIR/$title.mp3" ] then echo "Skipping (exists): $title.mp3" 1>&2 continue fi; echo "Downloading video file (HQ)..." youtube-dl -o /tmp/ytvideo -b "$item" 2>/dev/null if [ $? -eq 1 ] then echo "Trying low-qualitiy version..." youtube-dl -o /tmp/ytvideo "$item" fi; if [ $? -eq 0 ] then echo "Converting video to mp3..." ffmpeg -i /tmp/ytvideo $FFMPEG "$OUTDIR/$title.mp3" fi rm /tmp/ytvideo done; exit 0