-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube2mp3
executable file
·111 lines (95 loc) · 2.75 KB
/
youtube2mp3
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
#!/bin/bash
#youtube2mp3
# Copyright (c) 2012 Lork Scorguar <[email protected]>
# This project is a fork of the version from PTKDev <[email protected]>
# It's aim to be use only on terminal and add id3tag to the song
#
# 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 <http://www.gnu.org/licenses/>.
#!/bin/bash
function usage {
echo -e "Help\nOptions are:\n\
-h | --help: display this help\n\
-y | --youtube: needs url of the video\n\
-t | --title: needs the title of the music to complete id3tag\n\
-a | --artist: needs the artist of the music to complete id3tag\n"
exit
}
# Execute getopt
ARGS=`getopt -o "y:t:a:h" -l "youtube:,title:,artist:,help" \
-n "getopt.sh" -- "$@"`
#Bad arguments
if [ $? -ne 0 ];
then
usage
fi
# A little magic
eval set -- "$ARGS"
# Now go through all the options
while true;
do
case "$1" in
-y|--youtube)
if [ -n "$2" ];
then
address=$2
fi
shift 2;;
-t|--title)
if [ -n "$2" ];
then
title=$2
fi
shift 2;;
-a|--artist)
if [ -n "$2" ];
then
artist=$2
fi
shift 2;;
-h|--help)
usage
;;
--)
shift
break;;
esac
done
regex='v=(.*)'
bitrate=320
dest_dir="/home/lork/Téléchargements"
if [[ $return_code_1 -eq 0 ]] && [[ $return_code_2 -eq 0 ]] && [[ $return_code_3 -eq 0 ]]; then
if [[ $address =~ $regex ]]; then
video_id=${BASH_REMATCH[1]}
video_id=$(echo $video_id | cut -d'&' -f1)
video_title="$(youtube-dl --get-title $address)"
video_title=${video_title//\//-}
youtube-dl --output youtube-%\(id\)s.%\(ext\)s $address
if [ -e youtube-$video_id.flv ]; then
ext="flv"
fi
if [ -e youtube-$video_id.mp4 ]; then
ext="mp4"
fi
if [ -e youtube-$video_id.webm ]; then
ext="webm"
fi
ffmpeg -i youtube-$video_id.$ext /tmp/youtube-$video_id.wav
lame /tmp/youtube-$video_id.wav "$dest_dir"/"$video_title".mp3 -b $bitrate
rm youtube-$video_id.$ext /tmp/youtube-$video_id.wav
id3tool -t "$title" -r "$artist" "$dest_dir"/"$video_title".mp3
echo -e "Youtube mp3 download done\n$video_title downloaded"
else
echo -e "An error occured during the processing"
fi
fi