Skip to content

Commit

Permalink
Detach FFmpeg downloading script from the build process
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz committed Aug 22, 2024
1 parent 593673c commit 427dfd0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 37 deletions.
31 changes: 22 additions & 9 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ jobs:
bundle-ffmpeg:
- true
- false
include:
- bundle-ffmpeg: true
artifact-name-base: YoutubeDownloader
- bundle-ffmpeg: false
artifact-name-base: YoutubeDownloader.Bare
exclude:
# FFmpeg builds for these platforms are not easily available
- bundle-ffmpeg: true
Expand All @@ -76,7 +81,8 @@ jobs:

- name: Download FFmpeg
if: ${{ matrix.bundle-ffmpeg }}
run: dotnet build YoutubeDownloader -t:DownloadFFmpeg
shell: pwsh
run: YoutubeDownloader/DownloadFFmpeg.ps1 -platform ${{ matrix.rid }}

- name: Publish app
run: >
Expand All @@ -91,7 +97,7 @@ jobs:
- name: Upload artifacts
uses: actions/upload-artifact@0b2256b8c012f0828dc542b3febcab082c67f72b # v4.3.4
with:
name: ${{ matrix.bundle-ffmpeg && 'YoutubeDownloader' || 'YoutubeDownloader.Bare' }}.${{ matrix.rid }}
name: ${{ matrix.artifact-name-base }}.${{ matrix.rid }}
path: YoutubeDownloader/bin/publish
if-no-files-found: error

Expand Down Expand Up @@ -135,6 +141,11 @@ jobs:
bundle-ffmpeg:
- true
- false
include:
- bundle-ffmpeg: true
artifact-name-base: YoutubeDownloader
- bundle-ffmpeg: false
artifact-name-base: YoutubeDownloader.Bare
exclude:
# FFmpeg builds for these platforms are not easily available
- bundle-ffmpeg: true
Expand All @@ -151,26 +162,28 @@ jobs:
- name: Download artifacts
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
with:
name: ${{ matrix.bundle-ffmpeg && 'YoutubeDownloader' || 'YoutubeDownloader.Bare' }}.${{ matrix.rid }}
name: ${{ artifact-name-base }}.${{ matrix.rid }}
path: YoutubeDownloader/

- name: Set permissions
- name: Set permissions (app)
if: ${{ !startsWith(matrix.rid, 'win-') }}
run: |
chmod +x YoutubeDownloader/YoutubeDownloader
${{ matrix.bundle-ffmpeg && 'chmod +x YoutubeDownloader/ffmpeg' || '' }}
run: chmod +x YoutubeDownloader/YoutubeDownloader

- name: Set permissions (FFmpeg)
if: ${{ !startsWith(matrix.rid, 'win-') && matrix.bundle-ffmpeg }}
run: chmod +x YoutubeDownloader/ffmpeg

- name: Create package
# Change into the artifacts directory to avoid including the directory itself in the zip archive
working-directory: YoutubeDownloader/
run: zip -r ../${{ matrix.bundle-ffmpeg && 'YoutubeDownloader' || 'YoutubeDownloader.Bare' }}.${{ matrix.rid }}.zip .
run: zip -r ../${{ artifact-name-base }}.${{ matrix.rid }}.zip .

- name: Upload release asset
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: >
gh release upload ${{ github.ref_name }}
${{ matrix.bundle-ffmpeg && 'YoutubeDownloader' || 'YoutubeDownloader.Bare' }}.${{ matrix.rid }}.zip
${{ artifact-name-base }}.${{ matrix.rid }}.zip
--repo ${{ github.event.repository.full_name }}
notify:
Expand Down
31 changes: 27 additions & 4 deletions YoutubeDownloader/DownloadFFmpeg.ps1
Original file line number Diff line number Diff line change
@@ -1,18 +1,41 @@
# This script is called from inside an MSBuild task to download FFmpeg binaries:
# dotnet build -t:DownloadFFmpeg

param (
[string]$platform,
[string]$outputPath
)

$ErrorActionPreference = "Stop"

# If the platform is not specified, use the current OS/arch
if (-not $platform) {
$arch = [Runtime.InteropServices.RuntimeInformation]::OSArchitecture

if ($isWindows) {
$platform = "windows-$arch"
} elseif ($isLinux) {
$platform = "linux-$arch"
} elseif ($isMacOS) {
$platform = "osx-$arch"
} else {
throw "Unsupported platform"
}
}

# Normalize platform identifier
$platform = $platform.ToLower().Replace("win-", "windows-")

# If the output path is not specified, use the current directory
if (-not $outputPath) {
$fileName = if ($platform.Contains("windows-")) { "ffmpeg.exe" } else { "ffmpeg" }
$outputPath = "$PSScriptRoot/$fileName"
}

# Delete the existing file if it exists
if (Test-Path $outputPath) {
Remove-Item $outputPath
}

# Download the archive
Write-Host "Downloading FFmpeg..."
Write-Host "Downloading FFmpeg for $platform..."
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$http = New-Object System.Net.WebClient
try {
Expand Down
29 changes: 5 additions & 24 deletions YoutubeDownloader/YoutubeDownloader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
<AvaloniaResource Include="..\favicon.ico" Link="favicon.ico" />
</ItemGroup>

<ItemGroup>
<None Include="ffmpeg.exe" CopyToOutputDirectory="PreserveNewest" Condition="Exists('ffmpeg.exe')" />
<None Include="ffmpeg" CopyToOutputDirectory="PreserveNewest" Condition="Exists('ffmpeg')" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="AsyncImageLoader.Avalonia" Version="3.2.1" />
<PackageReference Include="Avalonia" Version="11.1.3" />
Expand Down Expand Up @@ -53,28 +58,4 @@
<TrimmerRootAssembly Include="WebView.Core" />
</ItemGroup>

<!-- Task to download FFmpeg -->
<PropertyGroup>
<FFmpegPlatform>$(RuntimeIdentifier)</FFmpegPlatform>
<FFmpegPlatform Condition="'$(FFmpegPlatform)' == '' AND $([MSBuild]::IsOsPlatform('Windows')) AND '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'Arm64'">win-arm64</FFmpegPlatform>
<FFmpegPlatform Condition="'$(FFmpegPlatform)' == '' AND $([MSBuild]::IsOsPlatform('Windows')) AND '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'X86'">win-x86</FFmpegPlatform>
<FFmpegPlatform Condition="'$(FFmpegPlatform)' == '' AND $([MSBuild]::IsOsPlatform('Windows')) AND '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'X64'">win-x64</FFmpegPlatform>
<FFmpegPlatform Condition="'$(FFmpegPlatform)' == '' AND $([MSBuild]::IsOsPlatform('Linux')) AND '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'Arm64'">linux-arm64</FFmpegPlatform>
<FFmpegPlatform Condition="'$(FFmpegPlatform)' == '' AND $([MSBuild]::IsOsPlatform('Linux')) AND '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'X86'">linux-x86</FFmpegPlatform>
<FFmpegPlatform Condition="'$(FFmpegPlatform)' == '' AND $([MSBuild]::IsOsPlatform('Linux')) AND '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'X64'">linux-x64</FFmpegPlatform>
<FFmpegPlatform Condition="'$(FFmpegPlatform)' == '' AND $([MSBuild]::IsOsPlatform('OSX')) AND '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'Arm64'">osx-arm64</FFmpegPlatform>
<FFmpegPlatform Condition="'$(FFmpegPlatform)' == '' AND $([MSBuild]::IsOsPlatform('OSX')) AND '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'X64'">osx-x64</FFmpegPlatform>
<FFmpegFileName Condition="$(FFmpegPlatform.StartsWith('win-'))">ffmpeg.exe</FFmpegFileName>
<FFmpegFileName Condition="'$(FFmpegFileName)' == ''">ffmpeg</FFmpegFileName>
</PropertyGroup>

<Target Name="DownloadFFmpeg">
<Exec Command="pwsh -ExecutionPolicy Bypass -File &quot;$(ProjectDir)/DownloadFFmpeg.ps1&quot; -Platform $(FFmpegPlatform) -OutputPath &quot;$(ProjectDir)/$(FFmpegFileName)&quot;" LogStandardErrorAsError="true" />
</Target>

<ItemGroup>
<None Include="ffmpeg.exe" CopyToOutputDirectory="PreserveNewest" Condition="Exists('ffmpeg.exe')" />
<None Include="ffmpeg" CopyToOutputDirectory="PreserveNewest" Condition="Exists('ffmpeg')" />
</ItemGroup>

</Project>

0 comments on commit 427dfd0

Please sign in to comment.