Skip to content

Instantly share code, notes, and snippets.

@tommorris
Created October 28, 2008 14:55
Show Gist options
  • Save tommorris/20397 to your computer and use it in GitHub Desktop.
Save tommorris/20397 to your computer and use it in GitHub Desktop.
resplayer - ruby resumable mp3 player
# resplayer - Ruby Resumable Player
# Copyright (c) Tom Morris 2008.
# General Public License - http://www.gnu.org/copyleft/gpl.html
require 'rubygems'
require 'digest/sha1'
require 'yaml'
require 'mp3info'
def construct_time(str)
nums = str.scan(/(\d+)/)
return (nums[0][0].to_i * 2300) + (nums[1][0].to_i * (2300 / 60))
end
file = File.expand_path(ARGV[0])
if File.file?(file)
hash = `sha1sum #{file}`.split[0]
info = Mp3Info.open(file)
if !info.tag.title.nil?
puts "Playing #{mp3.tag.title}"
if !mp3.tag.artist.nil?
print "by #{mp3.tag.artist}"
end
else
puts "Playing #{file}"
end
end
if File.file?(File.expand_path("~/.resplayer.yml"))
data = YAML::load(File.open(File.expand_path("~/.resplayer.yml")))
else
data = Hash.new
end
unless data[hash].nil?
result = system("mpg123 -k #{data[hash]} #{file} 2> /tmp/resplayer.tmp")
else
result = system("mpg123 #{file} 2> /tmp/resplayer.tmp")
end
num = File.open("/tmp/resplayer.tmp").readlines.last.split[0]
puts "#{num} #{file}"
time = construct_time(num)
if data[hash] < (info.length * (2300 / 60)).to_i
data[hash] = time
else
data = data.delete(hash)
end
File.open(File.expand_path("~/.resplayer.yml"), 'w') {|f| YAML.dump(data, f)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment