Skip to content

Instantly share code, notes, and snippets.

@huseyinbiyik
Created March 15, 2024 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huseyinbiyik/27794e7570f86f5e2e5e7b2d19f7c844 to your computer and use it in GitHub Desktop.
Save huseyinbiyik/27794e7570f86f5e2e5e7b2d19f7c844 to your computer and use it in GitHub Desktop.
Image converter with using threads
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