-
Notifications
You must be signed in to change notification settings - Fork 0
/
youtube-music
executable file
·103 lines (95 loc) · 2.84 KB
/
youtube-music
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
#! /bin/bash
# youtube-music: a simple script for downloading music using youtube-dl to mp3
#
# Copyright (C) 2021 mini_bomba
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# Check dependencies
FAIL=0
if YTDL=$(which yt-dlp)
then
echo "Using yt-dlp" >&2
elif YTDL=$(which youtube-dl)
then
echo "yt-dlp not found; using fallback youtube-dl." >&2
echo "Consider installing yt-dlp if you experience throttling." >&2
else
echo "Missing required dependency: yt-dlp" >&2
FAIL=1
fi
if ! which ffmpeg > /dev/null 2>&1
then
echo "Missing required dependency: ffmpeg" >&2
FAIL=1
fi
if ! which grep > /dev/null 2>&1
then
echo "Missing required dependency: grep" >&2
FAIL=1
fi
if ! which jq > /dev/null 2>&1
then
echo "Missing required dependency: jq" >&2
FAIL=1
fi
if ! which xargs > /dev/null 2>&1
then
echo "Missing required dependency: xargs" >&2
FAIL=1
fi
if [ $FAIL -ne 0 ]
then
exit 1
fi
# Set default settings if not overriden
if [ -z "$MBYTM_DOWNLOAD_PROCS" ]
then
MBYTM_DOWNLOAD_PROCS=10
fi
if [ -z "$MBYTM_FFMPEG_PROCS" ]
then
MBYTM_FFMPEG_PROCS=4
fi
VID="$@"
# Ask user for url, if not given in args
if [ -z "$VID" ]
then
echo -n "No music video url/id given, please enter one now: "
read VID
if [ -z "$VID" ]
then
echo "No music video url/id given, exiting."
exit 1
fi
fi
# Create a temp directory (for easy playlist support)
TMP_DIR=$(mktemp -d)
trap "rm -rf $TMP_DIR" EXIT
trap "exit 1" INT
# Download videos to temp directory
echo "Downloading videos..."
if echo "$VID" | grep -P "youtube\.com/.*list" > /dev/null
then
$YTDL --flat-playlist -j "$VID" | jq -r '.url' | xargs -P $MBYTM_DOWNLOAD_PROCS -n 1 -d "\n" -r sh -c "echo \"Downloading \$0...\"; $YTDL -q -o \"$TMP_DIR/%(id)s\" -f \"bestaudio/best[height<=144]/best\" \$0; echo \"\$0 downloaded\""
else
$YTDL -o "$TMP_DIR/%(id)s" -f "bestaudio/best[height<=144]/best" "$VID"
fi
# Extract music to current directory
echo "Extracting music..."
ls "$TMP_DIR" | xargs -P $MBYTM_FFMPEG_PROCS -n 1 -r sh -c "echo \"Processing \$0...\"; ffmpeg -i \"$TMP_DIR/\$0\" -v warning -y \"./\$0.mp3\"; echo \"\$0 processed\""
# Cleanup temp files
echo "Deleting temp files..."
trap - EXIT INT
rm -rf "$TMP_DIR"
echo "Done!"