Created
May 29, 2023 08:55
-
-
Save dinkopehar/ee24fc088aaf928a23a5dfd856d9068a to your computer and use it in GitHub Desktop.
Script to add ID3 tags to .mp3 files for my car
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 '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