Tech Tidbits - Ruby, Ruby On Rails, Merb, .Net, Javascript, jQuery, Ajax, CSS...and other random bits and pieces.

Showing posts with label mp3. Show all posts
Showing posts with label mp3. Show all posts

Saturday, August 23, 2008

Amazon S3 with Ruby aws-s3 gem

I stated using Amazon S3 to store and backup the music I've written and recorded. I'm using the Ruby aws-s3 gem. Note that I check for some basic audio file types so I don't have to manually type the bucket or mime-type.

aws-s3.rb
---------
#!/usr/bin/env ruby
require 'rubygems'
require 'aws/s3'

# File to upload
local_file = ARGV[0]

# bucket & mime-type
if local_file =~ /mp3$/
bucket = 'doug_mp3'
mime_type = 'audio/mpeg'
elsif local_file =~ /wav$/
bucket = 'doug_wav'
mime_type = 'audio/x-wav'
elsif local_file =~ /aif[c|f]?$/
bucket = 'doug_aiff'
mime_type = 'audio/x-aiff'
else
bucket = ARGV[1]
mime_type = ARGV[2] || "application/octet-stream"
end

if bucket.nil?
puts "need a bucket"
exit
end

puts "file: #{local_file}"
puts "bucket: #{bucket}"
puts "mime_type: #{mime_type}"

AWS::S3::Base.establish_connection!(
:access_key_id => 'REPLACE_ME',
:secret_access_key => 'REPLACE_ME'
)

base_name = File.basename(local_file)

puts "Uploading #{local_file} as '#{base_name}' to '#{bucket}'"

AWS::S3::S3Object.store(
base_name,
File.open(local_file),
bucket,
:content_type => mime_type
)

puts "Uploaded!"

I can call the script a couple ways:

# with defaults based on extension
$ ruby aws-s3.rb mysong.mp3

# or explicitly (file, bucket, mime-type)
$ ruby aws-s3.rb mysong.mp3 doug_mp3 'audio/mp3'

Saturday, July 12, 2008

ruby-mp3info

I have an old SanDisk mp3 player that I use to listen to music and podcasts. Though it's a rather small card (512M), it's still annoying to have to scroll through my single playlist to get to a specific file.

The SanDisk sorts by the mp3 info title tag, so I wrote a small script that I run on my podcast files before I load them on my player so that they will show up first in the playlist by prepending '01_' to the mp3 title tag (and using the mp3 filename if the title is nil).

First, install the ruby-mp3info gem if you don't have it:

gem install ruby-mp3info

edit_mp3info_title.rb

require 'rubygems'
require "mp3info"

dir = Dir.open('/Users/dsparlingimbp/PODCAST/mp3')
puts "mp3 directory: #{dir.path}"

dir.each do |file|
next unless /\.mp3$/ =~ file
puts "mp3: #{file}"

mp3file = "#{dir.path}/#{file}"

Mp3Info.open(mp3file) do |mp3|
puts "original title: #{mp3.tag.title}"
title = '01_' + (mp3.tag.title || file)
mp3.tag.title = title
puts "new title: #{mp3.tag.title}"
end

end

About Me

My photo
Developer (Ruby on Rails, iOS), musician/composer, Buddhist, HSP, Vegan, Aspie.

Labels