为什么我的脚本只有在我 ^D 之后才结束?
Why does my script end only after I ^D it?
我有一个脚本可以接收 youtube link 并使用 youtube-dl 将该视频的可用分辨率提取到 "res.txt" 文件中。 res.txt 文件包含所需的输出,但仅在我按 ^D 后显示。谁能告诉我为什么我的脚本卡住了?
#!/bin/bash
youtube-dl -F https://www.youtube.com/watch?v=jVv_aSTVpyI > info.txt
echo Start
cat > res.txt
for i in '144p' '240p' '360p' '480p' '720p' '1080p' '1440p' '2160p' ; do
while read p ; do
if [[ "$p" == *"$i"* ]] ; then
echo "$i" >> res.txt
break
fi
done < info.txt
done
echo "done"
a=1
sed -i '1d' res.txt
echo "Please Type required resolution: "
while read p ; do
echo -n $a
echo -n ")"
echo $p
a=$((a+1))
done < res.txt
read reso
youtube-dl -f 'bestvideo[height<=$reso]+bestaudio' $link
这会更短、更高效:
youtube-dl -F https://www.youtube.com/watch?v=jVv_aSTVpyI |
grep -oE '(144|240|360|480|720|1080|1440|2160)p' |
sort -u > res.txt
grep -oE
: 提取匹配Extended RegEx的行,只打印匹配的部分。
sort -u
: 对行进行排序并删除重复项。
有JSON处理
或者,如果您想可靠地提取 width
和 height
元数据,youtube-dl
可以生成 JSON 数据集 -j
选项开关,用 jq
:
解析它
youtube-dl -j https://www.youtube.com/watch?v=jVv_aSTVpyI |
jq -r '[ .formats| .[] | select(.width != null) | [.width,.height] ] | unique | .[] | @tsv'
特色对话框菜单分辨率选择和下载
#!/usr/bin/env bash
[[ $# -eq 1 ]] || exit 2
# https://www.youtube.com/watch?v=jVv_aSTVpyI
link=""
declare -a res
IFS=$'\n' read -r -d '' -a res < <(
youtube-dl -j "$link" |
jq '[.formats|.[]|select(.width!=null)|[.width,.height]]|unique|.[]|@sh'
)
declare -a options=()
for i in "${!res[@]}"; do
read -r width height <<<"${res[i]}"
options+=("$i" "$width×$height")
done
{
choice=$(
dialog \
--clear \
--backtitle "youtube-dl $link" \
--title '[ V I D E O - R E S O L U T I O N ]' \
--menu 'Select the video resolution' 0 0 "${#options[@]}" "${options[@]}" \
1>&3 2>&1
)
} 3>&1 || exit
read -r width height <<<"${res[choice]}"
printf -v format 'bestvideo[height<=%d]+bestaudio' "$height"
youtube-dl -f "$format" "$link"
我有一个脚本可以接收 youtube link 并使用 youtube-dl 将该视频的可用分辨率提取到 "res.txt" 文件中。 res.txt 文件包含所需的输出,但仅在我按 ^D 后显示。谁能告诉我为什么我的脚本卡住了?
#!/bin/bash
youtube-dl -F https://www.youtube.com/watch?v=jVv_aSTVpyI > info.txt
echo Start
cat > res.txt
for i in '144p' '240p' '360p' '480p' '720p' '1080p' '1440p' '2160p' ; do
while read p ; do
if [[ "$p" == *"$i"* ]] ; then
echo "$i" >> res.txt
break
fi
done < info.txt
done
echo "done"
a=1
sed -i '1d' res.txt
echo "Please Type required resolution: "
while read p ; do
echo -n $a
echo -n ")"
echo $p
a=$((a+1))
done < res.txt
read reso
youtube-dl -f 'bestvideo[height<=$reso]+bestaudio' $link
这会更短、更高效:
youtube-dl -F https://www.youtube.com/watch?v=jVv_aSTVpyI |
grep -oE '(144|240|360|480|720|1080|1440|2160)p' |
sort -u > res.txt
grep -oE
: 提取匹配Extended RegEx的行,只打印匹配的部分。sort -u
: 对行进行排序并删除重复项。
有JSON处理
或者,如果您想可靠地提取 width
和 height
元数据,youtube-dl
可以生成 JSON 数据集 -j
选项开关,用 jq
:
youtube-dl -j https://www.youtube.com/watch?v=jVv_aSTVpyI |
jq -r '[ .formats| .[] | select(.width != null) | [.width,.height] ] | unique | .[] | @tsv'
特色对话框菜单分辨率选择和下载
#!/usr/bin/env bash
[[ $# -eq 1 ]] || exit 2
# https://www.youtube.com/watch?v=jVv_aSTVpyI
link=""
declare -a res
IFS=$'\n' read -r -d '' -a res < <(
youtube-dl -j "$link" |
jq '[.formats|.[]|select(.width!=null)|[.width,.height]]|unique|.[]|@sh'
)
declare -a options=()
for i in "${!res[@]}"; do
read -r width height <<<"${res[i]}"
options+=("$i" "$width×$height")
done
{
choice=$(
dialog \
--clear \
--backtitle "youtube-dl $link" \
--title '[ V I D E O - R E S O L U T I O N ]' \
--menu 'Select the video resolution' 0 0 "${#options[@]}" "${options[@]}" \
1>&3 2>&1
)
} 3>&1 || exit
read -r width height <<<"${res[choice]}"
printf -v format 'bestvideo[height<=%d]+bestaudio' "$height"
youtube-dl -f "$format" "$link"