Created
March 15, 2024 13:48
-
-
Save huseyinbiyik/27794e7570f86f5e2e5e7b2d19f7c844 to your computer and use it in GitHub Desktop.
Image converter with using threads
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 'mini_magick' | |
require 'thread' | |
# Maximum number of worker threads | |
max_threads = 10 | |
# Queue for AVIF files | |
queue = Queue.new | |
Dir["avif_thumbnails/*.avif"].each { |avif_file| queue << avif_file } | |
# Worker threads | |
workers = max_threads.times.map do | |
Thread.new do | |
while (avif_file = queue.pop(true) rescue nil) | |
jpeg_file = "jpeg_thumbnails/#{File.basename(avif_file, '.avif')}.jpeg" | |
image = MiniMagick::Image.open(avif_file) | |
image.format "jpeg" | |
image.write jpeg_file | |
puts "Converted #{avif_file} to #{jpeg_file}" | |
end | |
end | |
end | |
# Wait for all worker threads to finish | |
workers.each(&:join) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment