-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen-thumbnails.ps1
93 lines (75 loc) · 4.26 KB
/
gen-thumbnails.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<#
.SYNOPSIS
Generates thumbnails for images in a specified folder.
.DESCRIPTION
This function generates thumbnails for images in the specified folder. By default, it does not delete existing thumbnails, but you can enable the deletion of existing thumbnails by setting the $deleteExisting parameter to $true.
.PARAMETER folderPath
The path of the folder containing the images.
.PARAMETER deleteExisting
Specifies whether to delete existing thumbnails before generating new ones. Default value is $false.
.EXAMPLE
GenerateThumbnails -folderPath "C:\Images" -deleteExisting $true
Generates thumbnails for images in the "C:\Images" folder and deletes any existing thumbnails.
#>
param(
[string]$folderPath = ".\assets\img\posts",
[bool]$deleteExisting = $false
)
function GenerateThumbnails($folderPath, [bool]$deleteExisting = $false) {
# Get all the subfolders
$subfolders = Get-ChildItem -Path $folderPath -Directory
# Loop through each subfolder
foreach ($subfolder in $subfolders) {
# Print the name of the subfolder
Write-Output $subfolder.Name
# Create a directory called Thumbnails in the subfolder
$thumbnailsPath = "$($subfolder.FullName)\thumbnails"
$null = New-Item -Path $thumbnailsPath -ItemType Directory -Force
# Create a directory called Thumbnails in the subfolder
$tinyfilesPath = "$($subfolder.FullName)\tinyfiles"
$null = New-Item -Path $tinyfilesPath -ItemType Directory -Force
# If the user chose to delete existing thumbnails, delete them
if ($deleteExisting) {
$null = Get-ChildItem -Path $thumbnailsPath -File | Remove-Item -Force
$null = Get-ChildItem -Path $tinyfilesPath -File | Remove-Item -Force
}
# Get all the image files in the subfolder
$imageFiles = Get-ChildItem -Path $subfolder.FullName -Filter "*.jpeg" -File
$imageFiles += Get-ChildItem -Path $subfolder.FullName -Filter "*.jpg" -File
# Loop through each image file in the subfolder
foreach ($imageFile in $imageFiles) {
# Check if a thumbnail already exists
$thumbnailPath = Join-Path -Path $thumbnailsPath -ChildPath $imageFile.Name
if (-not (Test-Path -Path $thumbnailPath)) {
Write-Output "Source:" $imageFile.FullName
Write-Output "Destination:" $thumbnailPath
# Generate a new thumbnail using Magick
Write-Output "Convert command: " "convert $($imageFile.FullName) -resize 50% $thumbnailPath"
Start-Process -FilePath "magick" -ArgumentList "convert `"$($imageFile.FullName)`" -resize 50% `"$thumbnailPath`"" -NoNewWindow -Wait -WorkingDirectory $subfolder.FullName
}
# Check if a tinyfile already exists
$tinyfilePath = Join-Path -Path $tinyfilesPath -ChildPath $imageFile.Name
if (-not (Test-Path -Path $tinyfilePath)) {
Write-Output "Source:" $imageFile.FullName
Write-Output "Destination:" $tinyfilePath
# Generate a new thumbnail using Magick
Write-Output "Convert command: " "convert $($imageFile.FullName) -resize 10% $tinyfilePath"
Start-Process -FilePath "magick" -ArgumentList "convert `"$($imageFile.FullName)`" -resize 10% `"$tinyfilePath`"" -NoNewWindow -Wait -WorkingDirectory $subfolder.FullName
}
}
# generate poster file for mp4 files.
$mp4Files = Get-ChildItem -Path $subfolder.FullName -Filter "*.mp4" -File
foreach ($mp4File in $mp4Files) {
$thumbnailPath = Join-Path -Path $thumbnailsPath -ChildPath ($mp4File.BaseName + ".jpeg")
if (-not (Test-Path -Path $thumbnailPath)) {
Write-Output "Source:" $mp4File.FullName
Write-Output "Destination:" $thumbnailPath
# Generate a new thumbnail using FFMPEG.
Write-Output "FFMPEG command: " "ffmpeg -i `"$($mp4File.FullName)`" - update 1 -frames:v 1 -q:v 10 `"$thumbnailPath`" -loglevel quiet"
Start-Process -FilePath "ffmpeg" -ArgumentList "-i `"$($mp4File.FullName)`" -update 1 -frames:v 1 -q:v 10 `"$thumbnailPath`" -loglevel quiet" -NoNewWindow -Wait -WorkingDirectory $subfolder.FullName
}
}
}
}
# Call the function with the specified folder path$folderPath = ".\assets\img\posts"
GenerateThumbnails $folderPath $deleteExisting