#!/bin/bash LIST=false RETRIES="" PL_NAME="" LOG=1 DL_PATH="/home/lucas/Videos/YouTube/" if [[ "$(hostname)" == "serverhost" ]]; then DL_PATH="/OwnCloud/Torrents/Complete/" fi PLAYLISTS=() VIDEOS=("") DATE="$(date +'%m%d%H%M%S')" log="pl_log_${DATE}.log" pl_options="" #pl_options="--playlist-end 11" vid_options="" get_config() { while [[ ${1:0:1} == - ]]; do echo "Param: $1" [[ $1 == -retry ]] && { echo "Retriy Mode" RETRIES="--retries 100 --fragment-retries 100" shift 1; continue; }; [[ $1 == -log ]] && { echo "Playlist Log Mode" LOG=0 shift 1; continue; }; [[ $1 == -pl_name ]] && { if [ $# -lt 2 ]; then usage exit fi echo "Playlist Dir Name: $2" PL_NAME=$2; shift 2; continue; }; [[ $1 == -h ]] && { usage shift 1; exit 0; }; [[ $1 == -L ]] && { LIST=true shift 1; continue; }; echo "Parameter $1 discarded" shift 1; done } write_log() { echo "$@" echo "$@" >> "${DL_PATH}${log}" } usage(){ printf "Usage: -pl_name \tGive Playlist name for -log mode -retry\t\tUse more Retries -log\t\tDownload Playlist Video Titles to Log File -h\t\tHelp -L\t\tDowonload from FIle\n" } get_config "$@" if [ "$(hostname)" == "serverhost" ]; then DL_PATH="/OwnCloud/Torrents/Complete/" elif [ ! -d ${DL_PATH} ]; then mkdir "${DL_PATH}" fi if [ ${LOG} -eq 1 ]; then for PL in ${PLAYLISTS[@]}; do echo $PL youtube-dl ${RETRIES} -w -i "${PL}" ${pl_options} --output "${DL_PATH}%(uploader)s/%(playlist)s/%(upload_date)s_%(title)s.%(ext)s" done else write_log "Logfile ${log}" if [[ "${PL_NAME}" == "" ]]; then write_log "Playlist folder in -pl_name missing"; usage; exit 0; fi for PL in ${PLAYLISTS[@]}; do log_name="${DL_PATH}pl_log_$(echo "${PL}" | rev | cut -f1 -d'=' | rev).log" if [ ! -f ${log_name} ]; then write_log "New Playlist File ${log_name}" youtube-dl -j --flat-playlist "${PL}" ${pl_options} | cut -f4 -d',' | cut -f2 -d':' | tr -d ' "' | sed 's_^_https://youtu.be/_' > ${log_name} else write_log "${log_name} already present" fi while [ $(cat ${log_name} | wc -l) -ne 0 ] ; do count=0 count_fail=0 count_suc=0 lines=$(cat ${log_name} | wc -l) write_log "${lines} files to download" for line in $(cat ${log_name}); do youtube-dl ${RETRIES} -w -i "${line}" "${vid_options}" --output "${DL_PATH}%(uploader)s/${PL_NAME}/%(title)s_%(id)s.%(ext)s" ret=$? if [ ${ret} -eq 0 ]; then ((count_suc++)) write_log "Remove ${line} from file ${log_name}" cat ${log_name} | tr '\n' ';' | sed 's#'"${line}"';##g' | tr ';' '\n' > buf mv buf ${log_name} else ((count_fail++)) fi ((count++)) write_log "${count}/${lines} : ${line} : ${ret} - Sucess: ${count_suc} : Fail: ${count_fail}" done done done fi if [[ "${LIST}" == "true" ]]; then if [ ! -f /data/youtubelist.txt ] ; then echo "No youtubelist present" fi count=1 vid_num="$(cat /data/youtubelist.txt | wc -l | grep "")" IFS=$'\n'; set -f for VID in $(cat /data/youtubelist.txt); do echo $VID echo "********************* VID ${count} of ${vid_num} *********************" if [ "$(echo ${VID} | grep youtube)" != "" ]; then youtube-dl ${RETRIES} -w -i "${VID}" ${pl_options} --output "${DL_PATH}%(uploader)s/%(playlist)s/%(upload_date)s_%(title)s.%(ext)s" else youtube-dl ${RETRIES} -w -i "${VID}" --output "${DL_PATH}%(title)s_${count}.%(ext)s" --force-ipv4 fi ((count++)) done fi count=1 for VID in ${VIDEOS[@]}; do echo "Video: $VID" if [ "$(echo ${VID} | grep youtube)" != "" ]; then youtube-dl ${RETRIES} -w -i "${VID}" --output "${DL_PATH}%(uploader)s/%(title)s.%(ext)s" --force-ipv4 else youtube-dl ${RETRIES} -w -i "${VID}" --output "${DL_PATH}%(title)s_${count}.%(ext)s" --force-ipv4 el fi ((count++)) done