commit 083971d9b03f452472af25345afa76811a3a6f15 Author: Tyrel Souza Date: Sun Oct 8 14:31:32 2023 -0400 make mp3 into m4b with chapters diff --git a/chapters.py b/chapters.py new file mode 100644 index 0000000..6f8c990 --- /dev/null +++ b/chapters.py @@ -0,0 +1,71 @@ +import math +import glob +import subprocess +from mutagen.mp3 import MP3 + + +class Chapters: + + def __init__(self, title, author): + self.title = title + self.author = author + self.file_names = sorted(glob.glob("*.mp3")) + self.FFMETADATAFILE = f""";FFMETADATA1 +title={self.title} +author={self.author} +encoder=libfdk_aac + +""" + self.file_list = "" + + def generate_metadata(self): + start = 1 + + for f in self.file_names: + self.file_list += f"file '{f}'\n" + mp3 = MP3(f) + l = int(mp3.info.length * 1000) + title = f[:-4] + + self.FFMETADATAFILE += f"""[CHAPTER] +TIMEBASE=1/1000 +START={start} +END={start+l} +title={title} + +""" + start = start+l + + def convert(self): + commands = [ + 'ffmpeg', + '-f', 'concat', + '-safe', '0', + '-i', 'file_list.txt', + '-i', 'FFMETADATAFILE.txt', + '-map_metadata', '1', + '-c:a', 'libfdk_aac', + '-b:a', '128k', + '-f', 'mp4', f'"{self.title}.m4b"', + ] + if subprocess.run(commands).returncode == 0: + print("FFmpeg Script Ran Successfully") + else: + print("There was an error running your FFmpeg script") + + +def main(): + title = input("Title: ") + author = input("Author: ") + ch = Chapters(title=title, author=author) + ch.generate_metadata() + with open("FFMETADATAFILE.txt", "w") as f: + print(ch.FFMETADATAFILE, file=f) + with open("file_list.txt", "w") as f: + print(ch.file_list, file=f) + + ch.convert() + + +if __name__ == "__main__": + main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..26ced3f --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +mutagen