@@ -313,25 +313,33 @@ def create_m4b(chapter_files, filename, cover_image, output_folder):
313
313
cover_file_path = Path (output_folder ) / 'cover'
314
314
with open (cover_file_path , 'wb' ) as f :
315
315
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
+ ]
317
322
else :
318
323
cover_image_args = []
319
324
320
325
proc = subprocess .run ([
321
326
'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
334
341
])
342
+
335
343
Path (concat_file_path ).unlink ()
336
344
if proc .returncode == 0 :
337
345
print (f'{ final_filename } created. Enjoy your audiobook.' )
0 commit comments