]> Arthur Taft Gitweb - yt-dlp-wrapper.git/blob - windows/yt-dlp-wrapper.ps1
Fix a little oopsie
[yt-dlp-wrapper.git] / windows / yt-dlp-wrapper.ps1
1 # Set Variables
2 $current_dir = Get-Location
3 $bin_dir = Join-Path -Path $current_dir -ChildPath '\bin'
4 $sig_dir = Join-Path -Path $current_dir -ChildPath '\sig'
5 $bin_test = Test-Path -Path $bin_dir
6 $sig_test = Test-Path -Path $sig_dir
7 $exe_location = Join-Path -Path $bin_dir -ChildPath '\yt-dlp.exe'
8 $exe_test = Test-Path -Path $exe_location
9 $ffmpeg_location = Join-Path -Path $bin_dir -ChildPath '\ffmpeg.exe'
10 $ffplay_location = Join-Path -Path $bin_dir -ChildPath '\ffplay.exe'
11 $ffprobe_location = Join-Path -Path $bin_dir -ChildPath '\ffprobe.exe'
12 $ffmpeg_dll_location = Join-Path -Path $bin_dir -ChildPath '\*.dll'
13 $ffmpeg_test = Test-Path -Path $ffmpeg_location
14 $audio_dir = Join-Path -Path $current_dir -ChildPath '\audio'
15 $video_dir = Join-Path -Path $current_dir -ChildPath '\video'
16 $work_dir = Join-Path -Path $current_dir -ChildPath '\work'
17 $ffmpeg_zip_work_location = Join-Path -Path $work_dir -ChildPath 'ffmpeg.zip'
18 $ffmpeg_work_location = Join-Path -Path $work_dir -ChildPath '\ffmpeg-master-latest-win64-gpl-shared\bin\*'
19 $test_audio_dir = Test-Path -Path $audio_dir
20 $test_video_dir = Test-Path -Path $video_dir
21 $audio_title = Join-Path -Path $audio_dir -ChildPath "'\%(title)s.%(ext)s'"
22 $video_title = Join-Path -Path $video_dir -ChildPath "'\%(title)s.%(ext)s'"
23 $val = 0
24 $url = ""
25 $dl_val = 0
26 $yt_sig_location = Join-Path -Path $current_dir -ChildPath '\sig\SHA2-512SUMS'
27 $yt_current_sig = certutil.exe -hashfile $exe_location SHA512
28 $ffmpeg_sig_location = Join-Path -Path $current_dir -ChildPath '\sig\checksums.sha256'
29 $ffmpeg_current_sig = certutil.exe -hashfile $ffmpeg_location SHA256
30 $yt_sig_eq_check = Select-String -Path $yt_sig_location -Pattern $yt_current_sig -SimpleMatch -Quiet
31 $ffmpeg_sig_eq_check = Select-String -Path $ffmpeg_sig_location -Pattern $ffmpeg_current_sig -SimpleMatch -Quiet
32 $ffmpeg_link = "https://github.com/yt-dlp/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-win64-gpl-shared.zip" 
33 $yt_dlp_link = "https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp.exe" 
34 $batch_dl_file = Join-Path -Path $current_dir -ChildPath 'batch-download.txt'
35
36 function Get-FFmpeg {
37
38     param (
39        $ffmpeg_link 
40     )
41
42     # Create work directory to extract archive with new executable
43     New-Item -Path $current_dir -Name "work" -ItemType "directory"
44     Invoke-WebRequest $ffmpeg_link -OutFile $ffmpeg_zip_work_location
45     # Extract archive to work directory
46     Expand-Archive -Path $ffmpeg_zip_work_location -DestinationPath $work_dir
47
48     # Move new executable to correct location
49     Move-Item -Path $ffmpeg_work_location -Destination $bin_dir 
50
51     # Remove work directory
52     Remove-Item -Recurse -Force $work_dir
53
54     Write-Output "Done!"
55     
56 }
57
58 function Convert-BatchLink {
59
60     param (
61         $dl_val
62     )
63
64     while($dl_val -ne 1){
65         # Ask the user if they are downloading audio or video
66         $Format = Read-Host "Audio or Video?"
67
68         # Download audio
69         if($Format -eq "audio") {
70             Invoke-Expression "$exe_location -x -f 'ba' --audio-format mp3 --windows-filenames -o $audio_title -a $batch_dl_file"
71             $dl_val++
72         }
73         # Download video
74         elseif($Format -eq "video"){
75             Invoke-Expression "$exe_location -f 'bv*[ext=mp4]+ba[ext=m4a]/b[ext=mp4] / bv*+ba/b' --windows-filenames --sponsorblock-remove sponsor,selfpromo,interaction -o $video_title -a $batch_dl_file"
76             $dl_val++
77         }
78         else {
79             Write-Output "Response must be 'Audio' or 'Video'!"
80             continue
81         }
82     }
83 }
84
85 function Convert-Link {
86     
87     param (
88         $dl_val
89     )
90
91     while($dl_val -ne 1) {
92         # Ask the user if they are downloading audio or video
93         $Format = Read-Host "Audio or Video?"
94
95         # Download audio
96         if($Format -eq "audio") {
97             $url = Read-Host "Enter URL Here"
98             Invoke-Expression "$exe_location -x -f 'ba' --audio-format mp3 --windows-filenames -o $audio_title $url"
99             $dl_val++
100         }
101         # Download video
102         elseif($Format -eq "video"){
103             $url = Read-Host "Enter URL Here"
104             Invoke-Expression "$exe_location -f 'bv*[ext=mp4]+ba[ext=m4a]/b[ext=mp4] / bv*+ba/b' --windows-filenames --sponsorblock-remove sponsor,selfpromo,interaction -o $video_title $url"
105             $dl_val++
106         }
107         else {
108             Write-Output "Response must be 'Audio' or 'Video'!"
109             continue
110         }
111     }   
112 }
113     
114 if($bin_test -ne $true) {
115     Write-Output "bin directory not found! Creating now!"
116     New-Item -Path $current_dir -Name "bin" -ItemType "directory" 
117 }
118
119 if($sig_test -ne $true) {
120     Write-Output "sig directory not found! Creating now!"
121     New-Item -Path $current_dir -Name "sig" -ItemType "directory"   
122 }
123
124 if($ffmpeg_test -eq $true){
125     # Remove old ffmpeg signature
126     Remove-Item $ffmpeg_sig_location
127 } else {
128     Write-Output "ffmpeg executable not found! Downloading now..."
129     Get-FFmpeg($ffmpeg_link)
130 }
131
132 if($exe_test -eq $true) {
133     # Remove old yt-dlp signature 
134     Remove-Item $yt_sig_location
135 } else {
136     Write-Output "yt-dlp executable not found! Downloading now..."
137     Invoke-WebRequest $yt_dlp_link -OutFile $exe_location
138     Write-Output "Done!"
139 }
140
141 Write-Output "Updating ffmpeg signature..."
142 # Download new ffmpeg signature
143 Invoke-WebRequest https://github.com/arthur-taft/ffmpeg-latest-signatures/releases/latest/download/checksums.sha256 -OutFile $ffmpeg_sig_location
144 Write-Output "Done!"
145
146 Write-Output "Updating yt-dlp signature..."
147 # Download new yt-dlp signature
148 Invoke-WebRequest https://github.com/arthur-taft/yt-dlp-signatures/releases/latest/download/SHA2-512SUMS -OutFile $yt_sig_location
149 Write-Output "Done!"
150
151 if($yt_sig_eq_check -ne $true) {
152     Write-Output "Old version of yt-dlp found, updating now..."
153
154     # Remove old executable
155     Remove-Item $exe_location
156
157     # Download new executable
158     Invoke-WebRequest $yt_dlp_link -OutFile $exe_location 
159
160     Write-Output "Done!"
161 }
162
163 if($ffmpeg_sig_eq_check -ne $true) {
164     Write-Output "Old version ffmpeg found, updating now..."
165
166     # Remove old executables
167     Remove-Item $ffmpeg_location
168     Remove-Item $ffprobe_location
169     Remove-Item $ffplay_location
170
171     # Remove old libraries
172     Remove-Item $ffmpeg_dll_location
173
174     Get-FFmpeg($ffmpeg_link)
175 }
176
177 # Check if audio output directory exists, if not, create it
178 if($test_audio_dir -eq $false) {
179     Write-Output "Audio directory not found!"
180     Write-Output "Creating now..."
181     New-Item -Path $current_dir -Name "audio" -ItemType "directory"
182     Write-Output "Done!"
183 }
184
185 # Check if video output directory exists, if not, create it
186 if($test_video_dir -eq $false) {
187     Write-Output "Video directory not found!"
188     Write-Output "Creating now..."
189     New-Item -Path $current_dir -Name "video" -ItemType "directory"
190     Write-Output "Done!"
191 }
192
193 while($val -ne 1) {
194     # Ask the user if they are performing a batch download
195     $Type = Read-Host "Do you want to batch download using links in the 'batch-downloads.txt' file? (y/n)"
196     
197     if($Type -eq "y") {
198         Convert-BatchLink($dl_val) 
199         $val++
200     }
201     elseif($Type -eq "n") {
202         Convert-Link($dl_val)
203         $val++
204     }
205     else{
206         Write-Output "Answer must be Y or N!"
207         continue
208     }
209 }