aboutsummaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2025-03-24 14:03:38 +0100
committerEugen Wissner <belka@caraus.de>2025-03-24 14:03:38 +0100
commita05bd27cafd90006f4ebf3b6b1840e16cb0ad986 (patch)
tree12ff7d348efba019fc4d77dc0b1d1b8dc3dd4204 /bin
parent69bf515582f867b8521cf97ddd310e8dfdf1238b (diff)
downloadkazbek-a05bd27cafd90006f4ebf3b6b1840e16cb0ad986.tar.gz
7digital: Read the album from the metadata
Diffstat (limited to 'bin')
-rwxr-xr-xbin/7digital.rb54
1 files changed, 30 insertions, 24 deletions
diff --git a/bin/7digital.rb b/bin/7digital.rb
index 518e4df..fc90823 100755
--- a/bin/7digital.rb
+++ b/bin/7digital.rb
@@ -7,6 +7,8 @@
require 'pathname'
require 'open3'
+require 'optparse'
+require 'zip'
# Renames music files in a directory according to the file's tags.
# Expects two arguments:
@@ -14,16 +16,13 @@ require 'open3'
# - Music directory to extract songs into.
class Song
- attr_reader :title, :track, :extension
+ attr_reader :track, :extension
+ attr_accessor :title, :album
def initialize(extension)
@extension = extension
end
- def title=(title)
- @title = title.strip
- end
-
def track=(track)
@track = track.strip.split('/').first.rjust(2, '0')
end
@@ -38,22 +37,19 @@ def find_unnamed_directory(parent_path)
end
def extract_and_rename_archive(album_archive, music_directory)
+ music_directory.mkpath
artist_name, album_name = album_archive.basename('.zip').to_s.split(' - ')
- system 'unzip', '-d', music_directory.to_path, album_archive.to_path, exception: true
+ Zip::File.open album_archive.to_path do |zip_file|
+ zip_file.each do |entry|
+ puts "Inflating #{entry.name}"
+ extract_target = music_directory + File.basename(entry.name)
- artist_path = music_directory + artist_name
- album_path = artist_path + album_name
- source_artist_path = find_unnamed_directory music_directory
-
- if artist_path.exist?
- find_unnamed_directory(source_artist_path).rename album_path
- source_artist_path.unlink
- else
- source_artist_path.rename artist_path
- find_unnamed_directory(artist_path).rename album_path
+ entry.extract extract_target
+ end
end
- album_path
+
+ music_directory
end
def probe_song(song_path)
@@ -61,32 +57,42 @@ def probe_song(song_path)
Open3.popen3 'ffprobe', song_path.to_s do |_stdin, _stdout, stderr, _wait_pid|
while (line = stderr.gets)
- key, value = line.split ':'
+ key, value = line.split ':', 2
next if value.nil?
case key.strip.downcase
when 'title'
- song.title = value if song.title.nil?
+ song.title = value.strip if song.title.nil?
when 'track'
song.track = value if song.track.nil?
+ when 'album'
+ song.album = value.strip if song.album.nil?
end
end
end
song
end
+unless ARGV.length == 2
+ $stderr.puts 'Usage: 7digital.rb MUSIC_ARCHIVE.zip DIRECTORY'
+ exit 1
+end
+
album_archive = Pathname.new ARGV[0]
music_directory = Pathname.new ARGV[1]
metadata = {}
-album_path = extract_and_rename_archive album_archive, music_directory
+extract_and_rename_archive album_archive, music_directory
-Dir.each_child album_path do |filename|
- song_path = album_path + filename
+Dir.each_child music_directory do |filename|
+ song_path = music_directory + filename
- metadata[song_path] = probe_song(song_path).to_s
+ metadata[song_path] = probe_song(song_path)
end
metadata.each_pair do |from, to|
- File.rename(from, album_path + to)
+ album_path = music_directory + to.album
+
+ album_path.mkpath
+ File.rename(from, album_path + to.to_s)
end