[Ruby乐趣]读写Mp3 ID3标签

本文介绍了一个使用 Ruby 编写的简单 ID3 v1.1 标签解析器和编辑器,该程序能够从音频文件中读取并修改元数据如标题、艺术家等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/usr/bin/ruby -w                  #
#
 Ruby ID3 Tag v1.1 Library         #
#
 Version: 0.90                     #
#
 (c)2006 Suninny's Rails Theme     #
#
   http://blog.youkuaiyun.com/rails      #
#
####################################

class ID3
  
def initialize(filename)
    @filename 
= filename
    begin
      File.open(filename) do 
|file|
        file.seek(
-128, IO::SEEK_END)
        data 
= file.read
        tag, title, artist, album, year, comment, genre 
=
          data.unpack(
"A3A30A30A30A4A30C1")
        
if tag != 'TAG'
          clear
        
else
          @title, @artist, @album, @year, @comment, @genre 
=
            title, artist, album, year, comment, genre
        end
      end
    rescue
      clear
    end
  end

  attr_accessor :title, :artist, :album, :comment, :year, :genre

  
def write
    File.open(@filename, 
"r+") do |file|
      file.seek(
-128, IO::SEEK_END)
      data 
= file.read
      tag, title, artist, album, year, comment, genre 
=
        data.unpack(
"A3A30A30A30A4A30C1")
      
if tag == 'TAG'
        file.seek(
-128, IO::SEEK_END)
      
else
        file.seek(0, IO::SEEK_END)
      end
      file.write bin
    end
  end

  
def bin
    title   
= @title + (" " * 30)
    artist  
= @artist + (" " * 30)
    album   
= @album + (" " * 30)
    comment 
= @comment + (" " * 30)
    [
'TAG', title, artist, album, @year, comment, @genre].
      pack(
"a3a30a30a30a4a30C1")
  end

  
def clear
    @title
=@artist=@album=@year=@comment = ''
    @genre 
= 0
  end
end
用wxRuby作了个简易界面:

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值