Skip to content

Commit

Permalink
2023-11-16 22:39:06
Browse files Browse the repository at this point in the history
  • Loading branch information
uidHUB committed Nov 16, 2023
1 parent 97514c0 commit 92b5a97
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 67 deletions.
1 change: 0 additions & 1 deletion PkgStore.FFmpeg.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
Description = ''
PowerShellVersion = '7.1'
RequiredModules = @('PkgStore.Kernel')
FunctionsToExport = @('Compress-Video')
PrivateData = @{
PSData = @{
Tags = @('pwsh', 'ffmpeg')
Expand Down
83 changes: 17 additions & 66 deletions PkgStore.FFmpeg.psm1
Original file line number Diff line number Diff line change
@@ -1,72 +1,23 @@
<#PSScriptInfo
.VERSION 0.1.0
.GUID 82549e1a-69f4-407b-baff-4f97d75b9852
.AUTHOR Kitsune Solar
.AUTHOREMAIL [email protected]
.COMPANYNAME iHub.TO
.COPYRIGHT 2023 Kitsune Solar. All rights reserved.
.LICENSEURI https://github.com/pkgstore/pwsh-ffmpeg/blob/main/LICENSE
.PROJECTURI https://github.com/pkgstore/pwsh-ffmpeg
#>

$App = @('ffmpeg.exe')
$AppExe = @{LiteralPath = "${PSScriptRoot}"; Filter = "$($App[0])"; Recurse = $true; File = $true}
$AppExe = ((Get-ChildItem @AppExe) | Select-Object -First 1)
$NL = "$([Environment]::NewLine)"

function Compress-Video() {
<#
.SYNOPSIS
.DESCRIPTION
#>

Param(
[Parameter(Mandatory)][Alias('I')][string[]]$P_In,
[Alias('CV')][string]$P_vCodec = 'libx265',
[Alias('CA')][string]$P_aCodec = 'copy',
[Alias('R')][int]$P_Framerate,
[Alias('C')][int]$P_CRF,
[Alias('PS')][string]$P_Preset,
[Alias('EXT')][string]$P_Extension = 'mp4'
)

# Checking FFmpeg location.
Test-FFmpeg

(Get-Item $P_In) | ForEach-Object {
$I = "$($_.FullName)" # Input data.
$O = "$($_.FullName.TrimEnd($_.Extension)).${P_Extension}" # Output data.

$Param = @('-hide_banner') # Hide FFmpeg banner.
$Param += @('-i', "${I}") # Input data.
$Param += @('-c:v', "${P_vCodec}") # Video codec.
if ($P_CRF) { $Param += @('-crf', "${P_CRF}") } # Constant Rate Factor.
if ($P_Preset) { $Param += @('-preset', "${P_Preset}") } # Video preset.
if ($P_Framerate) { $Param += @('-r', "${P_Framerate}") } # Video frame rate.
$Param += @('-c:a', "${P_aCodec}") # Audio codec.
$Param += @("${O}") # Output data.

& "${AppExe}" $Param
$Aliases = @()
$PrivateFunctions = (Get-ChildItem -Path (Join-Path $PSScriptRoot 'Private') -Include '*.ps1' -File -Recurse)
$PublicFunctions = (Get-ChildItem -Path (Join-Path $PSScriptRoot 'Public') -Include '*.ps1' -File -Recurse)

(@($PrivateFunctions) + @($PublicFunctions)) | ForEach-Object {
try {
Write-Verbose "Loading '$($_.FullName)'."
. $_.FullName
} catch {
Write-Warning $_.Exception.Message
}
}

function Test-FFmpeg {
<#
.SYNOPSIS
.DESCRIPTION
#>

# Getting 'curl.exe' directory.
$Dir = "$($AppExe.DirectoryName)"
@($PublicFunctions) | ForEach-Object {
$Alias = (Get-Alias -Definition $_.BaseName -ErrorAction 'SilentlyContinue')

# Checking the location of files.
$App | ForEach-Object {
if (-not (Test-Data -T 'F' -P "${Dir}\${_}")) {
Write-Msg -T 'W' -A 'Stop' -M ("'${_}' not found!${NL}${NL}" +
"1. Download '${_}' from 'https://www.gyan.dev/ffmpeg/builds/'.${NL}" +
"2. Extract all the contents of the archive into a directory '${PSScriptRoot}'.")
}
if ($Alias) {
$Aliases += $Alias
Export-ModuleMember -Function $_.BaseName -Alias $Alias
} else {
Export-ModuleMember -Function $_.BaseName
}
}
26 changes: 26 additions & 0 deletions Private/Start-FFmpeg.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function Start-FFmpeg {
<#
.SYNOPSIS
.DESCRIPTION
#>

Param(
[Alias('AD')][string[]]$AppData = @('ffmpeg.exe')
)

$AppPath = (Split-Path -Path "${PSScriptRoot}" -Parent)
$App = @{LiteralPath = "${AppPath}"; Filter = "$($AppData[0])"; Recurse = $true; File = $true}
$App = ((Get-ChildItem @App) | Select-Object -First 1)
$NL = [Environment]::NewLine

$AppData | ForEach-Object {
if (-not (Test-Data -T 'F' -P "$($App.DirectoryName)\${_}")) {
Write-Msg -T 'W' -A 'Stop' -M ("'${_}' not found!${NL}${NL}" +
"1. Download '${_}' from 'https://www.gyan.dev/ffmpeg/builds/'.${NL}" +
"2. Extract all the contents of the archive into a directory '${AppPath}'.")
}
}

$App
}
33 changes: 33 additions & 0 deletions Public/Compress-Video.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function Compress-Video() {
<#
.SYNOPSIS
.DESCRIPTION
#>

Param(
[Parameter(Mandatory)][Alias('I')][string[]]$P_In,
[Alias('CV')][string]$P_vCodec = 'libx265',
[Alias('CA')][string]$P_aCodec = 'copy',
[Alias('R')][int]$P_Framerate,
[Alias('C')][int]$P_CRF,
[Alias('PS')][string]$P_Preset,
[Alias('EXT')][string]$P_Extension = 'mp4'
)

(Get-Item $P_In) | ForEach-Object {
$I = "$($_.FullName)" # Input data.
$O = "$($_.FullName.TrimEnd($_.Extension)).${P_Extension}" # Output data.

$Param = @('-hide_banner') # Hide FFmpeg banner.
$Param += @('-i', "${I}") # Input data.
$Param += @('-c:v', "${P_vCodec}") # Video codec.
if ($P_CRF) { $Param += @('-crf', "${P_CRF}") } # Constant Rate Factor.
if ($P_Preset) { $Param += @('-preset', "${P_Preset}") } # Video preset.
if ($P_Framerate) { $Param += @('-r', "${P_Framerate}") } # Video frame rate.
$Param += @('-c:a', "${P_aCodec}") # Audio codec.
$Param += @("${O}") # Output data.

& $(Start-FFmpeg) $Param
}
}

0 comments on commit 92b5a97

Please sign in to comment.