Skip to content

Instantly share code, notes, and snippets.

@threez
Created August 18, 2013 19:07
Show Gist options
  • Save threez/6263374 to your computer and use it in GitHub Desktop.
Save threez/6263374 to your computer and use it in GitHub Desktop.
A simple script to convert m4a files into mp3 out of the itunes library. Best used in conjunction with the 'request itunes titles' automator task.
#!/usr/bin/env ruby
require 'open3'
require 'fileutils'
USER = 'XXX
CMD = '/Applications/CocoaDialog.app/Contents/MacOS/CocoaDialog'
MUSIC_LOCATION = "/Users/#{USER}/Music/iTunes/iTunes Music/"
TARGET_DIR = "/Users/#{USER}/Musik MP3"
# start cocoa dialog for progress bar
stdin, stdout, stderr = Open3.popen3(CMD + ' progressbar --title "Converting MP3"')
# ingore output
stdout.close
stderr.close
# get list of all songs
songs = STDIN.read.split(/\n/).map { |line| line.chomp }
# process each song in the song list
songs.each_with_index do |song, i|
# remove absolute path to the song
short_song = song.gsub(MUSIC_LOCATION, '')
STDOUT.puts "<< #{short_song}"
# display status
percent = i * 100.0 / songs.size
stdin.puts "#{percent} #{short_song}\n"
# build up target song location
target_song = File.join(TARGET_DIR, short_song).gsub(/.m4a/, '.mp3')
# create directories of the new song location
FileUtils.mkdir_p(File.dirname(target_song))
if File.extname(song).downcase == '.mp3'
STDOUT.puts "-- Copy #{short_song} to #{target_song}"
# just copy the mp3 to the new location
FileUtils.cp song, target_song
else
STDOUT.puts "-- Converting #{short_song} to #{target_song}"
# encode the song into mp3
system "/opt/local/bin/faad -q -o - '#{song}' | /opt/local/bin/lame --quiet -V2 - '#{target_song}'"
end
end
stdin.puts "100 Fertig\n"
sleep 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment