-
Notifications
You must be signed in to change notification settings - Fork 1
/
write_chapters.py
107 lines (90 loc) · 3.77 KB
/
write_chapters.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from typing import List
# chapter object
class Chapter(object):
def __init__(self, time = float, title = str):
# chapter start time
self.time = time
# chapter title
self.title = title
def to_dict(self):
return {
'time': self.time,
'title': self.title
}
# Chapter writer class to handle writing chapter information
# to different files (i.e. audio files or text files)
# * main method write_chapters wirtes chapters to a file (i.e. text- or audio file)
# * should only accepts audio in suitable format
class ChapterWriter(object):
def write_chapters(self, format: str, chapters = List[Chapter], filepath = str):
writer = _get_writer(format)
return writer(chapters, filepath)
def _get_writer(format):
if format == 'txt':
return _write_to_txt
elif format == 'mp3':
return _write_to_mp3
elif format == 'm4a':
return _write_to_m4a
else:
raise ValueError(format)
def _write_to_mp3(chapters = List[Chapter], filepath = str):
import eyed3
from eyed3.id3 import Tag
# write chapters to mp3 metadata
tag = Tag()
tag.parse(filepath)
# remove chapters if already set
# tag.table_of_contents.set(b'toc', child_ids=[])
# for every chapter id (i.e. 'ch0' od. 'chp0'), do
# tag.chapters.remove(b'chp3')
chapterIds = []
for i, chapter in enumerate(chapters):
endTime = int(chapter.time * 1000)
startTime = 0
if i != 0:
startTime = int(chapters[i-1].time * 1000)
chapterId = str.encode('ch' + str(i))
newChapter = tag.chapters.set(chapterId, (startTime, endTime))
newChapter.sub_frames.setTextFrame(b'TIT2', u'{}'.format(chapter.title))
chapterIds.append(chapterId)
tag.table_of_contents.set(b"toc", child_ids=chapterIds)
tag.save()
def _write_to_txt(chapters = List[Chapter], filepath = str):
from datetime import datetime
# write chapters to text file
with open(filepath, 'w') as f:
out = ''
for chapter in chapters:
timeStr = datetime.utcfromtimestamp(chapter.time).strftime('%H:%M:%S.%f')[:-3]
out += '{} {}\n'.format(timeStr, chapter.title)
f.write(out)
def _write_to_m4a(chapters = List[Chapter], filepath = str):
from datetime import datetime
from subprocess import call
from os import path
import tempfile
text_samples = ''
for chapter in chapters:
time_str = datetime.utcfromtimestamp(chapter.time).strftime('%H:%M:%S.%f')[:-3]
text_samples += f'<TextSample sampleTime="{time_str}" sampleDescriptionIndex="1" xml:space="preserve">{chapter.title}</TextSample>'
ttxt_template = (
f'<?xml version="1.0" encoding="UTF-8" ?>'
f'<!-- GPAC 3GPP Text Stream -->'
f'<TextStream version="1.1">'
f' <TextStreamHeader width="0" height="0" layer="65535" translation_x="0" translation_y="0">'
f' <TextSampleDescription horizontalJustification="center" verticalJustification="bottom" backColor="1f 1f 1f 0" verticalText="no" fillTextRegion="no" continuousKaraoke="no" scroll="None">'
f' <FontTable>'
f' <FontTableEntry fontName="Sans-Serif" fontID="1"/>'
f' </FontTable>'
f' <TextBox top="0" left="0" bottom="0" right="0"/>'
f' <Style styles="Bold " fontID="1" fontSize="18" color="0 0 0 ff"/>'
f' </TextSampleDescription>'
f' </TextStreamHeader>'
f'{text_samples}'
f'</TextStream>'
)
with tempfile.NamedTemporaryFile(mode='w', suffix='.ttxt') as tf:
tf.write(ttxt_template)
tf.flush()
call(f'MP4Box -chap "{tf.name}" "{filepath}"', shell=True)