bash - youtube-dl 下载脚本调试
问题描述
转储更新这篇文章的非常糟糕的想法。此脚本的新位置: https ://gist.github.com/Wogol/66e9936b6d49cc5fecca59eaeca1ca2e
我试图创建一个使用简单易用的 Youtube-dl 的 .command macOS 脚本(也应该在 GNU/Linux 下工作)。我已修复,因此它会下载描述、缩略图、字幕、json、创建文件夹结构并保存视频 ID、上传者和上传日期。
主脚本的问题:
已修复(8 月 13 日)我遇到的问题是音频和视频或仅音频选项。出于某种原因,脚本中只有音频有效。视频和音频的下载脚本不起作用,但如果我将相同的命令行(“调试输出行”)粘贴到终端窗口中,它就可以工作。抓着我的头。Youtube-dl 给我这个消息:错误:请求的格式不可用
已修复(8 月 31 日)获得视频工作的最大分辨率。找到了强制 mp4 或最大分辨率的信息,但没有将它们结合起来。
信息文件的问题:
还创建一个包含标题、频道名称、发布日期、描述的信息文件。我现在正在努力从 .json 和 youtube-dl 获取视频信息以导出到 info.txt 文件中。
- 已修复(9 月 5 日) textfile=""$folder"info.txt" 不起作用。给出这个错误:(我想添加 youtube-dl 文件夹。
ytdl.command: line 104: ~/Downloads/ytdl/dog_vids/info.txt: No such file or directory
- 已修复(9 月 5 日)找到 youtube-dl 文件夹并使其与 grep 一起使用。像这样的东西:
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
已修复(9 月 5 日)使用 grep 命令我将 json 文件命名为“*.json”,因为每个目录只有一个,但我不喜欢那个解决方案。(可以用上面的点来回答)
已修复(9 月 5 日)如何让 grep 不要抓取“?现在它会在所有内容之前和之后添加它们。
已修复(9 月 5 日)如何从 json 文件中获取标签信息?标签看起来像这样:
"tags": ["music", "video", "classic"]
- 已修复(9 月 5 日)在下载视频的后台运行创建信息文件部分脚本?
当前版本试图让它工作
(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 日。
- 将 url 移到顶部,因此当用户粘贴 url 时,他们会恢复视频标题。这样用户就知道他们得到了正确的视频。
- 添加了最大分辨率 1920x1080。(不工作)
8 月 13 日。
- 下载音视频作品。
8 月 31 日。
- 固定力 mp4 和 1080 的最大高度。
9 月 5 日。
- 最后工作脚本。在此处阅读更多信息(或向下滚动): Youtube-dl 下载脚本调试
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
解决方案
终于让脚本工作了。
从很多人那里得到了很多帮助,但非常感谢 Reino 在这个线程中的帮助: Grep command questions - Grep text from program output?
该脚本有问题,可以优化,但我不知道如何修复它们。这是我创建的第一个 bash 脚本。
这样做的目标是创建一个脚本:
- 简单易用。
- 没有终端命令。
- 不同目录中的初始排序。
- 视频或仅音频。
- MP4 最大分辨率为 1920x1080,因为一切都支持它开箱即用。
- 包含有关视频的附加信息的文本文件。
这些是我在 Downie (macOS) 和 Clipgrab 等程序中错过的功能。
为了让其他人使用这个脚本和未来的修复,我尝试创建一个 Github 页面......可以说不是我的一杯茶。
脚本在此页面的第一篇文章中。
推荐阅读
- javascript - vue-loader 干扰自定义插件
- gdb - 如何在 Cortex-M 设备上的同一 GDB 会话中调试引导加载程序和应用程序?
- javascript - 在 Highchart js 中显示 Ajax 数据
- laravel - 如何更改 Laravel Nova 中的按钮文本?
- gitlab - 使用 gitlab 部署某个构建
- java - 将 Swing GUI 添加到现有的工作 java 程序
- wordpress - Wordpress 登录页面重定向到子目录,得到 http 500 错误?
- sql-server - 通过 deviceid 从每个组中获取最新的 1 条记录
- ios - 线程 1:在 Xcode 11.2.1 上运行的信号 SIGABRT 错误
- angular - Angular 无法分配给对象“[object HTMLImageElement]”的只读属性“offsetTop”