Skip to content

Instantly share code, notes, and snippets.

@ShadowPower
Last active February 22, 2025 16:37
Show Gist options
  • Select an option

  • Save ShadowPower/6be0488429b87b91f85235779080b71b to your computer and use it in GitHub Desktop.

Select an option

Save ShadowPower/6be0488429b87b91f85235779080b71b to your computer and use it in GitHub Desktop.
convert pixiv ugoira to webp
param (
[Parameter(Mandatory = $true)]
[string]$PixivZipPath
)
# 检查 ffmpeg 是否可用
function Check-FFmpeg {
$ffmpegPath = Get-Command ffmpeg -ErrorAction SilentlyContinue
if (-not $ffmpegPath) {
Write-Error "ffmpeg is not installed or not in PATH."
exit 1
}
}
# 生成独立的临时目录用于解压缩
function Create-TempFolder {
$baseTempPath = [System.IO.Path]::GetTempPath() # 跨平台的临时路径
$tempFolder = [System.IO.Path]::Combine($baseTempPath, [System.Guid]::NewGuid().ToString())
New-Item -ItemType Directory -Path $tempFolder | Out-Null
return $tempFolder
}
# 解压缩Pixiv动图文件
function Unzip-File($zipFilePath, $destinationFolder) {
Add-Type -AssemblyName System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipFilePath, $destinationFolder)
}
# 解析Pixiv文件名获取帧间隔时间
function Get-FrameInterval {
param (
[string]$filename
)
if ($filename -match '@(\d+)ms') {
return [int]$matches[1]
} else {
Write-Error "Cannot find frame interval in filename: $filename. Assuming 100ms as default."
return 100
}
}
# 将解压出来的图片转成webp动图
function Convert-ToWebp {
param (
[string]$inputFolder,
[string]$outputWebpPath,
[int]$frameInterval
)
# 获取所有 JPG 和 PNG 图片
$imageFiles = Get-ChildItem -Path $inputFolder | Where-Object { $_.Extension -in '.jpg', '.png' } | Sort-Object Name
if ($imageFiles.Count -eq 0) {
Write-Error "No image files found in $inputFolder"
exit 1
}
# 输出调试信息,查看文件名
Write-Host "Found images:"
foreach ($file in $imageFiles) {
Write-Host $file.Name
}
# 计算帧率
$frameRate = [math]::Round(1000 / $frameInterval, 2)
Write-Host "Using framerate: $frameRate"
# 使用 FFmpeg 合成 WebP 动图
$ffmpegArgs = @(
"-y",
"-framerate", $frameRate,
"-i", "$inputFolder/%06d.jpg",
"-c:v", "libwebp",
"-lossless", "0" # 启用有损压缩
"-qscale", "75" # 设置质量 (0-100, 越高越好, 通常 75-95 是不错的范围)
"-preset", "text" # 预设 (default, photo, drawing, icon, text)
"-method", "6" # 编码方法 (0-6, 越高压缩越慢,质量可能更好, 4 或 6 通常较好)
"-pix_fmt", "yuva420p", # 使用 YUV 格式
"-loop", "0",
$outputWebpPath
)
Write-Host "Running ffmpeg with arguments: $($ffmpegArgs -join ' ')"
# 执行 ffmpeg 命令
try {
$result = & ffmpeg @ffmpegArgs
Write-Host "FFmpeg result: $result"
} catch {
Write-Error "FFmpeg command failed with error: $_"
}
}
# 主程序
Check-FFmpeg
# 获取文件名用于解析帧间隔
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($PixivZipPath)
$frameInterval = Get-FrameInterval -filename $baseName
# 创建临时文件夹
$tempFolder = Create-TempFolder
try {
# 解压缩Pixiv Zip文件
Unzip-File -zipFilePath $PixivZipPath -destinationFolder $tempFolder
# 输出路径,设置为当前目录
$outputWebpPath = [System.IO.Path]::ChangeExtension([System.IO.Path]::GetFileName($PixivZipPath), '.webp')
$outputWebpPath = Join-Path -Path (Get-Location) -ChildPath $outputWebpPath
# 转换为 WebP
Convert-ToWebp -inputFolder $tempFolder -outputWebpPath $outputWebpPath -frameInterval $frameInterval
Write-Host "WebP animation has been created at: $outputWebpPath"
}
finally {
# 删除临时文件夹
Remove-Item -Recurse -Force $tempFolder
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment