-
Notifications
You must be signed in to change notification settings - Fork 0
/
ytdl-dM.ps1
434 lines (360 loc) · 14.1 KB
/
ytdl-dM.ps1
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#v12012021
#NB
$ytdl_dir = "" # write YouTube-dl location here
$youtube_dl_location = "$ytdl_dir\youtube-dl.exe"
$ffmpeg_location = "$ytdl_dir\ffmpeg\bin" # change ffmpeg location if needed
$dir = "" # write target location here
$logdir = "$dir\download-log.txt" # change log location if needed
$isLogging = $true # toggle logging
$ytAPIkey = "" # write your API-Key here (see description)
$url = ""
$errorCount = 0
# functions
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
function Get-VideoInfo {
param(
[Parameter(Mandatory = $True)]
[string]$videoURL,
[Parameter(Mandatory = $True)]
[ValidateSet('title', 'duration', 'id', 'etag')]
[string]$infoType
)
$regex = '[^ ]*v='
$videoID = $videoURL -replace $regex
$regex = '&t[^ ]*'
$videoID = $videoID -replace $regex
$metadata = irm "https://www.googleapis.com/youtube/v3/videos?id=$videoID&key=$ytAPIkey&part=snippet&part=contentDetails"
if ($infoType -eq 'duration')
{
$videoDuration = $metadata.items.contentDetails.duration
$regex = '(^[A-Z]*)'
$videoDuration = $videoDuration -replace $regex
$regex = 'M[^ ]*'
$videoM = $videoDuration -replace $regex
[int]$videoM *= 60
$regex = '[^ ]*M'
$videoS = $videoDuration -replace $regex
$regex = 'S[^ ]*'
$videoS = $videoS -replace $regex
[int]$videoInfo = $videoM + $videoS
} elseif ($infoType -eq 'title')
{
$videoInfo = $metadata.items.snippet.title
} elseif ($infoType -eq 'id')
{
$videoInfo = $metadata.items.id
} elseif ($infoType -eq 'etag')
{
$videoInfo = $metadata.etag
}
$videoInfo
}
function Set-WindowState {
<#
.LINK
https://gist.github.com/Nora-Ballard/11240204
#>
[CmdletBinding(DefaultParameterSetName = 'InputObject')]
param(
[Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]
[Object[]] $InputObject,
[Parameter(Position = 1)]
[ValidateSet('FORCEMINIMIZE', 'HIDE', 'MAXIMIZE', 'MINIMIZE', 'RESTORE',
'SHOW', 'SHOWDEFAULT', 'SHOWMAXIMIZED', 'SHOWMINIMIZED',
'SHOWMINNOACTIVE', 'SHOWNA', 'SHOWNOACTIVATE', 'SHOWNORMAL')]
[string] $State = 'SHOW'
)
Begin {
$WindowStates = @{
'FORCEMINIMIZE' = 11
'HIDE' = 0
'MAXIMIZE' = 3
'MINIMIZE' = 6
'RESTORE' = 9
'SHOW' = 5
'SHOWDEFAULT' = 10
'SHOWMAXIMIZED' = 3
'SHOWMINIMIZED' = 2
'SHOWMINNOACTIVE' = 7
'SHOWNA' = 8
'SHOWNOACTIVATE' = 4
'SHOWNORMAL' = 1
}
$Win32ShowWindowAsync = Add-Type -MemberDefinition @'
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
'@ -Name "Win32ShowWindowAsync" -Namespace Win32Functions -PassThru
if (!$global:MainWindowHandles) {
$global:MainWindowHandles = @{ }
}
}
Process {
foreach ($process in $InputObject) {
if ($process.MainWindowHandle -eq 0) {
if ($global:MainWindowHandles.ContainsKey($process.Id)) {
$handle = $global:MainWindowHandles[$process.Id]
} else {
Write-Error "Main Window handle is '0'"
continue
}
} else {
$handle = $process.MainWindowHandle
$global:MainWindowHandles[$process.Id] = $handle
}
$Win32ShowWindowAsync::ShowWindowAsync($handle, $WindowStates[$State]) | Out-Null
Write-Verbose ("Set Window State '{1} on '{0}'" -f $MainWindowHandle, $State)
}
}
}
# URL
$Geturl = {
$promptURL = Read-Host -Prompt 'Enter URL or type "C"'
if ($promptURL -eq 'C')
{
if ((Get-Clipboard) -eq $null)
{
Write-Host "Problem with Userinput..."
Start-Sleep -Seconds 2
exit
}
else
{
$url = Get-Clipboard
}
}
else
{
if (($promptURL -match "/") -or ($promptURL -match "."))
{
$url = $promptURL
}
else
{
[System.Windows.Forms.MessageBox]::Show("Not a valid URL","Error",0) | Out-Null
cls
.$Geturl
}
}
# delete timecode
$regex = '&t[^ ]*'
$url = $url -replace $regex
}
# process
$download = {
cls
.$Geturl
# type
Write-Host 'Video / Audio'
$type = Read-Host -Prompt 'Choose a type'
# subtitles / vQuality
$vBoth = 1
if (($type -eq 'Video') -or ($type -eq 'V'))
{
Write-Host 'Y / N'
$sub = Read-Host -Prompt 'Subtitle?'
Write-Host ''
Write-Host 'high / best / both (bo)'
$vquality = Read-Host -Prompt 'Videoquality?'
if (($vquality -eq 'high') -or ($vquality -eq 'h') -or ($vquality -eq ''))
{
$fquality = "bestvideo+best"
}
elseif (($vquality -eq 'best') -or ($vquality -eq 'b'))
{
$fquality = "bestvideo+bestaudio"
$b = "_b"
}
elseif (($vquality -eq 'both') -or ($vquality -eq 'bo'))
{
$vBoth = 2
}
else
{
Write-Host "Problem with Userinput..."
Start-Sleep -Seconds 2
exit
}
}
else
{
if (($type -ne 'Audio') -and ($type -ne 'A'))
{
Write-Host "Problem with Userinput..."
Start-Sleep -Seconds 2
exit
}
}
# download
#$ErrorActionPreference = "SilentlyContinue"
if ((($type -eq 'Video') -or ($type -eq 'V')) -and (($sub -eq 'n') -or ($sub -eq '')))
{
if ($vBoth -eq 2)
{
$fquality = "bestvideo+bestaudio"
$b = "_b"
}
for ($i = 0; $i -lt $vBoth; $i++)
{
Write-Host 'Starting Youtube-dl...'
Start-Process $youtube_dl_location -ArgumentList ("-o $dir\%(title)s$b.%(ext)s -f $fquality --ignore-config --hls-prefer-native --ffmpeg-location $ffmpeg_location --merge-output-format mp4 $url")
if ($vBoth -eq 2)
{
$fquality = "bestvideo+best"
$b = ""
$cmd_process = (Get-Process youtube-dl).Id
Wait-Process $cmd_process
}
}
if ($vBoth -eq 2)
{
$ErrorActionPreference = 'Ignore'
$cmd_process = (Get-Process youtube-dl -ErrorAction Ignore).Id
Wait-Process $cmd_process
Start-Sleep -Seconds 1
$ErrorActionPreference = 'Continue'
$title1 = ((Get-ChildItem $dir) | Sort-Object CreationTime -Descending | Select-Object -Skip 1 | Select-Object -First 1).Name
$title2 = ((Get-ChildItem $dir) | Sort-Object CreationTime -Descending | Select-Object -First 1).Name
Write-Host "$title1 $title2"
$title = [System.IO.Path]::GetFileNameWithoutExtension("$dir\$title2")
New-Item -Path "$dir\$title" -ItemType Directory | Out-Null
Move-Item $dir\$title1 $dir\$title
Move-Item $dir\$title2 $dir\$title
}
}
elseif ((($type -eq 'Video') -or ($type -eq 'V')) -and ($sub -eq 'y'))
{
if ($vBoth -eq 2)
{
[System.Windows.Forms.MessageBox]::Show("You can´t download both qualities with subtitles.`nPlease try again with other options.","Error",0) | Out-Null
.$download
}
Write-Host 'Starting Youtube-dl...'
Start-Process $youtube_dl_location -ArgumentList ("-o $dir\%(title)s.%(ext)s -f $fquality --ignore-config --hls-prefer-native --write-sub --ffmpeg-location $ffmpeg_location --merge-output-format mp4 $url")
$ErrorActionPreference = 'Ignore'
$cmd_process = (Get-Process youtube-dl -ErrorAction Ignore).Id
Wait-Process $cmd_process
Start-Sleep -Seconds 1
$ErrorActionPreference = 'Continue'
$title1 = ((Get-ChildItem $dir) | Sort-Object CreationTime -Descending | Select-Object -Skip 1 | Select-Object -First 1).Name
$title2 = ((Get-ChildItem $dir) | Sort-Object CreationTime -Descending | Select-Object -First 1).Name
$title = [System.IO.Path]::GetFileNameWithoutExtension("$dir\$title2")
New-Item -Path "$dir\$title" -ItemType Directory | Out-Null
Move-Item $dir\$title1 $dir\$title
Move-Item $dir\$title2 $dir\$title
}
elseif (($type -eq 'Audio') -or ($type -eq 'A'))
{
Write-Host 'opus / mp3 / both (b)'
$quality = Read-Host -Prompt 'Choose a quality'
Write-Host 'Starting Youtube-dl...'
if (($quality -eq 'opus') -or ($quality -eq 'o'))
{
Start-Process $youtube_dl_location -ArgumentList ("-o $dir\%(title)s.%(ext)s -x --audio-format best --audio-quality 0 --ignore-config --hls-prefer-native --ffmpeg-location $ffmpeg_location $url")
}
elseif (($quality -eq 'mp3') -or ($quality -eq 'm'))
{
Start-Process $youtube_dl_location -ArgumentList ("-o $dir\%(title)s.%(ext)s -x --audio-format mp3 --audio-quality 0 --ignore-config --hls-prefer-native --ffmpeg-location $ffmpeg_location $url")
}
elseif (($quality -eq 'both') -or ($quality -eq 'b'))
{
Start-Process $youtube_dl_location -ArgumentList ("-o $dir\%(title)s.%(ext)s -x --audio-format mp3 --audio-quality 0 --ignore-config --hls-prefer-native --ffmpeg-location $ffmpeg_location $url")
$cmd_process = (Get-Process youtube-dl).Id
Wait-Process $cmd_process
Start-Process $youtube_dl_location -ArgumentList ("-o $dir\%(title)s.%(ext)s -x --audio-format best --audio-quality 0 --ignore-config --hls-prefer-native --ffmpeg-location $ffmpeg_location $url")
Start-Sleep -Seconds 1
$cmd_process = (Get-Process youtube-dl).Id
Wait-Process $cmd_process
Start-Sleep -Seconds 3
$title1 = ((Get-ChildItem $dir) | Sort-Object CreationTime -Descending | Select-Object -Skip 1 | Select-Object -First 1).Name
$title2 = ((Get-ChildItem $dir) | Sort-Object CreationTime -Descending | Select-Object -First 1).Name
$title = [System.IO.Path]::GetFileNameWithoutExtension("$dir\$title2")
New-Item -Path "$dir\$title" -ItemType Directory | Out-Null
Move-Item $dir\$title1 $dir\$title
Move-Item $dir\$title2 $dir\$title
}
else
{
Write-Host 'Thats not a valid quality'
$errorCount += 1
}
}
else
{
Write-Host 'Thats not a valid filetype'
$errorCount += 1
}
Write-Host ''
# hide toggle
$ErrorActionPreference = 'Ignore'
$pwsh_process = (Get-Process powershell) | Sort-Object StartTime | Select-Object -First 1
$ytdl_process = (Get-Process youtube-dl) | Sort-Object StartTime | Select-Object -First 1
$pwsh_process | Set-WindowState -State HIDE
$ytdl_process | Set-WindowState -State HIDE
$ErrorActionPreference = 'Continue'
# append log
if (($isLogging -eq $true) <#-and ($Error.Count -eq 0)#> -and ($errorCount -eq 0))
{
# create log at first launch
if (-not [System.IO.File]::Exists($logdir))
{
New-Item -Path "$logdir" -ItemType File
}
# append new file to log
if ((Get-VideoInfo -videoURL $url -infoType duration) -gt 3600)
{
$addon = ' time can differ'
}
# get title of video
if ($vBoth -eq 2 -or $sub -eq 'y' -or $quality -eq 'b')
{
$title = [System.IO.Path]::GetFileNameWithoutExtension("$dir\$((Get-ChildItem $dir) | Sort-Object CreationTime -Descending | Select-Object -First 1).Name")
} else
{
$title = Get-VideoInfo -videoURL $url -infoType title
$videoID = Get-VideoInfo -videoUrl $url -infoType id
$videoEtag = Get-VideoInfo -videoUrl $url -infoType etag
if ($type -eq 'V' -or $type -eq 'Video')
{
$title = $title + '.mp4'
} elseif ((($type -eq 'Audio') -or ($type -eq 'A')) -and ($quality -eq 'mp3' -or $quality -eq 'm'))
{
$title = $title + '.mp3'
} elseif ((($type -eq 'Audio') -or ($type -eq 'A')) -and ($quality -eq 'opus' -or $quality -eq 'o'))
{
$title = $title + '.opus'
}
}
Add-Content $logdir -Value "$(Get-Date -Format "MM/dd/yyyy HH:mm") $title$addon Id= $videoID; Etag= $videoEtag"
}
# add another URL
if ($vBoth -eq 2 -or $sub -eq 'y' -or $quality -eq 'b')
{
# wait until download finished
$cmd_process = (Get-Process youtube-dl -erroraction 'silentlycontinue').id
if ($cmd_process -ne $null -and $cmd_process -ne 0)
{
Wait-Process $cmd_process
}
}
$result = "Proceed", "Add URL" | Out-GridView -PassThru -Title "Choose option"
if ($result -eq 'Add URL')
{
$pwsh_process | Set-WindowState -State SHOW
&$download
}
# hide toggle
$ytdl_process | Set-WindowState -State SHOW
$cmd_process = (Get-Process youtube-dl -erroraction 'silentlycontinue').id
if ($cmd_process -ne $null -and $cmd_process -ne 0)
{
Wait-Process $cmd_process
}
$pwsh_process | Set-WindowState -State SHOW
# finish
cls
Write-Host 'Finished'
Read-Host "Press enter to exit..."
exit
}
# run
&$download