Last active
August 31, 2025 18:16
-
-
Save soffes/f6fc010baabd6537ae05b1d2d7738e9a to your computer and use it in GitHub Desktop.
Convert audio files on an external hard drive into MP3 format on my desktop
This file contains hidden or 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" | |
| require "open3" | |
| require "pathname" | |
| require "progressbar" | |
| input_directory = "/Volumes/Artemis/Music" | |
| output_directory = "/Users/soffes/Desktop/Music" | |
| extensions = %w[wav aiff mp3 m4a] | |
| target_extension = "mp3" | |
| glob = File.join(input_directory, "**", "*.{#{extensions.join(",")}}") | |
| # This can be pretty slow | |
| puts "Gathering file list…" | |
| files = Dir.glob(glob, File::FNM_CASEFOLD) | |
| if files.empty? | |
| puts "No files found." | |
| exit 1 | |
| end | |
| # Make output directory | |
| FileUtils.mkdir_p(output_directory) | |
| progressbar = ProgressBar.create( | |
| title: "Converting", | |
| format: "%t: |%B| %E %c/%C", | |
| total: files.count | |
| ) | |
| files.each do |path| | |
| # Change the directory to be relative to our output directory instead | |
| output = File.join(output_directory, path.delete_prefix(input_directory)) | |
| # Replace the extension with our target extension | |
| album_dir = File.dirname(output) | |
| output = File.join(album_dir, "#{File.basename(output, ".*")}.#{target_extension}") | |
| # Skip if we have already converted it | |
| next if File.exist?(output) | |
| # Make the directory for the album | |
| FileUtils.mkdir_p(album_dir) | |
| # Convert | |
| command = %(ffmpeg -i "#{path}" -b:a 320k "#{output}") | |
| Open3.capture3(command) # using this instead of `` suppresses the output | |
| progressbar.increment | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment