Advertisement
Guest User

YouTube-DL Wrapper Script

a guest
Apr 4th, 2020
585
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 12.93 KB | None | 0 0
  1. youtube-dl wrapper script
  2. =================================
  3. #!/bin/bash
  4. #requires .env: YTU_DEST
  5. #  YTU_DEST=~/Videos or similar
  6. # YTU_DEST=/home/uc/Videos/ytuW
  7. # YTU_LOG=ytu.log
  8. # YTU_LIST=ytulist.txt
  9. #  ie:
  10. #  YTU_DEST=/home/uc/Downloads/ytu
  11. #
  12. # https://askubuntu.com/questions/438376/how-to-download-all-videos-on-a-youtube-channel
  13. # https://github.com/ytdl-org/youtube-dl/blob/master/README.md#options
  14.  
  15. set_media_type () {
  16.     echo "---------------------------------";
  17.     echo "Current media type: $OPT_MEDIATYPE";
  18.     echo "  -----  Set media type ------   ";
  19.     echo "---------------------------------";
  20.     echo "V - VIDEO";
  21.     echo "A - AUDIO";
  22.     read -p "Select Option...: " OPT_AV
  23.     echo "---------------------------------";
  24.     echo "Selected $OPT_AV"
  25.  
  26.     # get video unless audio selected
  27.     if [ "$OPT_AV" = "A" -o  "$OPT_AV" = "a" ] ; then
  28.         OPT_MEDIATYPE="-f best -x --audio-format "mp3"  --audio-quality 0 --embed-thumbnail";
  29.         #OPT_MEDIATYPE="-f 'bestaudio[ext=mp3]/bestaudio[ext=m4a]/bestaudio[ext=webm]/bestaudio'";
  30.         OPT_ARCHIVE_FILE=$OPT_ARCHIVE_FILE_AUDIO;
  31.     else
  32.         OPT_MEDIATYPE="-f best";
  33.     fi
  34.     echo "Assigned Media Type: $OPT_MEDIATYPE";
  35.    
  36.     # output status before proceding
  37.     #echo "---------------------------------";
  38.     #echo "Current media type: ${OPT_MEDIATYPE}";
  39.     #echo "Current URL: $URL -- $1"
  40.     #echo "Current DATEAFTER: $OPTS_DATEAFTER -- $2"
  41.     #echo "---------------------------------";
  42.    
  43. }
  44.  
  45. set_job_type () {
  46. #   echo "---------------------------------------------------";
  47. #   echo "URL is       : $URL";
  48. #   echo "DATEAFTER is : $OPTS_DATEAFTER";
  49. #   echo "   (OPTIONAL YYYYMMDD - example: 20191231 ) ";
  50.     echo "----------------------------------------------------";
  51.     echo "----------- SELECT JOB TYPE ------------------------";
  52.     echo "----------------------------------------------------";
  53.     echo "1 - ONE URL   NOT ALL PLAYLIST";  # https://www.youtube.com/watch?v=hECs8372M8A
  54.     echo "2 - PLAYLIST      ALL BY SINGLE URL -  ( get entire playlist )";
  55.     echo "----------------------------------------------------";
  56.     echo "3 - PLAYLIST PAGE ALL";
  57.     echo "4 - VIDEOS   PAGE ALL";
  58.     echo "5 - CHANNEL       ALL";
  59.     echo "---------------------------------------------------";
  60.     echo "6 - Read LIST                   - of SINGLE URLS from list $YTU_LIST";
  61.     echo "7 - View LOG:                   - $YTU_LOG";
  62.     echo "8 - READ URL and DATEAFTER";  
  63.     echo "----------------------------------------------------";
  64.     echo " 0 - EXIT";
  65.     read -p "Select option and press ENTER or CTRL-C to ABORT:  " OPTS
  66.     OPT_JOB_TYPE="$OPTS"
  67.     echo "----------------------------------------------------"
  68.     echo "----------------------------------------------------"
  69.     echo " ENTERED  : $OPT_JOB_TYPE";  
  70.     #echo " TARGET URL: $URL";
  71.     #echo " DATEAFTER SET TO: _ $OPTS_DATEAFTER _";
  72.     #echo "----------------------------------------------------";  
  73.    
  74. }
  75.  
  76. set_list_option () {
  77.     echo "URL: $URL";
  78.     if [ "$URL" != "" ] ; then
  79.         OPT_USE_LIST="N";
  80.     else
  81.         read_list_option
  82.     fi
  83.    
  84. }
  85.  
  86.  
  87. read_list_option () {
  88.     echo "---------------------------------";
  89.     echo "Current List Setting : $OPT_USE_LIST";
  90.     echo "Current List Location: $YTU_LIST";
  91.     echo "-----  READ from LIST? Y/N ------ ";
  92.     echo "---------------------------------";
  93.     read -p "Select Option...: " RESP
  94.     echo "Selected:  $RESP"
  95.     echo "---------------------------------";
  96.     # get video unless audio selected
  97.     if [ "$RESP" = "Y" -o  "$RESP" = "y" ] ; then
  98.         OPT_USE_LIST="Y";
  99.     else
  100.         OPT_USE_LIST="N";
  101.         set_url_date
  102.     fi
  103.    
  104. }
  105.  
  106.  
  107.  
  108. set_url_date () {
  109.  
  110.     echo "Read URL and DATEAFTER"
  111.     read -p "Type...>" URL RESP
  112.    
  113.     if [ "$RESP" = "" ] ; then
  114.         OPTS_DATEAFTER=
  115.     else
  116.         OPTS_DATEAFTER="--dateafter ${RESP}"
  117.     fi
  118.    
  119.     #if [ "$URL" = "" ] ; then
  120.     #   echo "URL is empty, retry.."
  121.     #   set_url_date
  122.     #fi
  123.  
  124.  
  125. # give a change to enter URL if empty
  126. #if [ "$URL" = "" ]; then
  127. #   set_url_date
  128. #fi
  129.  
  130.  
  131.        
  132. }
  133.  
  134. ytuExecute() {
  135.    
  136.     case "$OPT_JOB_TYPE" in
  137.     "1") echo "Geting single URL ( dont get entire playlist )"; # https://www.youtube.com/watch?v=hECs8372M8A
  138.         OPT_PLAYLIST_YESNO="$OPT_PLAYLIST_NO";
  139.         if [ "$OPT_AV" = "A" -o  "$OPT_AV" = "a" ] ; then
  140.             OPT_OUTPUT_FILE="$OPT_OUTPUT_FILE_SINGLE_AUDIO"
  141.         else
  142.             OPT_OUTPUT_FILE="$OPT_OUTPUT_FILE_SINGLE_VIDEO"
  143.         fi
  144.         OPT_JOB_SPECIFIC=
  145.        
  146.        
  147.         run_command #"$URL" "$OPTS_DATEAFTER"
  148.         ;;
  149.     "2") echo "Geting single URL ( get entire playlist )";      # https://www.youtube.com/watch?v=QYrjvLFT_B4&list=PLUjQ3tZwzZPG3tA6BjnjhAJVnZ8n4smUQ&index=4
  150.         OPT_PLAYLIST_YESNO="$OPT_PLAYLIST_YES";
  151.         if [ "$OPT_AV" = "A" -o  "$OPT_AV" = "a" ] ; then
  152.             OPT_OUTPUT_FILE="$OPT_OUTPUT_FILE_PLAYLIST_AUDIO"
  153.         else
  154.             OPT_OUTPUT_FILE="$OPT_OUTPUT_FILE_PLAYLIST_VIDEO"
  155.         fi
  156.         OPT_JOB_SPECIFIC=
  157.        
  158.         run_command
  159.         ;;     
  160.     "3") echo "Geting playlist by URL";     # https://www.youtube.com/playlist?list=PLUjQ3tZwzZPG3tA6BjnjhAJVnZ8n4smUQ
  161.         OPT_PLAYLIST_YESNO="$OPT_PLAYLIST_YES";
  162.        
  163.         if [ "$OPT_AV" = "A" -o  "$OPT_AV" = "a" ] ; then
  164.             OPT_OUTPUT_FILE="$OPT_OUTPUT_FILE_PLAYLIST_AUDIO"
  165.         else
  166.             OPT_OUTPUT_FILE="$OPT_OUTPUT_FILE_PLAYLIST_VIDEO"
  167.         fi
  168.         OPT_JOB_SPECIFIC=
  169.        
  170.  
  171.         run_command "$URL"
  172.         ;;
  173.     "4") echo "Geting all videos by URL";   # https://www.youtube.com/channel/UC8butISFwT-Wl7EV0hUK0BQ/videos
  174.         OPT_PLAYLIST_YESNO="$OPT_PLAYLIST_NO";
  175.         if [ "$OPT_AV" = "A" -o  "$OPT_AV" = "a" ] ; then
  176.             OPT_OUTPUT_FILE="$OPT_OUTPUT_FILE_SINGLE_AUDIO"
  177.         else
  178.             OPT_OUTPUT_FILE="$OPT_OUTPUT_FILE_SINGLE_VIDEO"
  179.         fi
  180.         OPT_JOB_SPECIFIC=
  181.        
  182.        
  183.         run_command "$URL"
  184.         ;;
  185.     "5") echo "Geting entire channel by URL";  # https://www.youtube.com/channel/UCIJGI_3XgnfUaSNQD8D2IMQ
  186.         OPT_PLAYLIST_YESNO="$OPT_PLAYLIST_NO";
  187.         if [ "$OPT_AV" = "A" -o  "$OPT_AV" = "a" ] ; then
  188.             OPT_OUTPUT_FILE="$OPT_OUTPUT_FILE_SINGLE_AUDIO"
  189.         else
  190.             OPT_OUTPUT_FILE="$OPT_OUTPUT_FILE_SINGLE_VIDEO"
  191.         fi
  192.         OPT_JOB_SPECIFIC=
  193.        
  194.        
  195.         run_command "$URL"
  196.         ;;
  197.  
  198.  
  199.  
  200.     "6") echo "Getting download urls from list: $YTU_LIST";
  201.         OPT_PLAYLIST_YESNO="$OPT_PLAYLIST_NO";
  202.         if [ "$OPT_AV" = "A" -o  "$OPT_AV" = "a" ] ; then
  203.             OPT_OUTPUT_FILE="$OPT_OUTPUT_FILE_SINGLE_AUDIO"
  204.         else
  205.             OPT_OUTPUT_FILE="$OPT_OUTPUT_FILE_SINGLE_VIDEO"
  206.         fi
  207.         OPT_JOB_SPECIFIC="$OPT_BATCH_FILE"
  208.  
  209.  
  210.         run_command "$URL"
  211.         ;;
  212.     "7")
  213.         echo "-------------------------------------------------"
  214.         echo "REVIEW LOGFILE: ${YTU_LOG}"
  215.         echo "Press ENTER or CTRL-C to ABORT"
  216.         echo "-------------------------------------------------"
  217.         read -p ">" pausing
  218.  
  219.         OPT_JOB_SPECIFIC=
  220.  
  221.         viewlog
  222.         ;;
  223.     "8") echo "Read URL and DATEAFTER"
  224.         read -p "Type...>" URL RESP
  225.        
  226.         if [ "$RESP" = "" ] ; then
  227.             OPTS_DATEAFTER=
  228.         else
  229.             OPTS_DATEAFTER="--dateafter ${RESP}"
  230.         fi
  231.        
  232.         if [ "$URL" = "" ] ; then
  233.             echo "URL is empty, exiting.."
  234.             exit 1
  235.         fi
  236.         OPT_JOB_SPECIFIC=
  237.        
  238.         run_command "${URL}"
  239.         ;;
  240.     "0") exit 0;
  241.         ;;
  242.     *)
  243.         ;;
  244.     esac;
  245.    
  246.     # write DONE msg to log
  247.     annotate_file_dldone
  248.     set_list_option
  249.    
  250. }
  251.  
  252. function exiting () {
  253.        
  254.     echo "DONE, EXITING NOW.....";
  255.     exiting
  256. }
  257.  
  258. run_command () {
  259.     read -p "READY TO START???? Press ENTER or CTRL-C to ABORT" pausing
  260.     OPTS_STATIC="$OPT_CONSOLE_TITLE $OPT_VERBOSE $OPT_MEDIATYPE  $OPT_WRITETHUMBNAIL $OPT_WRITE_DESCRIPTION $OPT_WRITE_INFO_JSON $OPT_IGNORE_ERRORS $OPT_NO_OVERWRITES $OPT_RESTRICT_FILENAMES $OPT_ARCHIVE_FILE";
  261.     OPTS_DYNAMIC="$OPT_JOB_SPECIFIC $OPT_OUTPUT_FILE $OPTS_DATEAFTER";
  262.    
  263.     if [ "$OPT_USE_LIST" = "Y" -o "$OPT_USE_LIST" = "y" ] ; then
  264.         echo "updating option string: BEFORE: $OPTS_DYNAMIC"
  265.         OPTS_DYNAMIC="$OPTS_DYNAMIC $OPT_BATCH_FILE"
  266.         echo "updating option string: AFTER : $OPTS_DYNAMIC"
  267.     else
  268.         echo "NOT using list"
  269.     fi
  270.  
  271.     # echo "RUNNING: youtube-dl $OPTS_ALL $OPTS_JOB $URL";
  272.     CMD="youtube-dl $OPTS_STATIC $OPTS_DYNAMIC $URL"
  273.     echo "COMMAND STRING: $CMD"
  274.     $CMD >> ytu.log 2>&1 & tail -f ytu.log
  275.  
  276. }
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283. #run_command () {  # $URL, $OPTS_ALL, $OPTS_JOB
  284. #   OPTS_ALL="--console-title --verbose -f $OPT_MEDIATYPE --write-thumbnail --write-description --write-info-json --ignore-errors --no-overwrites --restrict-filenames $OPTS_DATEAFTER";
  285. #
  286. #   # echo "RUNNING: youtube-dl $OPTS_ALL $OPTS_JOB $URL";
  287. #   CMD="youtube-dl $OPTS_ALL  $OPTS_JOB $OPTS_DATEAFTER $URL"
  288. #   echo "COMMAND STRING: $CMD"
  289. #   $CMD
  290. #}
  291.  
  292. exiting () {
  293.     echo "EXITING.....";
  294.     exit 0;
  295.    
  296. }
  297.  
  298.  
  299.  
  300. annotate_file_dldone () {
  301.     local MESSAGE="
  302.  
  303.  
  304. ############################################################################
  305. ####    DOWNLOADS COMPLETE
  306. ####    ${JOB} ON : ${TIMESTAMP}
  307. ############################################################################
  308.  
  309.  
  310. "
  311.     echo "$MESSAGE"  >> ytu.log
  312.    
  313. }
  314.  
  315.  
  316. viewlog () {
  317.     less ${YTU_LOG}
  318.     echo "EXITING.....";
  319.    
  320. }
  321.  
  322. move_mp3 () {
  323.     # NOT NEEDED, UNLESS THE FILENAME CONTAINS SPACES & SPECIALS CHARS THAT INDUCE THE '<FILENAME>' QUOTE WRAPPING BY LINUX OS
  324.     if [ "$OPT_AV" = "A" -o  "$OPT_AV" = "a" ] ; then
  325.         mv *.mp3 "$YTU_DEST_MP3"
  326.     fi
  327.    
  328. }
  329.  
  330.  
  331. # https://www.youtube.com/watch?v=nO9N0TgAblE
  332. echo "######################### INIT  #########################################";
  333. # export .env vars & clear screen
  334. export $(grep -v '^#' /home/uc/s/.env | xargs)
  335. #export $(grep -v '^#' /home/silosix/s/.env | xargs)
  336. clear
  337.  
  338. URL="$1";
  339. echo "URL PASSED: _ $URL _";
  340.  
  341.  
  342.  
  343. # set date if entered & valid ( add len check )
  344. if [ "$2" != "" ]; then
  345.     OPTS_DATEAFTER="--dateafter $2"; #YYYYMMDD OR now/#days/#months/#years
  346. else
  347.     OPTS_DATEAFTER=      # ASSIGN TO EMPTY SO IT WON'T BE USED
  348. fi
  349.  
  350. echo "------------------------"
  351. echo "PASSED PARAMS: "
  352. echo "------------------------"
  353. echo "1-URL           : $URL"
  354. echo "2-OPTS_DATEAFTER: $OPTS_DATEAFTER"
  355. echo "------------------------"
  356.  
  357. echo "######################### CHECK FOR VALID DIRS  #########################################";
  358. # init YTU_DEST, YTU_LOG, YTU_LIST
  359. if [ -z "$YTU_DEST" ] ; then
  360.     echo "Current Destination Directory is empty: $YTU_DEST"
  361.     YTU_DEST="~/Videos"
  362.     echo "using the default dir: $YTU_DEST"
  363. fi
  364.  
  365. if [ ! -d "$YTU_DEST" ] ; then
  366.     echo "Current Destination  Directory is invalid: $YTU_DEST"
  367.     YTU_DEST="~/Videos"
  368.     echo "using the default dir: $YTU_DEST"
  369. fi
  370.  
  371. if [ ! -d "$YTU_DEST" ] ; then
  372.     echo "Default Destination dir is invalid also exiting: $YTU_DEST"
  373.     exit 1;
  374. fi
  375.  
  376. if [ ! -z  "$YTU_LOG" ] ; then
  377.     YTU_LOG="$YTU_DEST/ytu.log"
  378.     touch "$YTU_LOG"
  379. fi
  380.  
  381.  
  382. # not needed if filename is 'cleaned' with correct youtube-dl option
  383. YTU_DEST_MP3="$YTU_DEST/mp3"
  384.  
  385.  
  386. # check /media/media/ytu/ytu-list.txt.. THEN use local as backup
  387. if [ -z "$YTU_DEST/ytu-list.txt" ] ; then
  388.     YTU_LIST="$YTU_DEST/ytu-list.txt"
  389. else
  390.     YTU_LIST="$dir_git/ytu/ytu-list.txt"   
  391. fi
  392.  
  393.  
  394. echo "------------------------"
  395. echo ".CONFIG PARAMS in .env";
  396. echo "------------------------"
  397. echo "SCRIPT_DIR: $SCRIPT_DIR"
  398. echo "YTU_DEST  : $YTU_DEST"
  399. echo "YTU_LOG   : $YTU_LOG"
  400. echo "YTU_LIST  : $YTU_LIST"
  401. echo "------------------------"
  402.  
  403.  
  404. echo "######################### STATIC VARS #########################################";
  405.  
  406. # STATIC VARS - ALWAYS USED
  407. OPT_CONSOLE_TITLE="--console-title"
  408. OPT_VERBOSE="--verbose"
  409. OPT_WRITETHUMBNAIL="--write-thumbnail"
  410. OPT_WRITE_DESCRIPTION="--write-description"
  411. OPT_WRITE_INFO_JSON="--write-info-json"
  412. OPT_IGNORE_ERRORS="--ignore-errors"
  413. OPT_NO_OVERWRITES="--no-overwrites"
  414. OPT_RESTRICT_FILENAMES="--restrict-filenames"
  415. OPT_ARCHIVE_FILE_VIDEO="--download-archive archive-video.txt"
  416. OPT_ARCHIVE_FILE_AUDIO="--download-archive archive-audio.txt"
  417. OPT_ARCHIVE_FILE="$OPT_ARCHIVE_FILE_VIDEO"
  418. OPT_BATCH_FILE="--batch-file $YTU_LIST"
  419.  
  420.  
  421. echo "######################### DYNAMIC VARS ########################################";
  422. # DYNAMIC VARS
  423. OPT_JOB_TYPE=
  424. OPT_PLAYLIST_YESNO=
  425. OPT_AV="V"
  426. OPT_MEDIATYPE="-f best"
  427. OPT_OUTPUT_FILE=
  428. OPT_JOB_SPECIFIC=
  429. OPT_USE_LIST="N"
  430.  
  431. # set in function()
  432. OPT_PLAYLIST_NO="--no-playlist";
  433. OPT_OUTPUT_FILE_SINGLE_VIDEO="--output $YTU_DEST/%(uploader)s/%(title)s.%(ext)s";
  434. OPT_OUTPUT_FILE_SINGLE_AUDIO="--output $YTU_DEST/%(uploader)s/audio/%(title)s-audio.%(ext)s";
  435.  
  436. OPT_PLAYLIST_YES="--yes-playlist"
  437. OPT_OUTPUT_FILE_PLAYLIST_VIDEO="--output $YTU_DEST/%(uploader)s/%(playlist)s/%(playlist_index)s-%(title)s.%(ext)s";
  438. OPT_OUTPUT_FILE_PLAYLIST_AUDIO="--output $YTU_DEST/audio/%(uploader)s/%(playlist)s/%(playlist_index)s-%(title)s.%(ext)s";
  439.  
  440.  
  441. # AGGREGATE ALL VARS TO THESE THEN CALL YTDL
  442. OPTS_DYNAMIC=
  443. OPTS_ALL=  
  444.  
  445. #  YOUTUBE-DL OPTION INIT PER JOB
  446. JOB_SINGLEPLAYLIST_NO=
  447. JOB_SINGLEPLAYLIST_YES=
  448. JOB_PLAYLIST_ALL=
  449. JOB_VIDEOS_ALL=
  450. JOB_CHANNEL_ALL=
  451.    
  452. echo "######################### CALL #####################################################";
  453. # 1ST PARAM = URL
  454. # 2ND PARAM = DATEAFTER ( 20191231 )
  455.  
  456.  
  457. set_list_option
  458.  
  459.  
  460. set_media_type "$URL" $2
  461. set_job_type
  462. ytuExecute
  463.  
  464.  
  465. echo "######################### END #####################################################";
  466. exit 0;
  467.  
  468. # SPARE OPTIONS
  469. # --config-location PATH
  470. # --playlist-reverse
  471. # --download-archive FILE
  472. # --batch-file FILE
  473. # --console-title
  474.  
  475.  
  476.  
  477. #"$OPT_CONSOLE_TITLE"
  478. #"$OPT_VERBOSE"
  479. #"$OPT_MEDIATYPE"        
  480. #"$OPT_WRITETHUMBNAIL"
  481. #"$OPT_WRITE_DESCRIPTION"
  482. #"$OPT_WRITE_INFO_JSON"
  483. #"$OPT_IGNORE_ERRORS"
  484. #"$OPT_NO_OVERWRITES"
  485. #"$OPT_RESTRICT_FILENAMES"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement