Sunday, March 29, 2020

song sequencer for flash drive

sometimes i don't want the hassle of having to connect to bluetooth on my car stereo. i just wanna stick in a flashdrive which automatically plays the songs. car stereos usually play songs in ALPHABETICAL order. i wrote a small ruby script to make it easy to play the songs according to my preferred sequence. just download ruby (it's free) and copy paste the following script to a file named sequence-songs.rb:

#-----------------------------------

require 'fileutils'

puts ARGV[0]

i = 1

File.foreach('playlist') do |line|
  # .strip removes whitespace and newline characters (\n)
  song = line.strip 
  if i <= 9
    prefix = "000#{i}" 
  elsif (i >= 10) && (i <= 99)
    prefix = "00#{i}"
  elsif (i >= 100) && (i <= 999)
    prefix = "0#{i}"
  elsif (i >= 1000) && (i <= 9999)
    prefix = "#{i}"
  end
  
  new_name = "#{prefix}_#{song}" 
  source = "#{ARGV[0]}/#{song}"
  if File.exist?(source)
    puts "copying #{source} #{new_name} ..."
    FileUtils.cp("#{source}", new_name)
  else
    puts "Warning: #{source} does not exist. if it is in another directory, simply re-run this script with the correct directory as arument"
  end
  
  i += 1
end

#------------------------------------------------------

example, if you are using a windows system, open a dos or CMD box (enter "cmd" in search) and cd to the directory containing your songs. then, assuming your flash drive is d:\, run:

dir /b > d:\playlist

if you want to add songs located in other directories, go to each of those directories and APPEND the songs. note > is now >> (append instead of overwrite):

dir /b >> d:\playlist

then just EDIT d:\playlist (e.g. using notepad or vi) to arrange the songs and remove the songs you want excluded. 

then at the cmd prompt go to d:\ and RUN sequence-songs.rb <songs_directory>. example, if the script is in c:\my\bin and your songs are in c:\my\music\70s, run:

ruby c:\my\bin\sequence-songs.rb c:/my/music/70s

EXAMPLE, the following songs in your playlist file

Steely Dan - Black Friday.mp3
Steely Dan - Deacon Blues.mp3
Eagles - Best Of My Love.mp3
Eagles - I Can't Tell You Why.mp3

will be named

0001_Steely Dan - Black Friday.mp3
0002_Steely Dan - Deacon Blues.mp3
0003_Eagles - Best Of My Love.mp3
0004_Eagles - I Can't Tell You Why.mp3

if your playlist also contains songs located in another directory, simply re-run the script with the other directory location. files not located in the specified directory will simply be SKIPPED. example, if my playlist also contains let's say stray cat songs located in c:\my\music\80s:

Steely Dan - Black Friday.mp3
Steely Dan - Deacon Blues.mp3
Eagles - Best Of My Love.mp3
Eagles - I Can't Tell You Why.mp3
stray cats - stray cat strut.mp3
Stray Cats -- Sexy And Seventeen.mp3

also run:

ruby c:\my\bin\sequence-songs.rb c:/my/music/80s

and here's a script that would let you know if there are DUPLICATE files (when i was arranging my songs into directories, i might have did a copy instead of a move on some files, which causes the duplication). it only works on 1 level of categories. you can scan all directories or just 1 directory.
 
directories = Dir.glob('*').select { |f| File.directory?(f) }
if  ARGV.empty?
  # scan all directories
  base_dirs = directories.dup
else
  # only check the specified directory
  base_dirs = [ARGV[0]]
end

base_dirs.each do |base_dir|
  Dir.chdir(base_dir)
  files = Dir.children('.')
  Dir.chdir("..")

  directories.each do |directory|
    if directory.downcase != base_dir.downcase
      #puts "looking for duplicates in #{directory} and #{base_dir}..."
      files.each do |file|
        if File.exist?("#{directory}/#{file}")
          puts "#{file} exists in #{directory} and #{base_dir}"
        end
      end
    end
  end      
  directories.shift
end


(for more of my knowledge bombs, click the "ian's knowledge bombs" banner at the top of this article and choose any article in the table of contents that piques your interest)

No comments:

Post a Comment