aedocw / epub2tts-edge

epub2tts-edge uses Microsoft Edge cloud-based TTS to create a full featured audiobook m4b from an epub or text file
GNU General Public License v3.0
95 stars 14 forks source link

Put chapters in metadata perhaps #21

Closed sorbx64 closed 5 months ago

sorbx64 commented 5 months ago

I successfully converted my epub file to m4b, it worked perfectly and produced good quality audio. But, it will be great it could put chapters with starttime / timestamps etc in metadata of the m4b. that will save a lot of time of having to go through entire audiobook and assign chapters. Also, sorry if it should have been a discussion topic than an issue. discussion page seem not to be available.

aedocw commented 5 months ago

I'm a little confused, as it normally does put chapters into metadata. When the script is exporting the book to a text file, it detects chapter breaks and even tries to find the chapter title. Each of those will appear on a line with a # in front like:

# Chapter one

Every time a line starts with #, the script treats that as a chapter change. Then at the end when it combines all the chapters into a single audio file it measures the length of each of them and uses them to create a metadata file that is used to create the final m4b.

Did your text file have any lines beginning with #?

Also I hadn't noticed the discussion option was not enabled for this project, I'll turn that on - thank you for bringing it to my attention :)

sorbx64 commented 5 months ago

Did your text file have any lines beginning with #?

Yes. My file (.txt) generated by running epub2tts-edge on epub file had chapters in # Chapter Name format. In fact, i checked and modified many chapter names. The m4b thus produced had good pauses indicating chapter breaks where i wanted them. But chapters with timestamps were not available as a metadata on final m4b file. Thats the issue. Here is wsl terminal output of all the metadata of m4b file.

output_of_mainpy

main.py is what I quickly put together to log all metadata of m4b file. below is its code for reference -

from mutagen.mp4 import MP4

def extract_chapters(file_path):
    audio = MP4(file_path)

    # Print all available tags and their values
    for tag, value in audio.tags.items():
        print(f"{tag}: {value}")

if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser(description="Extract chapter data from an M4B file.")
    parser.add_argument("file_path", type=str, help="Path to the M4B file")

    args = parser.parse_args()
    extract_chapters(args.file_path)

Just for reference, in case you are wondering covr tag is absent in metadata logged in terminal output. It is because i have deleted it using another python script because i wanted cover in audiobook but after the m4b was created i changed my mind. But i assure you that tag deleting script only deleted covr tag. Even before the tag deleting script was run, I had logged all the metadata of m4b using main.py yet chapter data was absent in it.

aedocw commented 5 months ago

That's really strange. Can you try making a copy of the text file and removing most of the content, maybe just leave a few sentences and two or three chapters, and run epub2tts-edge with that? Then use ffprobe <file.m4b> and share the output?

sorbx64 commented 5 months ago

hey, I ran epub2tts-edge on new stripped text file. Then ran ffprobe on produced m4b file and here is output -

Screenshot (283)

I see there is metadata that is visible using ffprobe. After this, I ran ffprobe on original audiobook.m4b and it logs out the metadata including chapters with timestamps! My main.py python script that uses mutagen.mp4 seems to neglect the chapter data, I don't know why. Although, thank you, my problem is resolved. I will use ffprobe next time to log out chapters data.

Besides, Is there a option or a way to make epub2tts-edge directly output mp4 file instead of m4b file?

aedocw commented 5 months ago

That's excellent, I'm glad we were able to sort this out!

There is no option to directly output to mp4 instead of m4b unfortunately.