1
0
Fork 0

make mp3 into m4b with chapters

This commit is contained in:
Tyrel Souza 2023-10-08 14:31:32 -04:00
commit 083971d9b0
No known key found for this signature in database
GPG Key ID: F3614B02ACBE438E
2 changed files with 72 additions and 0 deletions

71
chapters.py Normal file
View File

@ -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()

1
requirements.txt Normal file
View File

@ -0,0 +1 @@
mutagen