Add title and author metadata to .m4b file

This commit is contained in:
Dave Nesbitt 2025-01-15 22:29:10 +01:00
parent 75cb3d1a64
commit d331a9ea5d

View file

@ -73,7 +73,7 @@ def main(kokoro, file_path, lang, voice, pick_manually):
print('Progress:', f'{progress}%') print('Progress:', f'{progress}%')
i += 1 i += 1
if has_ffmpeg: if has_ffmpeg:
create_m4b(chapter_mp3_files, filename) create_m4b(chapter_mp3_files, filename, title, creator)
def extract_texts(chapters): def extract_texts(chapters):
@ -138,18 +138,23 @@ def strfdelta(tdelta, fmt='{D:02}d {H:02}h {M:02}m {S:02}s'):
return f.format(fmt, **values) return f.format(fmt, **values)
def create_m4b(chaptfer_files, filename): def create_m4b(chapter_files, filename, title, author):
tmp_filename = filename.replace('.epub', '.tmp.m4a') tmp_filename = filename.replace('.epub', '.tmp.m4a')
if not Path(tmp_filename).exists(): if not Path(tmp_filename).exists():
combined_audio = AudioSegment.empty() combined_audio = AudioSegment.empty()
for wav_file in chaptfer_files: for wav_file in chapter_files:
audio = AudioSegment.from_wav(wav_file) audio = AudioSegment.from_wav(wav_file)
combined_audio += audio combined_audio += audio
print('Converting to Mp4...') print('Converting to Mp4...')
combined_audio.export(tmp_filename, format="mp4", codec="aac", bitrate="64k") combined_audio.export(tmp_filename, format="mp4", codec="aac", bitrate="64k")
final_filename = filename.replace('.epub', '.m4b') final_filename = filename.replace('.epub', '.m4b')
print('Creating M4B file...') print('Creating M4B file...')
proc = subprocess.run(['ffmpeg', '-i', f'{tmp_filename}', '-c', 'copy', '-f', 'mp4', f'{final_filename}']) proc = subprocess.run([
'ffmpeg', '-i', f'{tmp_filename}', '-c', 'copy', '-f', 'mp4',
'-metadata', f'title={title}',
'-metadata', f'author={author}',
f'{final_filename}'
])
Path(tmp_filename).unlink() Path(tmp_filename).unlink()
if proc.returncode == 0: if proc.returncode == 0:
print(f'{final_filename} created. Enjoy your audiobook.') print(f'{final_filename} created. Enjoy your audiobook.')