Skip to content

Instantly share code, notes, and snippets.

@aphyr
Created July 4, 2015 23:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aphyr/170648f9ed4c798ede10 to your computer and use it in GitHub Desktop.
Save aphyr/170648f9ed4c798ede10 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'sequel'
require 'fileutils'
require 'uri'
require 'pp'
def home
ENV['HOME']
end
def phone
"#{home}/phone/music"
end
# Returns URIs for a playlist by name
def playlist_uris(db, name)
db[:CorePlaylists].
filter({Name: name}).
join(db[:CorePlaylistEntries], :PlaylistID => :PlaylistID).
join(db[:CoreTracks], :TrackID => :TrackID).
map(:Uri).
map { |u| URI.unescape(URI.parse(u).path) }
end
# Copies URIs to the phone folder, returning a list of destination file paths.
def copy(paths)
paths.map do |src|
dest = src.sub(/^#{home}\/Music/, phone)
unless File.file?(dest) and File.stat(src).size == File.stat(dest).size
FileUtils.mkdir_p File.dirname(dest)
puts "Copy #{src} -> #{dest}"
FileUtils.copy src, dest
end
dest
end
end
# Writes a playlist as name.m3u in the given directory, replacing extensions
# with .mp3.
def write_m3u(name, path, files)
File.open("#{path}/#{name}.m3u", "w") do |playlist|
playlist.puts "#EXTM3U"
files.each do |file|
playlist.puts file.sub(/^#{path}\/?/, '').sub(/\.\w+$/, '.mp3')
end
end
end
['Gym'].each do |playlist|
local = Sequel.connect "sqlite://#{home}/.config/banshee-1/banshee.db"
files = copy playlist_uris(local, playlist)
write_m3u playlist, phone, files
end
`recode #{phone}`
#!/usr/bin/env ruby
# Takes a directory and turns music in it to mp3s; deleting originals.
require 'find'
require 'fileutils'
# Find files
files = []
Find.find(ARGV.first) do |file|
if file =~ /\.(aac|mp4|m4a|wma|m4p|ogg|flac|wav)$/
files << file
end
end
# Previous runs might have got somewhere
remaining = files.reject do |f|
File.exists?(f.sub(/\.[^\.]+$/, '.mp3'))
end
exit if files.empty?
# Convert
unless remaining.empty?
puts ['parallel', "soundconverter", "-b", "-m", "audio/mpeg", "-s", ".mp3",
':::', *remaining].join(" ")
system('parallel', "soundconverter", "-b", "-m", "audio/mpeg", "-s", ".mp3",
':::', *remaining)
if $? != 0
puts "Encountered errors; not deleting anything"
exit!
end
end
# Delete
puts "Deleting files"
files.each do |file|
File.delete file
end
puts "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment