This repository was archived by the owner on May 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmp3chapter.py
More file actions
executable file
·52 lines (41 loc) · 1.46 KB
/
Copy pathmp3chapter.py
File metadata and controls
executable file
·52 lines (41 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python3
# encoding: utf-8
"""
encode mp3 chapter
"""
import eyed3
def add_next_time(data, musicfile):
maxlen = len(data) - 1
for i, line in enumerate(data):
if i == maxlen:
data[i][0] = eyed3.id3.frames.StartEndTuple(
start=data[i][0], end=musicfile.info.time_secs)
else:
data[i][0] = eyed3.id3.frames.StartEndTuple(
start=data[i][0], end=data[i + 1][0])
return data
def parse_chapters_file(fname):
chaps = []
with open(fname, "r") as f:
for line in f.readlines():
time, title = line.split()[0], " ".join(line.split()[1:])
chaps.append([to_millisecs(time), title.strip()])
return chaps
def add_chapters(fname, chapterfile):
audio = eyed3.load(fname)
tag = audio.tag
chaps = parse_chapters_file(chapterfile)
chaps = add_next_time(chaps, audio)
# total_length = int(tag.getTextFrame('TLEN'))
list_of_chapters = []
for i, c in enumerate(chaps):
chaptername = "chp{}".format(i).encode("utf-8")
list_of_chapters.append(chaptername)
new_chapter = tag.chapters.set(chaptername, c[0])
new_chapter.sub_frames.setTextFrame(b"TIT2", c[1])
# print(tag.table_of_contents.get(b"toc"))
tag.table_of_contents.set(b"toc", child_ids=list_of_chapters)
tag.save()
def to_millisecs(time):
h, m, s = [float(x) for x in time.split(":")]
return int(1000 * (s + m * 60 + h * 60 * 60))