Skip to content

Commit c7cc8dc

Browse files
authored
Download FFmpeg via a project target (Tyrrrz#658)
1 parent 1041df0 commit c7cc8dc

File tree

4 files changed

+78
-66
lines changed

4 files changed

+78
-66
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,12 @@ jobs:
9292
with:
9393
dotnet-version: 9.0.x
9494

95-
- name: Download FFmpeg
96-
if: ${{ matrix.bundle-ffmpeg }}
97-
shell: pwsh
98-
run: YoutubeDownloader/DownloadFFmpeg.ps1 -platform ${{ matrix.rid }}
99-
10095
- name: Publish app
10196
run: >
10297
dotnet publish YoutubeDownloader
10398
-p:Version=${{ github.ref_type == 'tag' && github.ref_name || format('999.9.9-ci-{0}', github.sha) }}
10499
-p:CSharpier_Bypass=true
100+
-p:DownloadFFmpeg=${{ matrix.bundle-ffmpeg }}
105101
--output YoutubeDownloader/bin/publish
106102
--configuration Release
107103
--runtime ${{ matrix.rid }}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
param (
2+
[Parameter(Mandatory=$false)]
3+
[string]$Platform,
4+
5+
[Parameter(Mandatory=$false)]
6+
[string]$OutputPath
7+
)
8+
9+
$ErrorActionPreference = "Stop"
10+
11+
# If the platform is not specified, use the current OS/arch
12+
if (-not $Platform) {
13+
$arch = [Runtime.InteropServices.RuntimeInformation]::OSArchitecture
14+
15+
if ($isWindows) {
16+
$Platform = "windows-$arch"
17+
} elseif ($isLinux) {
18+
$Platform = "linux-$arch"
19+
} elseif ($isMacOS) {
20+
$Platform = "osx-$arch"
21+
} else {
22+
throw "Unsupported platform"
23+
}
24+
}
25+
26+
# Normalize platform identifier
27+
$Platform = $Platform.ToLower().Replace("win-", "windows-")
28+
$fileName = if ($Platform.Contains("windows-")) { "ffmpeg.exe" } else { "ffmpeg" }
29+
30+
# If the output path is not specified, use the current directory
31+
if (-not $OutputPath) {
32+
$OutputPath = "$PSScriptRoot/$fileName"
33+
}
34+
35+
# If the output path is an existing directory, append the default file name for the platform
36+
if (Test-Path $OutputPath -PathType Container) {
37+
$OutputPath = Join-Path $OutputPath $fileName
38+
}
39+
40+
# Delete the existing file if it exists
41+
if (Test-Path $OutputPath) {
42+
Remove-Item $OutputPath
43+
}
44+
45+
# Download the archive
46+
Write-Host "Downloading FFmpeg for $Platform..."
47+
$http = New-Object System.Net.WebClient
48+
try {
49+
$http.DownloadFile("https://github.com/Tyrrrz/FFmpegBin/releases/download/7.0/ffmpeg-$Platform.zip", "$OutputPath.zip")
50+
} finally {
51+
$http.Dispose()
52+
}
53+
54+
try {
55+
# Extract FFmpeg
56+
Add-Type -Assembly System.IO.Compression.FileSystem
57+
$zip = [IO.Compression.ZipFile]::OpenRead("$OutputPath.zip")
58+
try {
59+
[IO.Compression.ZipFileExtensions]::ExtractToFile($zip.GetEntry($fileName), $OutputPath)
60+
} finally {
61+
$zip.Dispose()
62+
}
63+
64+
Write-Host "Done downloading FFmpeg."
65+
} finally {
66+
# Clean up
67+
Remove-Item "$OutputPath.zip" -Force
68+
}

YoutubeDownloader/DownloadFFmpeg.ps1

Lines changed: 0 additions & 61 deletions
This file was deleted.

YoutubeDownloader/YoutubeDownloader.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
1313
</PropertyGroup>
1414

15+
<PropertyGroup>
16+
<DownloadFFmpeg>true</DownloadFFmpeg>
17+
</PropertyGroup>
18+
1519
<ItemGroup>
1620
<AvaloniaResource Include="..\favicon.ico" Link="favicon.ico" />
1721
</ItemGroup>
@@ -60,4 +64,9 @@
6064
<TrimmerRootAssembly Include="WebView.Core" />
6165
</ItemGroup>
6266

67+
<Target Name="DownloadFFmpeg" BeforeTargets="Restore;PreBuildEvent" Condition="$(DownloadFFmpeg) AND !Exists('ffmpeg.exe') AND !Exists('ffmpeg')">
68+
<Exec Command="pwsh -ExecutionPolicy Bypass -File $(ProjectDir)/Download-FFmpeg.ps1 -Platform $(RuntimeIdentifier) -OutputPath $(ProjectDir)" LogStandardErrorAsError="true" Condition="'$(RuntimeIdentifier)' != ''" />
69+
<Exec Command="pwsh -ExecutionPolicy Bypass -File $(ProjectDir)/Download-FFmpeg.ps1 -OutputPath $(ProjectDir)" LogStandardErrorAsError="true" Condition="'$(RuntimeIdentifier)' == ''" />
70+
</Target>
71+
6372
</Project>

0 commit comments

Comments
 (0)