From c7cc8dc6ef92f34ed5dbad60a607e7d84a938f87 Mon Sep 17 00:00:00 2001
From: Oleksii Holub <1935960+Tyrrrz@users.noreply.github.com>
Date: Sun, 11 May 2025 02:12:17 +0300
Subject: [PATCH] Download FFmpeg via a project target (#658)
---
.github/workflows/main.yml | 6 +-
YoutubeDownloader/Download-FFmpeg.ps1 | 68 ++++++++++++++++++++++
YoutubeDownloader/DownloadFFmpeg.ps1 | 61 -------------------
YoutubeDownloader/YoutubeDownloader.csproj | 9 +++
4 files changed, 78 insertions(+), 66 deletions(-)
create mode 100644 YoutubeDownloader/Download-FFmpeg.ps1
delete mode 100644 YoutubeDownloader/DownloadFFmpeg.ps1
diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index a6ff706..fad35e8 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -92,16 +92,12 @@ jobs:
with:
dotnet-version: 9.0.x
- - name: Download FFmpeg
- if: ${{ matrix.bundle-ffmpeg }}
- shell: pwsh
- run: YoutubeDownloader/DownloadFFmpeg.ps1 -platform ${{ matrix.rid }}
-
- name: Publish app
run: >
dotnet publish YoutubeDownloader
-p:Version=${{ github.ref_type == 'tag' && github.ref_name || format('999.9.9-ci-{0}', github.sha) }}
-p:CSharpier_Bypass=true
+ -p:DownloadFFmpeg=${{ matrix.bundle-ffmpeg }}
--output YoutubeDownloader/bin/publish
--configuration Release
--runtime ${{ matrix.rid }}
diff --git a/YoutubeDownloader/Download-FFmpeg.ps1 b/YoutubeDownloader/Download-FFmpeg.ps1
new file mode 100644
index 0000000..75ae81d
--- /dev/null
+++ b/YoutubeDownloader/Download-FFmpeg.ps1
@@ -0,0 +1,68 @@
+param (
+ [Parameter(Mandatory=$false)]
+ [string]$Platform,
+
+ [Parameter(Mandatory=$false)]
+ [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-")
+$fileName = if ($Platform.Contains("windows-")) { "ffmpeg.exe" } else { "ffmpeg" }
+
+# If the output path is not specified, use the current directory
+if (-not $OutputPath) {
+ $OutputPath = "$PSScriptRoot/$fileName"
+}
+
+# If the output path is an existing directory, append the default file name for the platform
+if (Test-Path $OutputPath -PathType Container) {
+ $OutputPath = Join-Path $OutputPath $fileName
+}
+
+# Delete the existing file if it exists
+if (Test-Path $OutputPath) {
+ Remove-Item $OutputPath
+}
+
+# Download the archive
+Write-Host "Downloading FFmpeg for $Platform..."
+$http = New-Object System.Net.WebClient
+try {
+ $http.DownloadFile("https://github.com/Tyrrrz/FFmpegBin/releases/download/7.0/ffmpeg-$Platform.zip", "$OutputPath.zip")
+} finally {
+ $http.Dispose()
+}
+
+try {
+ # Extract FFmpeg
+ Add-Type -Assembly System.IO.Compression.FileSystem
+ $zip = [IO.Compression.ZipFile]::OpenRead("$OutputPath.zip")
+ try {
+ [IO.Compression.ZipFileExtensions]::ExtractToFile($zip.GetEntry($fileName), $OutputPath)
+ } finally {
+ $zip.Dispose()
+ }
+
+ Write-Host "Done downloading FFmpeg."
+} finally {
+ # Clean up
+ Remove-Item "$OutputPath.zip" -Force
+}
\ No newline at end of file
diff --git a/YoutubeDownloader/DownloadFFmpeg.ps1 b/YoutubeDownloader/DownloadFFmpeg.ps1
deleted file mode 100644
index f72e00b..0000000
--- a/YoutubeDownloader/DownloadFFmpeg.ps1
+++ /dev/null
@@ -1,61 +0,0 @@
-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 for $platform..."
-$http = New-Object System.Net.WebClient
-try {
- $http.DownloadFile("https://github.com/Tyrrrz/FFmpegBin/releases/download/7.0/ffmpeg-$platform.zip", "$outputPath.zip")
-} finally {
- $http.Dispose()
-}
-
-try {
- # Extract FFmpeg
- Add-Type -Assembly System.IO.Compression.FileSystem
- $zip = [IO.Compression.ZipFile]::OpenRead("$outputPath.zip")
- try {
- $fileName = If ($platform.Contains("windows-")) { "ffmpeg.exe" } Else { "ffmpeg" }
- [IO.Compression.ZipFileExtensions]::ExtractToFile($zip.GetEntry($fileName), $outputPath)
- } finally {
- $zip.Dispose()
- }
-
- Write-Host "Done downloading FFmpeg."
-} finally {
- # Clean up
- Remove-Item "$outputPath.zip" -Force
-}
\ No newline at end of file
diff --git a/YoutubeDownloader/YoutubeDownloader.csproj b/YoutubeDownloader/YoutubeDownloader.csproj
index 780c353..d59de84 100644
--- a/YoutubeDownloader/YoutubeDownloader.csproj
+++ b/YoutubeDownloader/YoutubeDownloader.csproj
@@ -12,6 +12,10 @@
true
+
+ true
+
+
@@ -60,4 +64,9 @@
+
+
+
+
+
\ No newline at end of file