Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing frames from ID3v2 tag #11

Closed
mitchlloyd opened this issue Jun 9, 2012 · 9 comments
Closed

Removing frames from ID3v2 tag #11

mitchlloyd opened this issue Jun 9, 2012 · 9 comments

Comments

@mitchlloyd
Copy link

I was looking for a way to clear out all of the frames from an ID3 tag before setting them. I thought I would cycle through each frame with frame_list and call remove_frame on the tag for each frame. However, I'm having trouble. Whenever I remove frames it seems like I can't get the change to "stick" and they're back when I open the file again.

Here's a script showing the problem I'm having.

require 'taglib'

FILENAME = 'example.mp3'
@file = File.new(FILENAME)

TagLib::MPEG::File.open(FILENAME) do |file|
  tag = file.id3v2_tag
  tag.artist = "Britt Daniels"
  file.save
end

TagLib::MPEG::File.open(FILENAME) do |file|
  tag = file.id3v2_tag
  artist = tag.frame_list('TPE1').first.to_s
  puts "After setting the artist, TPE1 is #{artist}"
end

TagLib::MPEG::File.open(FILENAME) do |file|
  tag = file.id3v2_tag
  tag.remove_frames('TPE1')
  file.save
end

TagLib::MPEG::File.open(FILENAME) do |file|
  tag = file.id3v2_tag
  artist = tag.frame_list('TPE1').first.to_s
  puts "After removeing TPE1, TPE1 is still #{artist}"
end

Is there a way to accomplish what I'm trying to do here?

@mitchlloyd
Copy link
Author

I fixed this problem by clearing out the frames and removing the last 128 bytes from the file (the ID3v1 tag info). This was a pretty tough gotcha. I didn't see a method for clearing out the tag data, so maybe that would be a nice addition to this library.

@robinst
Copy link
Owner

robinst commented Jun 10, 2012

The problem is that TagLib combines the two tag versions (v1 and v2), if both exist.

To get rid of this, specify that only ID3v2 should be saved (all other tags are stripped with this):

file.save(TagLib::MPEG::File::ID3v2)

That wasn't yet documented (sorry about that), I'll do that and keep this issue open until that is done.

@mitchlloyd
Copy link
Author

Ah thanks. That will help me clean up some code. Also thanks for the library -- vey helpful!

@mitchlloyd
Copy link
Author

This only saves the ID3v2 tags as expected but it doesn't seem to strip out the ID3v1 tags. I'm using version 0.5.0 from rubygems.org.

@robinst
Copy link
Owner

robinst commented Jun 10, 2012

Hm, how did you check if the ID3v1 tag is still there?

@mitchlloyd
Copy link
Author

OK, so sorry for the conjecture there. It seems that the ID3v1 tags are removed, but somehow when loading up the id3v2 tag, the artist information is back:

require 'taglib'

FILENAME = 'example.mp3'
@file = File.new(FILENAME, 'w')

TagLib::MPEG::File.open(FILENAME) do |file|
  tag = file.id3v2_tag
  tag.artist = "Britt Daniel"
  file.save # Saving both id3v2 and id3v1 I suppose.
end

TagLib::MPEG::File.open(FILENAME) do |file|
  tag = file.id3v2_tag
  tag.remove_frames('TPE1')

  # Saving just the ID3v2, should strip ID3v1
  file.save(TagLib::MPEG::File::ID3v2)
end

TagLib::MPEG::File.open(FILENAME) do |file|
  tag = file.id3v1_tag
  artist = tag.artist
  puts "When I get just the id3v1 tag, artist is not there: #{artist}"

  tag = file.id3v2_tag
  artist = tag.artist
  puts "But if I load the id3v2 tag, artist is back: #{artist}"
end

@robinst
Copy link
Owner

robinst commented Jun 10, 2012

Ok, turns out this is a non-intuitive behavior of TagLib. I've sent a pull request with a fix:

taglib/taglib#40

In the meantime, use the following to get the desired behavior:

TagLib::MPEG::File.open(FILENAME) do |file|
  tag = file.id3v2_tag
  tag.remove_frames('TPE1')

  file.strip(TagLib::MPEG::File::ID3v1)
  file.save(TagLib::MPEG::File::ID3v2)
end

@mitchlloyd
Copy link
Author

Yes, seems to work great. Thanks for all the help with this.

@robinst
Copy link
Owner

robinst commented Jun 16, 2012

Released 0.5.1 which documents strip and save -> closing this.

@robinst robinst closed this as completed Jun 16, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants