Skip to content

Commit ef74c1b

Browse files
authored
Merge pull request #82 from natankeddem/ffmpeg0
Improved ffmpeg arguments for m4b creation.
2 parents 3614390 + 03252cf commit ef74c1b

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

audiblez/core.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -313,25 +313,33 @@ def create_m4b(chapter_files, filename, cover_image, output_folder):
313313
cover_file_path = Path(output_folder) / 'cover'
314314
with open(cover_file_path, 'wb') as f:
315315
f.write(cover_image)
316-
cover_image_args = ["-i", f'{cover_file_path}', "-map", "0:a", "-map", "2:v"]
316+
cover_image_args = [
317+
'-i', f'{cover_file_path}',
318+
'-map', '2:v', # Map cover image
319+
'-disposition:v', 'attached_pic', # Ensure cover is embedded
320+
'-c:v', 'copy', # Keep cover unchanged
321+
]
317322
else:
318323
cover_image_args = []
319324

320325
proc = subprocess.run([
321326
'ffmpeg',
322-
'-y', # overwrite output file without asking
323-
'-i', f'{concat_file_path}', # input 0 file (audio)
324-
'-i', f'{chapters_txt_path}', # input 1 file (chapters)
325-
*cover_image_args, # cover image
326-
'-map', '0', # map all streams from input 0
327-
'-map_metadata', '1', # map metadata from input 1
328-
'-c:a', 'copy', # copy audio codec
329-
'-c:v', 'copy', # copy video codec
330-
'-disposition:v', 'attached_pic', # attach cover image
331-
'-c', 'copy', # copy codec
332-
'-f', 'mp4', # format
333-
f'{final_filename}' # output file
327+
'-y', # Overwrite output
328+
329+
'-i', f'{concat_file_path}', # Input audio
330+
'-i', f'{chapters_txt_path}', # Input chapters
331+
*cover_image_args, # Cover image (if provided)
332+
333+
'-map', '0:a', # Map audio
334+
'-c:a', 'aac', # Convert to AAC
335+
'-b:a', '64k', # Reduce bitrate for smaller size
336+
337+
'-map_metadata', '1', # Map metadata
338+
339+
'-f', 'mp4', # Output as M4B
340+
f'{final_filename}' # Output file
334341
])
342+
335343
Path(concat_file_path).unlink()
336344
if proc.returncode == 0:
337345
print(f'{final_filename} created. Enjoy your audiobook.')

0 commit comments

Comments
 (0)