From fc8aefab8382d444cdf9cb2bd6617c2cd55feaf9 Mon Sep 17 00:00:00 2001 From: Miwory Date: Sat, 5 Oct 2024 21:21:12 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=B2=D0=B0=D1=80=D0=B8=D0=B0=D0=BD=D1=82=20=D1=8D=D0=BA=D1=81?= =?UTF-8?q?=D0=BF=D0=BE=D1=80=D1=82=D0=B0=20=D1=8D=D0=BC=D0=BE=D0=B4=D0=B7?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 14 ++++++++++++++ script.ps1 | 45 ++++++++++++++++++++++++++++----------------- 2 files changed, 42 insertions(+), 17 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..126322e --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +# How to use script.ps1 + +This script is designed to convert any images and/or gifs to webm extension so that they can be used to create animated sticker/emoji packs in Telegram. + +## Setup + +1. Install ffmpeg and ffprobe on your system if they are not already installed. +2. Clone this repository and navigate to the directory containing script.ps1. +3. Create a directory named "input" and populate it with the images and/or gifs you want to convert. +4. Run the following command in your terminal: `& script.ps1` + +## Output + +The script will output the converted webm files to a directory named "output". The files will be named the same as the input files but with a .webm extension instead of their original extension. diff --git a/script.ps1 b/script.ps1 index 2b892bf..de11582 100644 --- a/script.ps1 +++ b/script.ps1 @@ -4,6 +4,28 @@ $ffprobePath = (Get-Command ffprobe -ErrorAction SilentlyContinue).Path $inputDir = "./input" $outputDir = "./output" +# Check if ffmpeg and ffprobe executables exist +if (-not $ffmpegPath -or -not $ffprobePath) { + Write-Host "ffmpeg or ffprobe is not installed on this system. Exiting..." + exit 1 +} + +# Ask user if he wants to do stickers or emoji +$choice = Read-Host "Do you want to create stickers or emoji? (s/e)" +if ($choice -eq "s") { + $stickerWidth = 512 +} elseif ($choice -eq "e") { + $stickerWidth = 100 +} else { + Write-Host "Invalid choice. Exiting..." + exit 1 +} + +# Check if the output directory exists, if not, create it +if (-not (Test-Path $outputDir)) { + New-Item -Path $outputDir -ItemType Directory -Force | Out-Null +} + # Functions # Function to get the bitrate of a video file using ffprobe function Get-Bitrate { @@ -70,7 +92,7 @@ function Compress-File { Write-Host Trying bitrate: ${bitrate}k # Compress with the current bitrate - & $ffmpegPath -loglevel quiet -i $inputFilePath -t 3 -r 25 -filter:v "setpts=PTS/$speedFactor,scale=-1:512" -c:v libvpx-vp9 -b:v ${bitrate}k $outputFilePathTemp -y + & $ffmpegPath -loglevel quiet -i $inputFilePath -t 3 -r 25 -filter:v "setpts=PTS/$speedFactor,scale=-1:$stickerWidth" -c:v libvpx-vp9 -b:v ${bitrate}k $outputFilePathTemp -y # Check the size of the compressed file $fileSize = (Get-Item $outputFilePathTemp).Length @@ -100,17 +122,6 @@ function Compress-File { } } -# Check if ffmpeg and ffprobe executables exist -if (-not $ffmpegPath -or -not $ffprobePath) { - Write-Host "ffmpeg or ffprobe is not installed on this system. Exiting..." - exit 1 -} - -# Check if the output directory exists, if not, create it -if (-not (Test-Path $outputDir)) { - New-Item -Path $outputDir -ItemType Directory -Force | Out-Null -} - # Process each file in the input directory foreach ($file in Get-ChildItem -Path $inputDir -File) { $outputFile = "$outputDir/$($file.BaseName).webm" @@ -124,7 +135,7 @@ foreach ($file in Get-ChildItem -Path $inputDir -File) { # Convert the file to WebM format if ($file.Extension -eq ".png") { - & $ffmpegPath -loglevel quiet -i $file.FullName -r 25 -vf "scale=-1:512" -c:v libvpx-vp9 -pix_fmt rgba $outputFile -y + & $ffmpegPath -loglevel quiet -i $file.FullName -r 25 -vf "scale=-1:$stickerWidth" -c:v libvpx-vp9 -pix_fmt rgba $outputFile -y } if ($file.Extension -eq ".gif") { @@ -139,7 +150,7 @@ foreach ($file in Get-ChildItem -Path $inputDir -File) { } # Convert the file to WebM format - & $ffmpegPath -loglevel quiet -i $file.FullName -t 3 -r 25 -filter:v "setpts=PTS/$speedFactor,scale=-1:512" -c:v libvpx-vp9 $outputFile -y + & $ffmpegPath -loglevel quiet -i $file.FullName -t 3 -r 25 -filter:v "setpts=PTS/$speedFactor,scale=-1:$stickerWidth" -c:v libvpx-vp9 $outputFile -y } else { Write-Host "Invalid duration for GIF $($file.FullName). Skipping..." continue @@ -148,14 +159,14 @@ foreach ($file in Get-ChildItem -Path $inputDir -File) { # Check if the file size exceeds 256 KB (262144 bytes) $fileSize = (Get-Item $outputFile).Length - if ($fileSize -gt 262144) { + if ($fileSize -gt 255 * 1024) { Write-Host "File $($outputFile) exceeds 256 KB, compressing..." # Get the initial bitrate of the WebM file $initialBitrate = Get-Bitrate -filePath $outputFile - # Compress the file until it is below 256 KB - Compress-File -inputFilePath $file.FullName -outputFilePath $outputFile -initialBitrate $initialBitrate -maxSizeKB 256 + # Compress the file until it is below 255 KB + Compress-File -inputFilePath $file.FullName -outputFilePath $outputFile -initialBitrate $initialBitrate -maxSizeKB 255 } } }