Skip to content

Instantly share code, notes, and snippets.

@dinkopehar
Created May 29, 2023 08:55
Show Gist options
  • Save dinkopehar/ee24fc088aaf928a23a5dfd856d9068a to your computer and use it in GitHub Desktop.
Save dinkopehar/ee24fc088aaf928a23a5dfd856d9068a to your computer and use it in GitHub Desktop.
Script to add ID3 tags to .mp3 files for my car
require 'id3tag'
require 'pathname'
require 'fileutils'
MUSIC_FOLDER = Pathname.new('./music')
# Get all MP3 files.
songs = Dir.entries(MUSIC_FOLDER).select { |song| song.end_with? '.mp3' }
# Read each file ID3 tag.
songs.each do |song|
File.open MUSIC_FOLDER + song do |mp3|
# Split song into two parts: artist and title.
artist, title = song.sub('.mp3', '').split ' - '
# Normalize both parts.
artist.strip!
title.strip!
# Read each MP3 file meta info.
ID3Tag.read(mp3) do |tag|
puts "Artist: #{tag.artist} | Title: #{tag.title}"
end
system("id3tag --song='#{title}' --artist='#{artist}' '#{MUSIC_FOLDER + song}'")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment