首页 > 解决方案 > youtube-dl 下载脚本调试

问题描述

转储更新这篇文章的非常糟糕的想法。此脚本的新位置: https ://gist.github.com/Wogol/66e9936b6d49cc5fecca59eaeca1ca2e


我试图创建一个使用简单易用的 Youtube-dl 的 .command macOS 脚本(也应该在 GNU/Linux 下工作)。我已修复,因此它会下载描述、缩略图、字幕、json、创建文件夹结构并保存视频 ID、上传者和上传日期。


主脚本的问题:


信息文件的问题:

还创建一个包含标题、频道名称、发布日期、描述的信息文件。我现在正在努力从 .json 和 youtube-dl 获取视频信息以导出到 info.txt 文件中。

    ytdl.command: line 104: ~/Downloads/ytdl/dog_vids/info.txt: No such file or directory
   youtube-dl --simulate --SHOW_THE_OUTPUT_PATH -o $folder'/%(title)s/%(title)s - (%(id)s) - %(uploader)s - %(upload_date)s.%(ext)s' https://www.youtube.com/watch?v=dQw4w9WgXcQ
    "tags": ["music", "video", "classic"]

当前版本试图让它工作

(8 月 12 日)

    textfile=""$folder"info.txt"
    
    echo TITLE >> ~/Downloads/ytdl/dog_vids/info.txt
    youtube-dl -e $url >> ~/Downloads/ytdl/dog_vids/info.txt
    echo \ >> ~/Downloads/ytdl/dog_vids/info.txt
    
    echo CHANNEL >> $textfile
    echo \ >> $textfile
    
    echo CHANNEL URL >> $textfile
    echo \ >> $textfile
    
    echo UPLOAD DATE >> $textfile
    echo \ >> $textfile
    
    echo URL >> $textfile
    echo $url >> $textfile
    echo \ >> $textfile
    
    echo TAGS >> $textfile
    echo \ >> $textfile
    
    echo DESCRIPTION >> $textfile
    youtube-dl --get-description $url >> $textfile

实验未来版本 - 从 JSON 文件中提取信息

这不是一个工作脚本。用 $textfile、$ytdlfolder 和 $jsonfile 展示我想要的效果。

    url=https://www.youtube.com/watch?v=dQw4w9WgXcQ
    
    textfile=""$folder""$YOUTUBE-DL_PATH"info.txt"
    ytdlfolder="$folder""$YOUTUBE-DL_PATH"
    jsonfile="$folder""$YOUTUBE-DL_JSON-FILE"

    Echo TITLE >> $textfile
    grep -o '"title": *"[^"]*"' $jsonfile | grep -o '"[^"]*"$' >> $textfile
    Echo \ >> $textfile
    
    Echo CHANNEL >> $textfile
    grep -o '"uploader": *"[^"]*"' $jsonfile | grep -o '"[^"]*"$' >> $textfile
    Echo \ >> $textfile
    
    Echo CHANNEL URL >> $textfile
    grep -o '"uploader_url": *"[^"]*"' *.json | grep -o '"[^"]*"$' >> $textfile
    Echo \ >> $textfile
    
    Echo UPLOAD DATE >> $textfile
    grep -o '"upload_date": *"[^"]*"' *.json | grep -o '"[^"]*"$' >> $textfile
    Echo \ >> $textfile
    
    Echo TAGS >> $textfile
    grep -o '"tags": *"[^"]*"' *.json | grep -o '"[^"]*"$' >> $textfile
    Echo \ >> $textfile
    
    echo URL >> $textfile
    echo $url >> $textfile
    echo \ >> $textfile
    
    Echo DESCRIPTION >> $textfile
    youtube-dl --get-description $url >> $textfile

剧本:

8 月 12 日。

8 月 13 日。

8 月 31 日。

9 月 5 日。

2020-09-17

2020-09-22

    #! /bin/bash
    ################################################################################
    # Script Name: Youtube-dl Easy Download Script
    # Description: Easy to use script to download YouTube videos with a couple of
    #              options.
    #
    # What this script do:
    #   - Downloads video in MP4 with highest quality and max resolution 1920x1080.
    #   - Downloads thumbnail and subtitles.
    #   - Gives user option where to download the video and video or only audio.
    #   - Creates a folder with same name as video title and puts all files there.
    #   - Creates a .txt file with information about the video.
    #
    #
    # Author:      Wogol - Stackoverflow.com, Github.com
    # License:     The GNU General Public License v3.0 - GNU GPL-3
    #
    # 
    # Big thanks to the people at youtube-dl GitHub and Stack Overflow. Without
    # their help this would never ever been possible for me.
    #
    # Special thanks to:
    #              Reino @ Stack Overflow
    #
    # #####
    #
    # Software required:                 youtube-dl, xidel, printf
    #
    # macOS:       1. Install Homebrew:  https://brew.sh
    #              2. Terminal command:  brew install youtube-dl xidel
    #
    # Linux:       Depends on package manager your distribution use.
    #
    # #####
    #
    # Version history:
    # 2020-09-22
    #   - Select menus is now one column.
    #   - Minor fixes.
    #   - Now all the bugs is fixed. Issues left is only optimizations.
    #
    # 2020-09-17
    #   - Folders can now have spaces in them.
    #
    # 2020-09-05
    #   - First working version.
    #
    # #####
    #
    # Issues left:
    #   - In the beginning there is a confirmation that show the title of the
    #     video so user know they got the correct video. It takes youtube-dl a
    #     couple of seconds. To speed up the script it is DISABLED by default.
    #
    #   - Have found out that the script dont need xidel to get json information
    #     but youtube-dl can get it. Dont know how to use youtube-dl --dump-json
    #     to get the same result.
    #
    #   - To get the path to the .txt file script use youtube-dl. This gives the
    #     script a pause for a few seconds. Best would get to get the path some how
    #     without connecting to YouTube again but use the output from youtube-dl
    #     some how. ... or run it in the background when video is downloading.
    #
    ################################################################################



    clear


    
    # - WELCOME MESSAGE -

    echo

    COLUMNS=$(tput cols)
    title="-= Youtube-dl Easy Download Script =-" 
    printf "%*s\n" $(((${#title}+$COLUMNS)/2)) "$title"



    # - PASTE URL -

    echo -e "\n*** - Paste URL address and hit RETURN. Example:\nhttps://www.youtube.com/watch?v=dQw4w9WgXcQ --OR-- https://youtu.be/dQw4w9WgXcQ\n"

    read url



    # - VIDEO TITLE -

    # So users know they have the correct URL.

    #echo -e "\nThe video is: (This takes 3-4 seconds, or more ...)"
    #youtube-dl -e $url
    #echo



    # - DOWNLOAD LOCATION -

    
    # DIRECTORY MUST END WITH SLASH: /


    echo -e "\n\n*** - Choose download folder:\n"
    
    COLUMNS=0
    PS3='Choose: '
    select directory in "~/Downloads/ytdl/Rick Astley/" "~/Downloads/ytdl/Never Gonna Give You Up/" "~/Downloads/ytdl/Other Rick Videos/" ; do
    echo -e "\nOption $REPLY selected. Download directory is:\n $directory"



    # - AUDIO/VIDEO SETTINGS -

    echo -e "\n\n*** - Choose download settings:\n"

    COLUMNS=0
    PS3='Choose: '
    options=("Audio & Video" "Audio only")
    select settingsopt in "${options[@]}"
        do
    case $settingsopt in

    "Audio & Video")
    av="-f bestvideo[ext=mp4][height<=1080]+bestaudio[ext=m4a]/best[ext=mp4]/best --merge-output-format mp4"

    ;;

    "Audio only")
    av="-f bestaudio[ext=m4a]/bestaudio"

    ;;

    esac

    echo -e "\nOption $REPLY selected:\n $settingsopt"



    # - THE DOWNLOAD SCRIPT -

    echo -e "\n\n*** - Starting download:\n"
    youtube-dl $av --write-thumbnail --all-subs --restrict-filenames -o "$directory%(title)s/%(title)s.%(ext)s" $url



    # - INFORMATION FILE -

    textfile=$(youtube-dl --get-filename --restrict-filenames -o "$directory%(title)s/%(title)s.txt" $url)

    xidel -s "$url" -e '
    let $json:=json(
        //script/extract(.,"ytplayer.config = (.+?\});",1)[.]
      )/args,
      $a:=json($json/player_response)/videoDetails,
      $b:=json($json/player_response)/microformat
    return (
    "- TITLE -",
    $a/title,"",
    "- CHANNEL -",
    $a/author,"",
    "- CHANNEL URL -",
    $b//ownerProfileUrl,"",
    "- UPLOAD DATE -",
    $b//publishDate,"",
    "- URL -",
    $json/loaderUrl,"",
    "- TAGS -",
    $a/keywords,"",
    "- DESCRIPTION -",
    $a/shortDescription
    )
    ' --printed-json-format=compact >> "$textfile"



    # - THE END -
    
    echo
    COLUMNS=$(tput cols) 
    ending="Download Complete!"
    printf "%*s\n\n" $(((${#ending}+$COLUMNS)/2)) "$ending"
    
    exit
    
    done
    done

标签: bashmacosyoutubeyoutube-dl

解决方案


终于让脚本工作了。

从很多人那里得到了很多帮助,但非常感谢 Reino 在这个线程中的帮助: Grep command questions - Grep text from program output?

该脚本有问题,可以优化,但我不知道如何修复它们。这是我创建的第一个 bash 脚本。

这样做的目标是创建一个脚本:

  • 简单易用。
  • 没有终端命令。
  • 不同目录中的初始排序。
  • 视频或仅音频。
  • MP4 最大分辨率为 1920x1080,因为一切都支持它开箱即用。
  • 包含有关视频的附加信息的文本文件。

这些是我在 Downie (macOS) 和 Clipgrab 等程序中错过的功能。

为了让其他人使用这个脚本和未来的修复,我尝试创建一个 Github 页面......可以说不是我的一杯茶。


脚本在此页面的第一篇文章中。

youtube-dl 下载脚本调试


推荐阅读