-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathyoutube_to_mp3.sh
executable file
·112 lines (97 loc) · 2.88 KB
/
youtube_to_mp3.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env bash
check_dependencies() {
local dependencies=("yt-dlp" "ffmpeg")
for dependency in "${dependencies[@]}"; do
if ! which "$dependency" >/dev/null 2>&1; then
echo "Error: '$dependency' is not installed."
return 1
fi
done
return 0
}
try_to_install_dependencies() {
local dependencies=("yt-dlp" "ffmpeg")
if [ -f /etc/os-release ]; then
if grep -q "debian" /etc/os-release; then
local command_to_install="apt install"
elif grep -q "arch" /etc/os-release; then
local command_to_install="pacman -S"
else
echo "Error: Unknown OS"
return 1
fi
else
echo "Error: /etc/os-release not found"
return 1
fi
for dependency in "${dependencies[@]}"; do
if ! which "$dependency" >/dev/null 2>&1; then
echo "Trying to install '$dependency'."
eval sudo $command_to_install "$dependency"
fi
done
}
download_video() {
local url="$1"
local output_file="$2"
echo "Attempting to download video from '$url'"
yt-dlp -f bestaudio -o "$output_file" "$url"
echo "Saving video to '$output_file'"
}
convert_video_to_mp3() {
local input_file="$1"
local output_file="${input_file%.*}.mp3"
echo "Converting video to mp3"
ffmpeg -i "$input_file" -vn -ab 128k "$output_file"
echo "Audio file saved to '$output_file'"
rm "$input_file"
echo "Video file deleted"
}
download_video_and_convert_to_mp3() {
local url="$1"
local output_file
if [ -z "$2" ]; then
output_file=$(yt-dlp --get-title "$url")
output_file=$(echo "$output_file" | tr ' ' '_' | tr '[:upper:]' '[:lower:]')
else
output_file="$2"
fi
download_video "$url" "$output_file"
convert_video_to_mp3 "$output_file"
}
read_playlist_to_array() {
local url="$1"
local temp_file
temp_file="$(mktemp)"
yt-dlp -j --flat-playlist --skip-download "$url" | jq -r '.id' | sed 's_^_https://youtu.be/_' > "$temp_file"
IFS=$'\n' read -d '' -r -a all_videos_urls < "$temp_file"
unset IFS
}
main() {
if [ $# -lt 1 ]; then
echo "Usage: youtube_to_mp3.sh [url] <output_file>"
return
fi
if ! check_dependencies; then
try_to_install_dependencies
if ! check_dependencies; then
echo "Error: Dependencies not installed."
return 1
fi
fi
local url="$1"
read_playlist_to_array "$url"
if [ "${#all_videos_urls[@]}" -eq 0 ]; then
echo "No videos found in the playlist."
return 1
fi
if [ "${#all_videos_urls[@]}" -gt "1" ]; then
echo "Downloading playlist..."
for video_url in "${all_videos_urls[@]}"; do
download_video_and_convert_to_mp3 "$video_url"
done
else
download_video_and_convert_to_mp3 "$url" "$2"
fi
}
main "$@"