]> Arthur Taft Gitweb - yt-dlp-wrapper.git/blob - linux/yt-dlp-wrapper.sh
Add auto updating
[yt-dlp-wrapper.git] / linux / yt-dlp-wrapper.sh
1 #!/bin/bash
2
3 # Cross-distro package checker and installer
4 check_or_install() {
5     local pkg="$1"
6     local pm=""
7
8     if command -v pacman &>/dev/null; then
9         pm="pacman"
10         pacman -Q "$pkg" &>/dev/null
11
12     elif command -v dpkg &>/dev/null && command -v apt &>/dev/null; then
13         pm="apt"
14         dpkg -s "$pkg" &>/dev/null
15
16     elif command -v rpm &>/dev/null && command -v dnf &>/dev/null; then
17         pm="dnf"
18         rpm -q "$pkg" &>/dev/null
19
20     elif command -v rpm &>/dev/null && command -v yum &>/dev/null; then
21         pm="yum"
22         rpm -q "$pkg" &>/dev/null
23
24     elif command -v qlist &>/dev/null; then
25         pm="emerge"
26         qlist -I "$pkg" &>/dev/null
27
28     elif command -v zypper &>/dev/null; then
29         pm="zypper"
30         zypper se --installed-only "$pkg" | grep -q "$pkg"
31
32     else
33         echo "Unsupported package manager" >&2
34         return 2
35     fi
36
37     if [[ $? -eq 0 ]]; then
38         echo "$pkg is already installed."
39         return 0
40     fi
41
42     echo "Installing missing package: $pkg via $pm..."
43     case "$pm" in
44         pacman) sudo pacman -Sy --noconfirm "$pkg" ;;
45         apt) sudo apt update && sudo apt install -y "$pkg" ;;
46         dnf) sudo dnf install -y "$pkg" ;;
47         yum) sudo yum install -y "$pkg" ;;
48         emerge) sudo emerge "$pkg" ;;
49         zypper) sudo zypper install -y "$pkg" ;;
50         *) echo "Cannot install $pkg on unknown system." >&2; return 3 ;;
51     esac
52 }
53
54 # Ensure dependencies
55 check_or_install wget
56 check_or_install ffmpeg
57
58 # Define variables
59 current_dir="$PWD"
60 exe_location="$current_dir/bin/yt-dlp"
61 ffmpeg_location="/usr/bin/ffmpeg"
62 audio_dir="$current_dir/audio"
63 video_dir="$current_dir/video"
64 generated_signature="$current_dir/exe_sig.sha512"
65 upstream_signature="$current_dir/sig/yt-dlp-linux.sha512"
66 val=0
67 dl_val=0
68 url=""
69 format=""
70 type=""
71 batch_dl_file="$current_dir/batch-dl.txt"
72
73 # Ensure bin directory exists
74 mkdir -p "$current_dir/bin"
75
76 # Ensure sig directory exists
77 mkdir -p "$current_dir/sig"
78
79 # Function to download audio
80 audio_dl () {
81     "$exe_location" -x -f 'ba' --audio-format mp3 -o "$audio_dir/%(title)s.%(ext)s" "$url"
82 }
83
84 audio_dl_batch () {
85     "$exe_location" -x -f 'ba' --audio-format mp3 -o "$audio_dir/%(title)s.%(ext)s" -a "$batch_dl_file"
86 }
87
88 # Function to download video
89 video_dl () {
90     "$exe_location" -f "bv*[ext=mp4]+ba[ext=m4a]/b[ext=mp4]/bv*+ba/b" -o "$video_dir/%(title)s.%(ext)s" "$url"
91 }
92
93 video_dl_batch () {
94     "$exe_location" -f "bv*[ext=mp4]+ba[ext=m4a]/b[ext=mp4]/bv*+ba/b" --sponsorblock-remove sponsor,selfpromo,interaction -o "$video_dir/%(title)s.%(ext)s" -a "$batch_dl_file"
95 }
96
97 # Function to single download
98 single_dl () {
99     local dl_val="$dl_val"
100     while [ "$dl_val" -ne 1 ]; do
101         read -p "Audio or Video?: " format
102
103         case "$format" in
104             audio)
105                 read -p "Enter URL Here: " url
106                 audio_dl
107                 dl_val=1
108                 ;;
109             video)
110                 read -p "Enter URL Here: " url
111                 video_dl
112                 dl_val=1
113                 ;;
114             *)
115                 echo "❗ Must be 'audio' or 'video'!"
116                 ;;
117         esac
118     done
119 }
120 # Function to batch download
121 batch_dl () {
122     local dl_val="$dl_val"
123     while [ "$dl_val" -ne 1 ]; do
124         read -p "Audio or Video?: " format
125
126         case "$format" in
127             audio)
128                 audio_dl_batch
129                 dl_val=1
130                 ;;
131             video)
132                 video_dl_batch
133                 dl_val=1
134                 ;;
135             *)
136                 echo "Must be 'audio' or 'video'!"
137                 ;;
138         esac
139     done
140 }
141
142 # Pull yt-dlp cert
143 wget -q -O "$current_dir/sig/yt-dlp-linux.sha512" https://github.com/arthur-taft/yt-dlp-signatures/releases/latest/download/yt-dlp-linux.sha512
144
145 # Generate signature for binary
146 sha512sum "$exe_location" > "$current_dir/exe_sig.sha512" && sed 's/.\{8\}$//' "$current_dir/exe_sig.sha512"
147
148 # Check if signatures match
149 if ! grep -Ff "$current_dir/exe_sig.sha512" "$current_dir/sig/yt-dlp-linux.sha512"; then
150     echo "Old verion of yt-dlp detected downloading now"
151     # Remove existing executable (if it exists)
152     [ -f "$exe_location" ] && rm "$exe_location"
153
154     # Download yt-dlp
155     wget -q -O "$exe_location" https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp
156     chmod +x "$exe_location"
157
158     rm "$current_dir/exe_sig.sha512"
159 fi
160
161 # Create directories if they don't exist
162 mkdir -p "$audio_dir"
163 mkdir -p "$video_dir"
164
165 # Download loop
166 while [ "$val" -ne 1 ]; do
167     read -p "Batch Download? (y/n): " type
168     case "$type" in
169         y)
170             batch_dl
171             val=1
172             ;;
173         n)
174             single_dl
175             val=1
176             ;;
177         *)
178             echo "Must be 'y' or 'n'!"
179             ;;
180     esac
181 done
182 exit 0