gpt4 book ai didi

ruby - 使用 ruby​​ mp3info 从外部站点读取 mp3 ID3(不加载整个文件)

转载 作者:数据小太阳 更新时间:2023-10-29 07:46:55 25 4
gpt4 key购买 nike

我在服务器上有一个文件列表,我想从每个文件中只加载和解析 ID3。

下面的代码加载了整个文件,这在批处理时(显然)非常耗时。

require 'mp3info'
require 'open-uri'

uri = "http://blah.com/blah.mp3"

Mp3Info.open(open(uri)) do |mp3|
puts mp3.tag.title
puts mp3.tag.artist
puts mp3.tag.album
puts mp3.tag.tracknum
end

最佳答案

这个解决方案适用于 id3v2(当前标准)。 ID3V1 在文件开头没有元数据,因此在这些情况下它不起作用。

这会读取文件的前 4096 个字节,这是任意的。据我所知 ID3 documentation ,大小没有限制,但 4kb 是我不再在我的库中出现解析错误的时候。

我能够构建一个简单的 dropbox 音频播放器,可以在这里看到: soundstash.heroku.com

并在此处开源代码:github.com/miketucker/Dropbox-Audio-Player

require 'open-uri'
require 'stringio'
require 'net/http'
require 'uri'
require 'mp3info'

url = URI.parse('http://example.com/filename.mp3') # turn the string into a URI
http = Net::HTTP.new(url.host, url.port)
req = Net::HTTP::Get.new(url.path) # init a request with the url
req.range = (0..4096) # limit the load to only 4096 bytes
res = http.request(req) # load the mp3 file
child = {} # prepare an empty array to store the metadata we grab
Mp3Info.open( StringIO.open(res.body) ) do |m| #do the parsing
child['title'] = m.tag.title
child['album'] = m.tag.album
child['artist'] = m.tag.artist
child['length'] = m.length
end

关于ruby - 使用 ruby​​ mp3info 从外部站点读取 mp3 ID3(不加载整个文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9736882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com