Skip to content

Instantly share code, notes, and snippets.

@dan
Created June 23, 2014 15:33
Show Gist options
  • Save dan/0928b9a8a377926da4a0 to your computer and use it in GitHub Desktop.
Save dan/0928b9a8a377926da4a0 to your computer and use it in GitHub Desktop.
require 'mp3info'
# some test data, replace it with your own data
title = "After Dark #388: After Back to Work"
subtitle = "Merlin & Dan talk after Back to Work episode 104."
artist = "Dan Benjamin on 5by5.tv"
album = "After Dark"
genre = "Podcast"
track = 338
year = 2013
# you need this to handle iTunes correctly
# if the user drags the file into iTunes, it should be displayed as a podcast,
# not as a random music file
episode_guid = "http://5by5.tv/afterdark/338"
feed_url = "http://feeds.5by5.tv/afterdark"
# first chapter should start at time: <0>
# last chapter should end at time: <duration>
chapters = [
{:title => "Chapter 1", :description => "Some description for Chapter 1", :start => 0, :end => 30},
{:title => "Chapter 2", :link => "http://www.5by5.com/2", :start => 30, :end => 60},
]
# open an mp3 file
Mp3Info.open("/Users/hering/5by5-chapters/afterdark-338.mp3") do |mp3|
# Set ID3v1 Tags
mp3.tag1.title = title if !title.nil?
mp3.tag1.artist = artist if !artist.nil?
mp3.tag1.album = album if !album.nil?
mp3.tag1.year = year if year > 0
mp3.tag1.tracknum = track if track > 0
mp3.tag1.genre_s = genre if !genre.nil?
# Set ID3v2 Tags
mp3.tag2.title = title if !title.nil?
mp3.tag2.TIT3 = subtitle if !subtitle.nil?
mp3.tag2.artist = artist if !artist.nil?
mp3.tag2.album = album if !album.nil?
mp3.tag2.genre_s = genre if !genre.nil?
mp3.tag2.tracknum = track if track > 0
mp3.tag2.year = year if year > 0
# This makes sure that if the user drags the file into iTunes, it is properly
# displayed as a podcast and the feed can be subscribed to
if (!feed_url.nil? && !episode_guid.nil?) then
mp3.tag2.PCST = "\x00\x00\x00\x00"
mp3.tag2.TGID = episode_guid
mp3.tag2.WFED = "\x00#{feed_url}\x00"
end
# optionally remove current picture
mp3.tag2.remove_pictures
# add image as 'cover (front)' type
# see: http://id3.org/id3v2.3.0#Attached_picture
file = File.new('/Users/hering/5by5-chapters/cover.jpg','rb')
mp3.tag2.add_picture(file.read, {:pic_type => 3, :mime => "image/jpeg", :description => "Cover"} )
# create chapters and chapter toc
# don't change! we have to do this binary style since the lib does not have
# explicit suppport for that
if (chapters.size > 0) then
chaps = []
ctoc = "toc1\x00"
ctoc << [3, chapters.size].pack("CC")
chapters.each_with_index do |ch, i|
num = i+1
title = ch[:title]
description = ch[:description]
link = ch[:link]
ctoc << "ch#{num}\x00"
chap = "ch#{num}\x00"
chap << [ch[:start]*1000, ch[:end]*1000].pack("NN");
chap << "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
title_tag = [title.encode("utf-16")].pack("a*");
chap << "TIT2"
chap << [title_tag.size+1].pack("N")
chap << "\x00\x00\x01"
chap << title_tag
if !description.nil? then
description_tag = [description.encode("utf-16")].pack("a*")
chap << "TIT3"
chap << [description_tag.size+1].pack("N")
chap << "\x00\x00\x01"
chap << description_tag
end
if !link.nil? then
chap << "WXXX"
chap << [link.length+2].pack("N")
chap << "\x00\x00\x00#{link}\00"
end
chaps << chap
end
mp3.tag2.CTOC = ctoc
mp3.tag2.CHAP = chaps
end
# this is just to print out the tags
#puts mp3
end
# once this blocks ends the file is saved automatically
@johnlpollard
Copy link

When I try to run this, I get the following error. incompatible character encodings: UTF-8 and ASCII-8BIT (Encoding::CompatibilityError) Do you know what I can do to make this script work?

@johnlpollard
Copy link

It only occurs because of this line chap << "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"

@johnlpollard
Copy link

I figured out .force_encoding('ASCII-8BIT') allows it to work. Now to figure out why the links are not working....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment