/
updater.ps1
577 lines (527 loc) · 18.6 KB
/
updater.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
$fallback7z = Join-Path (Get-Location) "\7z\7zr.exe";
$useragent = "mpv-win-updater"
function Get-7z {
$7z_command = Get-Command -CommandType Application -ErrorAction Ignore 7z.exe | Select-Object -Last 1
if ($7z_command) {
return $7z_command.Source
}
$7zdir = Get-ItemPropertyValue -ErrorAction Ignore "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\7-Zip" "InstallLocation"
if ($7zdir -and (Test-Path (Join-Path $7zdir "7z.exe"))) {
return Join-Path $7zdir "7z.exe"
}
if (Test-Path $fallback7z) {
return $fallback7z
}
return $null
}
function Check-7z {
if (-not (Get-7z))
{
$null = New-Item -ItemType Directory -Force (Split-Path $fallback7z)
$download_file = $fallback7z
Write-Host "Downloading 7zr.exe" -ForegroundColor Green
Invoke-WebRequest -Uri "https://www.7-zip.org/a/7zr.exe" -UserAgent $useragent -OutFile $download_file
}
else
{
Write-Host "7z already exist. Skipped download" -ForegroundColor Green
}
}
function Check-PowershellVersion {
$version = $PSVersionTable.PSVersion.Major
Write-Host "Checking Windows PowerShell version -- $version" -ForegroundColor Green
if ($version -le 2)
{
Write-Host "Using Windows PowerShell $version is unsupported. Upgrade your Windows PowerShell." -ForegroundColor Red
throw
}
}
function Check-Ytplugin {
$ytdlp = Get-ChildItem "yt-dlp*.exe" -ErrorAction Ignore
$youtubedl = Get-ChildItem "youtube-dl.exe" -ErrorAction Ignore
if ($ytdlp) {
return $ytdlp.ToString()
}
elseif ($youtubedl) {
return $youtubedl.ToString()
}
else {
return $null
}
}
function Check-Ytplugin-In-System {
$ytp = Get-Command -CommandType Application -ErrorAction Ignore yt-dlp.exe | Select-Object -Last 1
if (-not $ytp) {
$ytp = Get-Command -CommandType Application -ErrorAction Ignore youtube-dl.exe | Select-Object -Last 1
}
return [bool]($ytp -and ((Split-Path $ytp.Source) -ne (Get-Location)))
}
function Check-Mpv {
$mpv = (Get-Location).Path + "\mpv.exe"
$is_exist = Test-Path $mpv
return $is_exist
}
function Download-Archive ($filename, $link) {
Write-Host "Downloading" $filename -ForegroundColor Green
Invoke-WebRequest -Uri $link -UserAgent $useragent -OutFile $filename
}
function Download-Ytplugin ($plugin, $version) {
$link = ""
$plugin_exe = ""
switch -wildcard ($plugin) {
"yt-dlp*" {
Write-Host "Downloading $plugin ($version)" -ForegroundColor Green
$32bit = ""
if (-Not (Test-Path (Join-Path $env:windir "SysWow64"))) {
$32bit = "_x86"
}
$link = -join("https://github.com/yt-dlp/yt-dlp/releases/download/", $version, "/", $plugin, $32bit, ".exe")
$plugin_exe = -join($plugin, $32bit, ".exe")
}
"youtube-dl" {
Write-Host "Downloading $plugin ($version)" -ForegroundColor Green
$link = -join("https://yt-dl.org/downloads/", $version, "/youtube-dl.exe")
$plugin_exe = "youtube-dl.exe"
}
}
Invoke-WebRequest -Uri $link -UserAgent $useragent -OutFile $plugin_exe
}
function Extract-Archive ($file) {
$7z = Get-7z
Write-Host "Extracting" $file -ForegroundColor Green
& $7z x -y $file
}
function Get-Latest-Mpv($Arch, $channel) {
$filename = ""
$download_link = ""
switch -wildcard ($channel) {
"daily" {
$api_gh = "https://api.github.com/repos/shinchiro/mpv-winbuild-cmake/releases/latest"
$json = Invoke-WebRequest $api_gh -MaximumRedirection 0 -ErrorAction Ignore -UseBasicParsing | ConvertFrom-Json
$filename = $json.assets | where { $_.name -Match "mpv-$Arch" } | Select-Object -ExpandProperty name
$download_link = $json.assets | where { $_.name -Match "mpv-$Arch" } | Select-Object -ExpandProperty browser_download_url
}
"weekly" {
$i686_link = "https://sourceforge.net/projects/mpv-player-windows/rss?path=/32bit"
$x86_64_link = "https://sourceforge.net/projects/mpv-player-windows/rss?path=/64bit"
$x86_64v3_link = "https://sourceforge.net/projects/mpv-player-windows/rss?path=/64bit-v3"
$rss_link = ''
switch ($Arch)
{
i686 { $rss_link = $i686_link}
x86_64 { $rss_link = $x86_64_link }
x86_64-v3 { $rss_link = $x86_64v3_link }
}
Write-Host "Fetching RSS feed for mpv" -ForegroundColor Green
$result = [xml](New-Object System.Net.WebClient).DownloadString($rss_link)
$latest = $result.rss.channel.item.link[0]
$tempname = $latest.split("/")[-2]
$filename = [System.Uri]::UnescapeDataString($tempname)
$download_link = "https://download.sourceforge.net/mpv-player-windows/" + $filename
}
}
if ($filename -is [array]) {
return $filename[0], $download_link[0]
}
else {
return $filename, $download_link
}
}
function Get-Latest-Ytplugin ($plugin) {
switch -wildcard ($plugin) {
"yt-dlp*" {
$link = "https://github.com/yt-dlp/yt-dlp/releases.atom"
Write-Host "Fetching RSS feed for ytp-dlp" -ForegroundColor Green
$resp = [xml](Invoke-WebRequest $link -MaximumRedirection 0 -ErrorAction Ignore -UseBasicParsing).Content
$link = $resp.feed.entry[0].link.href
$version = $link.split("/")[-1]
return $version
}
"youtube-dl" {
$link = "https://yt-dl.org/downloads/latest/youtube-dl.exe"
Write-Host "Fetching RSS feed for youtube-dl" -ForegroundColor Green
$resp = Invoke-WebRequest $link -MaximumRedirection 0 -ErrorAction Ignore -UseBasicParsing
$redirect_link = $resp.Headers.Location
$version = $redirect_link.split("/")[4]
return $version
}
}
}
function Get-Latest-FFmpeg ($Arch) {
$api_gh = "https://api.github.com/repos/shinchiro/mpv-winbuild-cmake/releases/latest"
$json = Invoke-WebRequest $api_gh -MaximumRedirection 0 -ErrorAction Ignore -UseBasicParsing | ConvertFrom-Json
$filename = $json.assets | where { $_.name -Match "ffmpeg-$Arch" } | Select-Object -ExpandProperty name
$download_link = $json.assets | where { $_.name -Match "ffmpeg-$Arch" } | Select-Object -ExpandProperty browser_download_url
if ($filename -is [array]) {
return $filename[0], $download_link[0]
}
else {
return $filename, $download_link
}
}
function Get-Arch {
# Reference: http://superuser.com/a/891443
$FilePath = [System.IO.Path]::Combine((Get-Location).Path, 'mpv.exe')
[int32]$MACHINE_OFFSET = 4
[int32]$PE_POINTER_OFFSET = 60
[byte[]]$data = New-Object -TypeName System.Byte[] -ArgumentList 4096
$stream = New-Object -TypeName System.IO.FileStream -ArgumentList ($FilePath, 'Open', 'Read')
$stream.Read($data, 0, 4096) | Out-Null
# DOS header is 64 bytes, last element, long (4 bytes) is the address of the PE header
[int32]$PE_HEADER_ADDR = [System.BitConverter]::ToInt32($data, $PE_POINTER_OFFSET)
[int32]$machineUint = [System.BitConverter]::ToUInt16($data, $PE_HEADER_ADDR + $MACHINE_OFFSET)
$result = "" | select FilePath, FileType
$result.FilePath = $FilePath
switch ($machineUint)
{
0 { $result.FileType = 'Native' }
0x014c { $result.FileType = 'i686' } # 32bit
0x0200 { $result.FileType = 'Itanium' }
0x8664 { $result.FileType = 'x86_64' } # 64bit
}
$stream.Close()
$result
}
function ExtractGitFromFile {
$stripped = .\mpv --no-config | select-string "mpv" | select-object -First 1
$pattern = "-g([a-z0-9-]{7})"
$bool = $stripped -match $pattern
return $matches[1]
}
function ExtractGitFromURL($filename) {
$pattern = "-git-([a-z0-9-]{7})"
$bool = $filename -match $pattern
return $matches[1]
}
function ExtractDateFromFile {
$date = (Get-Item ./mpv.exe).LastWriteTimeUtc
$day = $date.Day.ToString("00")
$month = $date.Month.ToString("00")
$year = $date.Year.ToString("0000")
return "$year$month$day"
}
function ExtractDateFromURL($filename) {
$pattern = "mpv-[xi864_].*-([0-9]{8})-git-([a-z0-9-]{7})"
$bool = $filename -match $pattern
return $matches[1]
}
function Test-Admin
{
$user = [Security.Principal.WindowsIdentity]::GetCurrent();
(New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
function Create-XML {
@"
<settings>
<channel>unset</channel>
<arch>unset</arch>
<autodelete>unset</autodelete>
<getffmpeg>unset</getffmpeg>
</settings>
"@ | Set-Content "settings.xml" -Encoding UTF8
}
function Check-ChannelRelease {
$channel = ""
$file = "settings.xml"
if (-not (Test-Path $file)) {
$result = Read-KeyOrTimeout "Choose mpv updates frequency, weekly or daily? [1=weekly/2=daily] (default=1)" "D1"
Write-Host ""
if ($result -eq 'D1') {
$channel = "weekly"
}
elseif ($result -eq 'D2') {
$channel = "daily"
}
else {
throw "Please enter valid input key."
}
Create-XML
[xml]$doc = Get-Content $file
$doc.settings.channel = $channel
$doc.Save($file)
}
else {
[xml]$doc = Get-Content $file
$channel = $doc.settings.channel
}
return $channel
}
function Check-Arch($arch) {
$get_arch = ""
$file = "settings.xml"
if (-not (Test-Path $file)) { exit }
[xml]$doc = Get-Content $file
if ($doc.settings.arch -eq "unset") {
if ($arch -eq "i686") {
$get_arch = "i686"
}
else {
$result = Read-KeyOrTimeout "Choose variant for 64bit builds: x86_64 or x86_64-v3 (for cpu with AVX2 support) [1=x86_64 / 2=x86_64-v3 (default=1)" "D1"
Write-Host ""
if ($result -eq 'D1') {
$get_arch = "x86_64"
}
elseif ($result -eq 'D2') {
$get_arch = "x86_64-v3"
}
else {
throw "Please enter valid input key."
}
}
$doc.settings.arch = $get_arch
$doc.Save($file)
}
else {
$get_arch = $doc.settings.arch
}
return $get_arch
}
function Check-Autodelete($archive) {
$autodelete = ""
$file = "settings.xml"
if (-not (Test-Path $file)) { exit }
[xml]$doc = Get-Content $file
if ($doc.settings.autodelete -eq "unset") {
$result = Read-KeyOrTimeout "Delete archives after extract? [Y/n] (default=Y)" "Y"
Write-Host ""
if ($result -eq 'Y') {
$autodelete = "true"
}
elseif ($result -eq 'N') {
$autodelete = "false"
}
else {
throw "Please enter valid input key."
}
$doc.settings.autodelete = $autodelete
$doc.Save($file)
}
if ($doc.settings.autodelete -eq "true") {
if (Test-Path $archive)
{
Remove-Item -Force $archive
}
}
}
function Check-GetFFmpeg() {
$get_ffmpeg = ""
$file = "settings.xml"
if (-not (Test-Path $file)) { exit }
[xml]$doc = Get-Content $file
if ($doc.settings.getffmpeg -eq "unset") {
Write-Host "FFmpeg doesn't exist. " -ForegroundColor Green -NoNewline
$result = Read-KeyOrTimeout "Proceed with downloading? [Y/n] (default=n)" "N"
Write-Host ""
if ($result -eq 'Y') {
$get_ffmpeg = "true"
}
elseif ($result -eq 'N') {
$get_ffmpeg = "false"
}
else {
throw "Please enter valid input key."
}
$doc.settings.getffmpeg = $get_ffmpeg
$doc.Save($file)
}
else {
$get_ffmpeg = $doc.settings.getffmpeg
}
return $get_ffmpeg
}
function Upgrade-Mpv {
$need_download = $false
$remoteName = ""
$download_link = ""
$arch = ""
$channel = ""
if (Check-Mpv) {
$channel = Check-ChannelRelease
$file_arch = (Get-Arch).FileType
$arch = Check-Arch $file_arch
$remoteName, $download_link = Get-Latest-Mpv $arch $channel
$localgit = ExtractGitFromFile
$localdate = ExtractDateFromFile
$remotegit = ExtractGitFromURL $remoteName
$remotedate = ExtractDateFromURL $remoteName
if ($localgit -match $remotegit)
{
if ($localdate -match $remotedate)
{
Write-Host "You are already using latest mpv build -- $remoteName" -ForegroundColor Green
$need_download = $false
}
else {
Write-Host "Newer mpv build available" -ForegroundColor Green
$need_download = $true
}
}
else {
Write-Host "Newer mpv build available" -ForegroundColor Green
$need_download = $true
}
}
else {
Write-Host "mpv doesn't exist. " -ForegroundColor Green -NoNewline
$result = Read-KeyOrTimeout "Proceed with downloading? [Y/n] (default=y)" "Y"
Write-Host ""
if ($result -eq 'Y') {
$need_download = $true
if (Test-Path (Join-Path $env:windir "SysWow64")) {
Write-Host "Detecting System Type is 64-bit" -ForegroundColor Green
$original_arch = "x86_64"
}
else {
Write-Host "Detecting System Type is 32-bit" -ForegroundColor Green
$original_arch = "i686"
}
$channel = Check-ChannelRelease
$arch = Check-Arch $original_arch
$remoteName, $download_link = Get-Latest-Mpv $arch $channel
}
elseif ($result -eq 'N') {
$need_download = $false
}
else {
throw "Please enter valid input key."
}
}
if ($need_download) {
Download-Archive $remoteName $download_link
Check-7z
Extract-Archive $remoteName
}
Check-Autodelete $remoteName
}
function Upgrade-Ytplugin {
if (Check-Ytplugin-In-System) {
Write-Host "yt-dlp.exe or youtube-dl.exe already exists in your system, skip the update check." -ForegroundColor Green
return
}
$yt = Check-Ytplugin
if ($yt) {
$latest_release = Get-Latest-Ytplugin((Get-Item $yt).BaseName)
if ((& $yt --version) -match ($latest_release)) {
Write-Host "You are already using latest" (Get-Item $yt).BaseName "-- $latest_release" -ForegroundColor Green
}
else {
Write-Host "Newer" (Get-Item $yt).BaseName "build available" -ForegroundColor Green
& $yt --update
}
}
else {
Write-Host "ytdlp or youtube-dl doesn't exist. " -ForegroundColor Green -NoNewline
$result = Read-KeyOrTimeout "Proceed with downloading? [Y/n] (default=n)" "N"
Write-Host ""
if ($result -eq 'Y') {
$result_exe = Read-KeyOrTimeout "Download ytdlp or youtubedl? [1=ytdlp/2=youtubedl] (default=1)" "D1"
Write-Host ""
if ($result_exe -eq 'D1') {
$latest_release = Get-Latest-Ytplugin "yt-dlp"
Download-Ytplugin "yt-dlp" $latest_release
}
elseif ($result_exe -eq 'D2') {
$latest_release = Get-Latest-Ytplugin "youtube-dl"
Download-Ytplugin "youtube-dl" $latest_release
}
else {
throw "Please enter valid input key."
}
}
}
}
function Upgrade-FFmpeg {
$get_ffmpeg = Check-GetFFmpeg
if ($get_ffmpeg -eq "false") {
return
}
if (Test-Path (Join-Path $env:windir "SysWow64")) {
$original_arch = "x86_64"
$arch = Check-Arch $original_arch
}
else {
$arch = "i686"
}
$need_download = $false
$remote_name, $download_link = Get-Latest-FFmpeg $arch
$ffmpeg = (Get-Location).Path + "\ffmpeg.exe"
$ffmpeg_exist = Test-Path $ffmpeg
if ($ffmpeg_exist) {
$ffmpeg_file = .\ffmpeg -version | select-string "ffmpeg" | select-object -First 1
$file_pattern_1 = "git-[0-9]{4}-[0-9]{2}-[0-9]{2}-(?<commit>[a-z0-9]+)" # git-2023-01-02-cc2b1a325
$file_pattern_2 = "N-\d+-g(?<commit>[a-z0-9]+)" # N-109751-g9a820ec8b
$file_pattern = $file_pattern_1, $file_pattern_2 -join '|'
$url_pattern = "git-([a-z0-9]+)"
$file_match= [Regex]::Matches($ffmpeg_file, $file_pattern)
$remote_match = [Regex]::Matches($remote_name, $url_pattern)
$local_git = $file_match[0].groups['commit'].value
$remote_git = $remote_match[0].groups[1].value
if ($local_git -match $remote_git) {
Write-Host "You are already using latest ffmpeg build -- $remote_name" -ForegroundColor Green
$need_download = $false
}
else {
Write-Host "Newer ffmpeg build available" -ForegroundColor Green
$need_download = $true
}
}
else {
$need_download = $true
}
if ($need_download) {
Download-Archive $remote_name $download_link
Check-7z
Extract-Archive $remote_name
}
Check-Autodelete $remote_name
}
function Read-KeyOrTimeout ($prompt, $key){
$seconds = 9
$startTime = Get-Date
$timeOut = New-TimeSpan -Seconds $seconds
Write-Host "$prompt " -ForegroundColor Green
# Basic progress bar
[Console]::CursorLeft = 0
[Console]::Write("[")
[Console]::CursorLeft = $seconds + 2
[Console]::Write("]")
[Console]::CursorLeft = 1
while (-not [System.Console]::KeyAvailable) {
$currentTime = Get-Date
Start-Sleep -s 1
Write-Host "#" -ForegroundColor Green -NoNewline
if ($currentTime -gt $startTime + $timeOut) {
Break
}
}
if ([System.Console]::KeyAvailable) {
$response = [System.Console]::ReadKey($true).Key
}
else {
$response = $key
}
return $response.ToString()
}
#
# Main script entry point
#
if (Test-Admin) {
Write-Host "Running script with administrator privileges" -ForegroundColor Yellow
}
else {
Write-Host "Running script without administrator privileges" -ForegroundColor Red
}
try {
Check-PowershellVersion
# Sourceforge only support TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$global:progressPreference = 'silentlyContinue'
Upgrade-Mpv
Upgrade-Ytplugin
Upgrade-FFmpeg
Write-Host "Operation completed" -ForegroundColor Magenta
}
catch [System.Exception] {
Write-Host $_.Exception.Message -ForegroundColor Red
exit 1
}