-
-
Save MaherSaif/c2baadd8f4cc9e3983d727cdfd0ac4df to your computer and use it in GitHub Desktop.
Ruby Oneliners to convert images to webp and generate thumbnails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'fileutils' | |
# Loop through all .jpg and .png files in the current directory | |
Dir.glob("{*.jpg,*.png}").each do |img| | |
# Construct the output filename with .webp extension | |
output_filename = "#{File.basename(img, File.extname(img))}.webp" | |
# Execute ffmpeg command to convert the image | |
system("ffmpeg -i '#{img}' '#{output_filename}'") | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ruby -e "Dir.glob('{*.jpg,*.png}').each { |img| system('ffmpeg -i \'' + img + '\' -q:v 80 \'' + File.basename(img, File.extname(img)) + '.webp\'') }" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'fileutils' | |
# Loop through all .jpg and .png files in the current directory | |
Dir.glob("{*.jpg,*.png,*.webp}").each do |img| | |
# Use ffprobe to get the original image width | |
original_width = %x(ffprobe -v error -select_streams v:0 -show_entries stream=width -of default=noprint_wrappers=1:nokey=1 "#{img}").to_i | |
# Calculate new widths for medium and small sizes | |
medium_width = original_width / 2 | |
small_width = original_width / 3 | |
# Base filename without extension | |
basename = File.basename(img, File.extname(img)) | |
# Convert and resize to medium | |
system("ffmpeg -i '#{img}' -vf scale=#{medium_width}:-1 '#{basename}.medium.webp'") | |
# Convert and resize to small | |
system("ffmpeg -i '#{img}' -vf scale=#{small_width}:-1 '#{basename}.small.webp'") | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ruby -e 'Dir.glob("{*.jpg,*.png,*.webp}").each do |img| width = %x(ffprobe -v error -select_streams v:0 -show_entries stream=width -of default=noprint_wrappers=1:nokey=1 "#{img}").to_i; medium_width = width / 2; small_width = width / 3; basename = File.basename(img, File.extname(img)); system("ffmpeg -i \"#{img}\" -vf scale=#{medium_width}:-1 \"#{basename}.medium.webp\""); system("ffmpeg -i \"#{img}\" -vf scale=#{small_width}:-1 \"#{basename}.small.webp\""); end' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment