1$url = "https://raw.githubusercontent.com/opencv/opencv_3rdparty/@FFMPEG_BINARIES_COMMIT@/ffmpeg/opencv_videoio_ffmpeg_64.dll"
2$expected_md5 = "@FFMPEG_FILE_HASH_BIN64@"
3$output = "$PSScriptRoot\@OPENCV_BIN_INSTALL_PATH@\opencv_videoio_ffmpeg@OPENCV_DLLVERSION@_64.dll"
4
5Write-Output ("=" * 120)
6try {
7    Get-content -Path "$PSScriptRoot\@OPENCV_LICENSES_INSTALL_PATH@\ffmpeg-readme.txt" -ErrorAction 'Stop'
8} catch {
9    Write-Output "Refer to OpenCV FFmpeg wrapper readme notes about library usage / licensing details."
10}
11Write-Output ("=" * 120)
12Write-Output ""
13
14if(![System.IO.File]::Exists($output)) {
15    try {
16        [io.file]::OpenWrite($output).close()
17    } catch {
18        Write-Warning "Unable to write: $output"
19        if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
20            Write-Warning "Launching with 'Administrator' elevated privileges..."
21            Pause
22            Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
23            exit
24        } else {
25            Write-Output "FATAL: Unable to write with elevated privileges: $output"
26            Pause
27            exit 1
28        }
29    }
30
31    try {
32        Write-Output ("Downloading: " + $output)
33        Import-Module BitsTransfer
34        $start_time = Get-Date
35        Start-BitsTransfer -Source $url -Destination $output -ErrorAction 'Stop'
36        Write-Output "Downloaded in $((Get-Date).Subtract($start_time).Seconds) seconds"
37    } catch {
38        $_ # Dump error
39        try {
40            Write-Output ("Downloading (second attempt): " + $output)
41            $start_time = Get-Date
42            Invoke-WebRequest -Uri $url -OutFile $output
43            Write-Output "Downloaded in $((Get-Date).Subtract($start_time).Seconds) seconds"
44        } catch {
45            Write-Output ("Can't download file: " + $output)
46            Write-Output ("URL: " + $url)
47            Write-Output "You need to download this file manually. Stop"
48            Pause
49            Exit
50        }
51    }
52} else {
53    Write-Output ("File exists: " + $output)
54    Write-Output ("Downloading is skipped. Remove this file and re-run this script to force downloading.")
55}
56
57if(![System.IO.File]::Exists($output)) {
58    Write-Output ("Destination file not found: " + $output)
59    Write-Output "Stop"
60    Pause
61    Exit
62}
63
64try {
65    $hash = Get-FileHash $output -Algorithm MD5 -ErrorAction 'Stop'
66
67    if($hash.Hash -eq $expected_md5) {
68        Write-Output "MD5 check passed"
69    } else {
70        Write-Output ("MD5     : " + $hash.Hash.toLower())
71        Write-Output ("Expected: " + $expected_md5)
72        Write-Output "MD5 hash mismatch"
73    }
74} catch {
75    $_ # Dump error
76    Write-Output "Can't check MD5 hash (requires PowerShell 4+)"
77}
78Pause
79Write-Output "Exit"
80