#!/bin/bash LIST=false RETRIES="" PL_NAME="Default" LOG=1 youtubelist="/home/lucas/scripts/youtubelist.txt" MUSIC=false VIDEO=true DATE="$(date +'%m%d%H%M%S')" log="pl_log_${DATE}.log" DISABLE_SERVERMODE=false #pl_options="--playlist-end 11" pl_options="" 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; }; [[ $1 == -disable ]] && { DISABLE_SERVERMODE=true shift 1; continue; }; [[ $1 == -M ]] && { MUSIC=true VIDEO=false 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\tHelpe -M Download Music -disable\tDisable VPN\n" } get_config "$@" if [ ! -d ${DL_PATH} ]; then mkdir "${DL_PATH}" fi if [ ! -f ${youtubelist} ] ; then echo "No youtubelist present" fi if [[ "$(hostname)" == "serverhost" ]] && [[ "${DISABLE_SERVERMODE}" == "false" ]]; then duser_cmd="sudo /home/lucas/scripts/serverhost/deluge_user_cmd.sh" duser_cmd="" DL_PATH="/torrent/youtube/" echo "Serverhost mode - VPN" elif [[ "$(hostname)" == "serverhost" ]]; then duser_cmd="" DL_PATH="/torrent/youtube/" echo "Serverhost mode - No VPN" else duser_cmd="" DL_PATH="/home/lucas/Downloads/" fi echo "IP: $(${duser_cmd} wget -T 5 -qO- ifconfig.co)|$(${duser_cmd} wget -T 5 -qO- ifconfig.co/country) " count_list=1 vid_num="$(cat ${youtubelist} | wc -l | grep "")" IFS=$'\n'; set -f for VID in $(cat ${youtubelist}); do write_log "$VID" write_log "********************* VID ${count_list} of ${vid_num} *********************" if [ "$(echo ${VID} | grep "playlist")" != "" ]; then write_log "Playlist found" PL_FAIL=0 if [ ${LOG} -eq 1 ]; then write_log "Log Mode only, ${VID}" ${duser_cmd} yt-dlp ${RETRIES} -w -i "${VID}" ${pl_options} --output "${DL_PATH}%(uploader)s/%(playlist)s/%(upload_date)s_%(title)s.%(ext)s" --merge-output-format mp4 --remux-video mp4 fi write_log "Logfile ${log}" pl_name="$(printf "%s" "${VID}" | rev | cut -f1 -d'=' | rev)" log_name="${DL_PATH}pl_log_${pl_name}.log" echo $pl_name echo $log_name if [ ! -f ${log_name} ]; then write_log "New Playlist File ${log_name}" if ${MUSIC}; then write_log "Download Music File" ${duser_cmd} yt-dlp -j --flat-playlist "${VID}" ${pl_options} | cut -f4 -d',' | cut -f2- -d':' | tr -d ' "' > ${log_name} else ${duser_cmd} yt-dlp -j --flat-playlist "${VID}" ${pl_options} | cut -f4 -d',' | cut -f2- -d':' | tr -d ' "' > ${log_name} fi else write_log "${log_name} already present" fi while [ $(cat ${log_name} | wc -l) -ne 0 ] ; do count_playlist=0 count_playlist_fail=0 count_playlist_suc=0 lines=$(cat ${log_name} | wc -l) write_log "${lines} files to download" for line in $(cat ${log_name}); do if ${MUSIC}; then write_log "Download Music File" ${duser_cmd} yt-dlp ${RETRIES} "${line}" -f bestaudio -x --audio-format mp3 --audio-quality 320k --embed-thumbnail --add-metadata --postprocessor-args "-id3v2_version 3" --output "${DL_PATH}%(uploader)s/%(playlist)s_${pl_name}/%(title)s.%(ext)s" else ${duser_cmd} yt-dlp ${RETRIES} -w -i "${line}" "${vid_options}" --output "${DL_PATH}%(uploader)s/%(playlist)s_${pl_name}/%(upload_date)s_%(title)s.%(ext)s" --merge-output-format mp4 --remux-video mp4 fi ret=$? if [ ${ret} -eq 0 ]; then ((count_playlist_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_playlist_fail++)) fi ((count_playlist++)) write_log "${count_playlist}/${lines} : ${line} : ${ret} - Sucess: ${count_playlist_suc} : Fail: ${count_playlist_fail}" done if [[ ${PL_FAIL} -gt 5 ]];then break fi ((PL_FAIL++)) echo "Increase PL_FAIL to ${PL_FAIL}" done else if [ "$(echo ${VID} | grep youtube)" != "" ]; then write_log "YouTube Video found" ${duser_cmd} yt-dlp ${RETRIES} -w -i "${VID}" ${pl_options} --output "${DL_PATH}%(uploader)s/%(playlist)s/%(upload_date)s_%(title)s.%(ext)s" --merge-output-format mkv else write_log "General Video found" $(${duser_cmd}) yt-dlp -w -i "${VID}" --output "${DL_PATH}%(title)s_${count_list}.%(ext)s" --force-ipv4 --merge-output-format mp4 --remux-video mp4 echo "${duser_cmd} yt-dlp ${RETRIES} -w -i ${VID} --output ${DL_PATH}%(title)s_${count_list}.%(ext)s --force-ipv4 --merge-output-format mp4 --remux-video mp4" fi fi ((count_list++)) done