Skip to content

Instantly share code, notes, and snippets.

@johnlpollard
Forked from dan/tagMp3Podcast.rb
Created July 18, 2018 19:09
Show Gist options
  • Save johnlpollard/180f23153b8f77d204d8bc3ec0e954e4 to your computer and use it in GitHub Desktop.
Save johnlpollard/180f23153b8f77d204d8bc3ec0e954e4 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment