-
Notifications
You must be signed in to change notification settings - Fork 4
/
extract_playlist.sh
executable file
·49 lines (39 loc) · 1.41 KB
/
extract_playlist.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/zsh
# this file converts a YouTube playlist (given as the playlist URL) to a txt file consisting of the URL's of the individual songs. it'll also automatically label the songs based on their titles.
if [ "$#" -ne 2 ]; then
echo "Usage: $0 [YouTube Playlist URL] [output_filename.txt]"
echo "Example: $0 'https://youtube.com/playlist?list=...' rock.txt"
exit 1
fi
playlist_url=$1
output_file=$2
if [[ ! $output_file =~ \.txt$ ]]; then
output_file="${output_file}.txt"
fi
echo "Extracting playlist info to $output_file..."
echo "Preview of how labels will look:"
echo "--------------------------------"
youtube-dl --get-title --get-id "$playlist_url" | \
while read -r title; do
read -r video_id
if [ -n "$title" ]; then
label=$(echo "$title" | \
tr -cd '[:alnum:] ' | \
tr '[:upper:]' '[:lower:]' | \
tr ' ' '_' | \
sed 's/__*/_/g')
echo "Original: $title"
echo "Label : $label"
echo "---"
echo "${label} | https://www.youtube.com/watch?v=${video_id}" >> "${output_file}.tmp"
fi
done
echo -n "Would you like to proceed with creating the playlist file? (y/n) "
read answer
if [[ $answer == "y" ]]; then
mv "${output_file}.tmp" "${output_file}"
echo "Created playlist file: $output_file"
else
rm "${output_file}.tmp"
echo "Cancelled. No file was created."
fi