Skip to content

Instantly share code, notes, and snippets.

@Foolson
Last active April 4, 2018 17:32
Show Gist options
  • Save Foolson/751981f47ecf079f83ebdc1fdb6b976b to your computer and use it in GitHub Desktop.
Save Foolson/751981f47ecf079f83ebdc1fdb6b976b to your computer and use it in GitHub Desktop.
Script to add id3v2 chapters to .mp3 files
from mutagen.id3 import ID3, CTOC, CHAP, TIT2, CTOCFlags
import time
import datetime
audioFile = input("Audio file: ")
audio = ID3(audioFile)
numberOfChapters = int(input("Number of chapters: ")) + 1
chapters = {}
for x in range(1, numberOfChapters):
print("Chapter "+str(x))
title = input("Chapter title: ")
startTime = input("Chapter start time (H:M:S): ")
a = time.strptime(startTime, "%H:%M:%S")
startTime = datetime.timedelta(hours=a.tm_hour, minutes=a.tm_min, seconds=a.tm_sec).seconds * 1000
endTime = input("Chapter end time (H:M:S): ")
a = time.strptime(endTime, "%H:%M:%S")
endTime = datetime.timedelta(hours=a.tm_hour, minutes=a.tm_min, seconds=a.tm_sec).seconds * 1000
chapters["ch"+str(x)] = {"startTime": startTime, "endTime": endTime, "title": title}
tocList = ','.join(chapters.keys())
for key, value in chapters.items():
audio.add(CHAP(element_id=key, start_time=value["startTime"], end_time=value["endTime"], sub_frames=[TIT2(text=[value["title"]])]))
audio.add(CTOC(element_id=u"toc", flags=CTOCFlags.TOP_LEVEL | CTOCFlags.ORDERED, child_element_ids=[tocList], sub_frames=[TIT2(text=[u"Table of contents"])]))
audio.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment