在 Youtube-dl 中,我需要一个路径来下载一个播放列表,其中一个文件夹用于一个不同的视频!并且每个视频都需要以其编号开头(1 代表第一个和他的名字;8 代表第八个和他的名字...)
答案1
#!/bin/bash
echo "Downloading playlist...(results.log)"
youtube-dl -j --flat-playlist "$1" | jq -r '.id' | sed 's_^_https://youtu.be/_' > results.log
num=1
total="$(wc -l < results.log)"
echo "Total videos: $total"
while [ "$num" -le "$total" ]; do
echo "Processing video #$num Thank you for Patience"
url="$(sed -n "$num"p results.log)"
title=$(youtube-dl -f mp4 -o '%(id)s.%(ext)s' --print-json --no-warnings "$url" | jq -r .title)
echo "Title is $title"
title="$num. $title"
fly='"'
title=$fly"$title"$fly
echo "New title is $title"
echo "URL is $url"
sleep 5
file_name="$(youtube-dl --get-filename --simulate $url)"
ext="${file_name##*.}"
echo "ext is $ext"
youtube-dl -f best --output $num.$ext $url
sleep 5
mkdir "$title"
mv $num.$ext ./"$title"
echo "please wait"
sleep 1
num=$(( num+1 ))
done
#some Housekeeping !!
for i in *; do mv "$i" "`echo $i | sed 's/"/ /'`" 2>/dev/null; done
for i in *; do mv "$i" "`echo $i | sed 's/"/ /'`" 2>/dev/null; done
for i in *; do mv "$i" "`echo $i | sed 's/ / /'`" 2>/dev/null; done
答案2
#!/bin/bash
#Download playlist
youtube-dl -j --flat-playlist "https://<yourYoutubePlaylist>" | jq -r '.id' | sed 's_^_https://youtu.be/_' > result.log
#set video count=1
num=1
total="$(wc -l result.log)"
while ( $num -le $total)
do
url="$(sed -n "$num"p result.log)"
#%(title)s should give video title
youtube-dl -f 18 --output $num_"%(title)s.%(ext)s" $url
参考文献:- 1.radiolondra: https://superuser.com/questions/1341684/youtube-dl-how-download-only-the-playlist-not-the-files-therein 2.maythux: 如何在使用 youtube-dl 提取音频时指定文件名?