ruby-ogginfo 0.3 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +5 -0
- data/lib/ogginfo.rb +19 -14
- data/test/test_ruby-ogginfo.rb +11 -0
- metadata +1 -1
data/History.txt
CHANGED
data/lib/ogginfo.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# $Id: ogginfo.rb
|
1
|
+
# $Id: ogginfo.rb 39 2008-03-15 17:21:31Z moumar $
|
2
2
|
#
|
3
3
|
# see http://www.xiph.org/ogg/vorbis/docs.html for documentation on vorbis format
|
4
4
|
# http://www.xiph.org/ogg/vorbis/doc/v-comment.html
|
@@ -22,7 +22,7 @@ end
|
|
22
22
|
class OggInfoError < StandardError ; end
|
23
23
|
|
24
24
|
class OggInfo
|
25
|
-
VERSION = "0.3"
|
25
|
+
VERSION = "0.3.1"
|
26
26
|
attr_reader :channels, :samplerate, :bitrate, :nominal_bitrate, :length
|
27
27
|
|
28
28
|
# +tag+ is a hash containing the vorbis tag like "Artist", "Title", and the like
|
@@ -30,18 +30,19 @@ class OggInfo
|
|
30
30
|
|
31
31
|
# create new instance of OggInfo, using +charset+ to convert tags to
|
32
32
|
def initialize(filename, charset = "iso-8859-1")
|
33
|
-
@
|
34
|
-
|
35
|
-
|
36
|
-
end
|
33
|
+
@filename = filename
|
34
|
+
@charset = charset
|
35
|
+
@file = File.new(@filename, "rb")
|
37
36
|
|
38
37
|
find_next_page
|
39
38
|
extract_infos
|
40
39
|
find_next_page
|
41
|
-
extract_tag
|
40
|
+
extract_tag
|
41
|
+
convert_tag_charset("utf-8", @charset)
|
42
42
|
@saved_tag = @tag.dup
|
43
43
|
extract_end
|
44
44
|
@bitrate = @file.stat.size.to_f*8/@length
|
45
|
+
@file.close
|
45
46
|
end
|
46
47
|
|
47
48
|
# "block version" of ::new()
|
@@ -62,15 +63,14 @@ class OggInfo
|
|
62
63
|
|
63
64
|
# write any tags to file
|
64
65
|
def close
|
65
|
-
@ic.close
|
66
|
-
@file.close if @file and not @file.closed?
|
67
66
|
if @tag != @saved_tag
|
68
67
|
cmd = %w{vorbiscomment -w}
|
68
|
+
convert_tag_charset(@charset, "utf-8")
|
69
69
|
|
70
70
|
@tag.each do |k,v|
|
71
71
|
cmd.concat(["-t", k.upcase+"="+v])
|
72
72
|
end
|
73
|
-
cmd << @
|
73
|
+
cmd << @filename
|
74
74
|
system(*cmd)
|
75
75
|
end
|
76
76
|
end
|
@@ -112,7 +112,7 @@ private
|
|
112
112
|
end
|
113
113
|
end
|
114
114
|
|
115
|
-
def extract_tag
|
115
|
+
def extract_tag
|
116
116
|
@tag = {}
|
117
117
|
@file.seek(22, IO::SEEK_CUR)
|
118
118
|
segs = @file.read(1).unpack("C")[0]
|
@@ -124,9 +124,6 @@ private
|
|
124
124
|
tag_size.times do |i|
|
125
125
|
size = @file.read(4).unpack("V")[0]
|
126
126
|
comment = @file.read(size)
|
127
|
-
if @ic
|
128
|
-
comment = @ic.iconv( com ) rescue comment
|
129
|
-
end
|
130
127
|
key, val = comment.split(/=/)
|
131
128
|
@tag[key.downcase] = val
|
132
129
|
end
|
@@ -142,4 +139,12 @@ private
|
|
142
139
|
@length = 0
|
143
140
|
end
|
144
141
|
end
|
142
|
+
|
143
|
+
def convert_tag_charset(from_charset, to_charset)
|
144
|
+
Iconv.open(to_charset, from_charset) do |ic|
|
145
|
+
@tag.each do |k, v|
|
146
|
+
@tag[k] = ic.iconv(v)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
145
150
|
end
|
data/test/test_ruby-ogginfo.rb
CHANGED
@@ -142,6 +142,17 @@ class OggInfoTest < Test::Unit::TestCase
|
|
142
142
|
end
|
143
143
|
end
|
144
144
|
|
145
|
+
def test_charset
|
146
|
+
generate_ogg
|
147
|
+
OggInfo.open("test.ogg", "utf-8") do |ogg|
|
148
|
+
ogg.tag["title"] = "hello\303\251"
|
149
|
+
end
|
150
|
+
|
151
|
+
OggInfo.open("test.ogg", "iso-8859-1") do |ogg|
|
152
|
+
assert_equal "hello\xe9", ogg.tag["title"]
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
145
156
|
def generate_ogg
|
146
157
|
unless test(?f, "test.ogg")
|
147
158
|
system("dd if=/dev/urandom bs=1024 count=3000 | oggenc -q0 --raw -o test.ogg -") or
|