Merge pull request #82 from natankeddem/ffmpeg0
Some checks failed
Installing via pip and running / install-and-run-on-python-3-10 (push) Has been cancelled
Git clone and run / linux-git-clone-and-run (push) Has been cancelled
Git clone and run / windows-git-clone-and-run (push) Has been cancelled
Installing via pip and running / install-and-run-on-python-3-11 (push) Has been cancelled
Installing via pip and running / install-and-run-on-python-3-12 (push) Has been cancelled
Installing via pip and running / windows-git-clone-and-run (push) Has been cancelled

Improved ffmpeg arguments for m4b creation.
This commit is contained in:
Claudio Santini 2025-03-02 19:28:03 +01:00 committed by GitHub
commit ef74c1bb15
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -313,25 +313,33 @@ def create_m4b(chapter_files, filename, cover_image, output_folder):
cover_file_path = Path(output_folder) / 'cover'
with open(cover_file_path, 'wb') as f:
f.write(cover_image)
cover_image_args = ["-i", f'{cover_file_path}', "-map", "0:a", "-map", "2:v"]
cover_image_args = [
'-i', f'{cover_file_path}',
'-map', '2:v', # Map cover image
'-disposition:v', 'attached_pic', # Ensure cover is embedded
'-c:v', 'copy', # Keep cover unchanged
]
else:
cover_image_args = []
proc = subprocess.run([
'ffmpeg',
'-y', # overwrite output file without asking
'-i', f'{concat_file_path}', # input 0 file (audio)
'-i', f'{chapters_txt_path}', # input 1 file (chapters)
*cover_image_args, # cover image
'-map', '0', # map all streams from input 0
'-map_metadata', '1', # map metadata from input 1
'-c:a', 'copy', # copy audio codec
'-c:v', 'copy', # copy video codec
'-disposition:v', 'attached_pic', # attach cover image
'-c', 'copy', # copy codec
'-f', 'mp4', # format
f'{final_filename}' # output file
'-y', # Overwrite output
'-i', f'{concat_file_path}', # Input audio
'-i', f'{chapters_txt_path}', # Input chapters
*cover_image_args, # Cover image (if provided)
'-map', '0:a', # Map audio
'-c:a', 'aac', # Convert to AAC
'-b:a', '64k', # Reduce bitrate for smaller size
'-map_metadata', '1', # Map metadata
'-f', 'mp4', # Output as M4B
f'{final_filename}' # Output file
])
Path(concat_file_path).unlink()
if proc.returncode == 0:
print(f'{final_filename} created. Enjoy your audiobook.')