Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: M4B to MP3 (chapter separation) (Read 100151 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

M4B to MP3 (chapter separation)

Reply #25
Had some time to figure out the Unicode problem today and it's only an issue on Windows. ffmpeg can't handle Unicode filenames on Windows but does it fine on other platforms. So to fix this I just had to work around it by feeding non-unicode filenames to ffmpeg then rename to the correct one.

Here's the new release (v0.4.2): download
  • Unicode support
  • Run the old version (without mp4v2) with --no-mp4v2. Note that this version can't parse all chapters and Unicode won't work.
  • Customize chapter filenames. (request by daleybox)
To get filenames like "Chapter 5 - Title.mp3" you can call the script like this:
Code: [Select]
python m4b.py --custom-name 'Chapter %(num)d - %(title)s' myfile.m4b

If you're not familiar with python formatting %(num)d will turn into the chapter number and %(title)s the chapter title. You can do more advanced formatting too, e.g. if you want "Chapter 05" instead you can use %(num)02d, and so on.

M4B to MP3 (chapter separation)

Reply #26
Valekhz - I prefer no pre-pending of "Chapter nn" to the beginning of the filename.  If you do prepend filenames with chapter numbers, perhaps there could be an option to enable / disable it?

daleybox - If for some reason valekhz does not implement the feature, or if you do not want to wait, there is a pretty easy workaround for you to try...
1) dump the filenames and creation timestamps of the files output by valekhz's m4b-converter into a text file sorted by time created (in Windows, this can be done with "dir /od > dir.txt" - even if they all display the same time to the minute - the OS knows which were created first to the second or better)
2) open this text file in Excel
3) use the "Concatenate" command in Excel to build a DOS command to do the renaming.  I've done this for Windows, and I'm sure equivalent simple steps exist for other OS's.  For example...
[blockquote]=CONCATENATE("copy """,A6,""" ""Chapter ",C6," - ",A6,"""")
where column A has the old file name, column C has the numbering you want to use
[/blockquote]4) copy this row down for all the files, then copy the whole list of commands into a DOS batch file

With this approach, you can then rename or create copies with the new name for all the files at once.  This approach is easy and flexible, and would prevent valekhz from then getting additional requests to support things like:
a) users with m4b files with a Preface or Forward or both asking to offset the first chapter by 1 or 2 (or more) files?
b) users with an audio book that is split into multiple m4b's with multiple chapters per m4b asking to offset so that the first file starts with Chapter Y?
c) users who prefer chapters in roman numerals asking for "chapter i"  and "Chapter ii" instead of "Chapter 1" and "Chapter 2"
d) users whose audio players sorts / plays the files the order Chapter 1, 10, 11,12.....,19, 2, 20, 21....29, 3, 30, 31 asking to make the chapters two digits wide so they sort in the proper order
e) users who speak language X asking to add options to name files "Kapitel 1" or "Capítula 1" instead of "Chapter 1".
f) users who want the author and/or title added along with or instead the file name / chapter name.

While I prefer that the chapter number is not added to the file name, I can solve that myself.  I'm just trying to think about the future in case there are enough people looking to split M4B files that it really matters.

M4B to MP3 (chapter separation)

Reply #27
valekhz,
I hadn't seen your latest post when I posted.  I will try your new file soon - thank you.  No that I see your post - you can ignore mine.  The Unicode support was a much bigger deal for me.

Thanks again for your work.

M4B to MP3 (chapter separation)

Reply #28
Wow!  I am both impressed and happy.
Not only does it process my files with special characters correctly, but you handled the chapter request very nicely.  I am impressed with all the options of the command-line version.  I also tried it on a file of size 15MB, and it worked there as well.  I do have a couple findings....

1) I used it on several files, all with special characters.  I didn't see any error messages or crashes, or any real problems at all.

2) I notice that sometimes both the Chinese characters and the Pinyin come out, and sometimes only the Pinyin.  Is that due to the construction of the original m4b or something about the way m4b.py is reading it?  Is there anything that can be done to get the Chinese characters all the time?

3) The question mark ("?") is not allowed as characters in file names, and the period "." is the delimiter between the base filename and the extension.  I notice that "?"'s are removed, and sentences have the "."  However, the characters used in Chinese writing are allowed as file names "?" and "。".  This is not too important, but as a low-priority request - is there any chance to make use these characters when they appear in the chapter name rather than removing the "?" and having double "normal periods" in some file names?  This might best be an option, as these characters may appear unusual to some users.

4) I notice that the special characters are not displayed properly by the "dir" command in Windows DOS command window, although they are displayed correctly in Windows Explorer.  A note to other users: If anyone wants a text file with the filenames (which I use for importing these files into another program), one can browse the directory using Firefox, the copy-paste the file name list to a text file, remove the extra fields (size/timestamp/etc) to get just a list of files.

Thanks again for your great work.

M4B to MP3 (chapter separation)

Reply #29
Thanks so much for the chapter naming support, and it's great how it can be customized so others don't have to have it if they don't want!  Thanks again so much, this is great!

M4B to MP3 (chapter separation)

Reply #30

2. m4b.py just reads the chapters as constructed by the author so there's nothing the script could do differently to get the chinese characters.

3. "." is valid in filenames and those aren't filtered out. I won't add the question mark thing to the script because it's too specific for this particular case. However, it's really easy to make this change yourself: m4b.py Changes were made to line 2, 129 and 139. The actual replacement happens at line 139: .replace('?', '?') You can add more replace if you want to change more characters.

4. I have tried many times to get that to work but no success. Your firefox solution is good enough.

M4B to MP3 (chapter separation)

Reply #31
Valekhz,

2. Ahh.  I suspected that might be the case.  I'm still happy that the majority of the chapters can be parsed.

3. I can understand the the question mark thing is too much of a special case.  And yes - I was able to get my special case to work, thanks to the guidance you provided.

Finally, if you don't mind - one last question:  Several of these files created by the m4B.py have the word / sentence that I want to hear, then 3-4 seconds of silence, then the same word / sentence again.  (same as original m4b chapter)  I actually only want the mp3 to contain the word / phrase once.  Might you be able to make another .py file that takes an mp3 file in, measures the duration of the file, and simply saves only the 1st half under the same name (deleting 2nd half)?  (FYI - I will then run this on all files in a given directory, and then clean up silences and the like with Audacity "Apply Chains" - but audacity doesn't seem to have batch splitting)

Sorry to ask you for something else after all you have done, but you seem to have the skills to do this 1000 times easier than I ever could.

Thank you

M4B to MP3 (chapter separation)

Reply #32
Valekhz,

2. Ahh.  I suspected that might be the case.  I'm still happy that the majority of the chapters can be parsed.

3. I can understand the the question mark thing is too much of a special case.  And yes - I was able to get my special case to work, thanks to the guidance you provided.

Finally, if you don't mind - one last question:  Several of these files created by the m4B.py have the word / sentence that I want to hear, then 3-4 seconds of silence, then the same word / sentence again.  (same as original m4b chapter)  I actually only want the mp3 to contain the word / phrase once.  Might you be able to make another .py file that takes an mp3 file in, measures the duration of the file, and simply saves only the 1st half under the same name (deleting 2nd half)?  (FYI - I will then run this on all files in a given directory, and then clean up silences and the like with Audacity "Apply Chains" - but audacity doesn't seem to have batch splitting)

Sorry to ask you for something else after all you have done, but you seem to have the skills to do this 1000 times easier than I ever could.

Thank you

No problem. I made an addition to line 19: m4b_half.py. This will save the first half of each chapter so if you don't want to cut all chapters you should run the old version first and save the ones that shouldn't be cut then run this one.

M4B to MP3 (chapter separation)

Reply #33
Valekhz,

I just wanted to say thank you one last time.  I have been using m4b.py and m4b_half.py for a few days, and found that it really solves the hardest part of the problem I had been having.  You wrote it to be flexible enough to solve the majority of my problems, and even allow me to make some minor tweaks on my own (I'm also replacing ":" with -" to be Windows/Dos compatible and "[" with "(" to be Anki compatible).

Thank you so much for your efforts.

M4B to MP3 (chapter separation)

Reply #34
No problem, I'm glad it worked.

I needed a few more features for myself so I'll release them as v0.5. Changes:
  • More advanced encoding options
  • Drop multiple m4b files onto m4b.py
I also cleaned up and simplified the code which means m4b_half and those modifications will need some changes. But you can safely skip this one if you already have the previous version and don't need the new features. The only thing left on my TODO is making the script work with more python versions than 2.7 but this has a low priority.

M4B to MP3 (chapter separation)

Reply #35
....  The script works well, however, when it extracts the chapters and names to mp3, I have no idea of what order they should have be in.  Could there be an internal counter or something so that you would get something like "Chapter 1 - " prepended to the chapter title?  Otherwise, the only way I see to figure out the order is to listen to each one and hope it starts with the chapter number...


  You should try sorting the mp3 files listing by time and date created; the oldest file should be the first chapter, no?  Just seems like that would work for you, then a rename batch could append the chapter index to the filename.
Brian2090

  *----======  "Are you sure that's a candle?"

M4B to MP3 (chapter separation)

Reply #36
If anyone is still interested I wrote a little python script to do this. You can find the git repository here: https://github.com/valekhz/m4b-converter. If you don't use git you can just download the python file here: https://github.com/valekhz/m4b-converter/raw/master/m4b.py.

You'll need python (only tested with 2.7 so far) and ffmpeg. Place the m4b.py somewhere and drag your *.m4b file onto m4b.py, or use the command line. There's a README in the git repository for more info on how to use it. I haven't been able to test that many .m4b files yet but those I've tried have worked great. I've tested it on Windows 7 and Ubuntu.

Basically it works by parsing the output of ffmpeg -i file.m4b to get the chapter data, encodes the audio and splits it based on the data from ffmpeg. Sorry if the code is a bit messy, haven't been coding in python for a while.



Huh?

this is my log: access denied (its german here "zugriff verweigert")

D:\py>m4b.py -o ./doc --ffmpeg ./bin --encoder ./bin --ext mp3 --no-mp4v2 test.m
4b --debug
INFO: m4bsplit started.
DEBUG: Options:
    ffmpeg: ./bin
    custom_name: %(title)s
    skip_encoding: False
    filename: ['test.m4b']
    encoder: ./bin
    ext: mp3
    encode_opts: -y -i %(infile)s -acodec libmp3lame -ar %(sample_rate)d -ab %(b
it_rate)dk %(outfile)s
    output_dir: ./doc
    debug: True
    pipe_wav: False
    no_mp4v2: True
INFO: Initiating script for file 'test.m4b'.
INFO: Loading metadata using ffmpeg...
DEBUG: Retrieving metadata from output of command: ./bin -i test.m4b
Traceback (most recent call last):
  File "D:\py\m4b.py", line 337, in <module>
    main()
  File "D:\py\m4b.py", line 328, in main
    chapters, sample_rate, bit_rate, metadata = load_metadata(args, log, filenam
e)
  File "D:\py\m4b.py", line 205, in load_metadata
    return ffmpeg_metadata(args, log, filename)
  File "D:\py\m4b.py", line 143, in ffmpeg_metadata
    ignore_errors=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  File "D:\py\m4b.py", line 44, in run_command
    proc = subprocess.Popen(cmd, **kwargs)
  File "C:\Python32\lib\subprocess.py", line 741, in __init__
    restore_signals, start_new_session)
  File "C:\Python32\lib\subprocess.py", line 960, in _execute_child
    startupinfo)
WindowsError: [Error 5] Zugriff verweigert

D:\py>


this is the output without the no v2 parameter:

D:\py>m4b.py -o ./doc --ffmpeg ./bin --encoder ./bin --ext mp3 test.m4b --debug
INFO: m4bsplit started.
DEBUG: Options:
    ffmpeg: ./bin
    custom_name: %(title)s
    skip_encoding: False
    filename: ['test.m4b']
    encoder: ./bin
    ext: mp3
    encode_opts: -y -i %(infile)s -acodec libmp3lame -ar %(sample_rate)d -ab %(b
it_rate)dk %(outfile)s
    output_dir: ./doc
    debug: True
    pipe_wav: False
    no_mp4v2: False
INFO: Initiating script for file 'test.m4b'.
INFO: Loading metadata using libmp4v2...
Traceback (most recent call last):
  File "D:\py\m4b.py", line 337, in <module>
    main()
  File "D:\py\m4b.py", line 328, in main
    chapters, sample_rate, bit_rate, metadata = load_metadata(args, log, filenam
e)
  File "D:\py\m4b.py", line 208, in load_metadata
    return mp4v2_metadata(filename)
  File "D:\py\m4b.py", line 188, in mp4v2_metadata
    from libmp4v2 import MP4File
ImportError: No module named libmp4v2

D:\py>

M4B to MP3 (chapter separation)

Reply #37
fixed 1 problem in line 44:

    proc = subprocess.Popen("runas /user:Admin cmd.exe", **kwargs)

as admin i come little bit closer...
but then ist stops at:

INFO: Loading metadata using ffmpeg...
DEBUG: Retrieving metadata from output of command: ./bin -i test.m4b

M4B to MP3 (chapter separation)

Reply #38
thanks to valekhz for creating this script

M4B to MP3 (chapter separation)

Reply #39
First off, many thanks to valekhz for building this tool. There is only one other program out there that I found (Bigasoft Audio Converter), but it doesn't have command-line support and so this tool still reigns supreme.

In hopes that valekhz is still open to tweaking the tool, I ran into one problem. For some reason, the tool crashes on The Economist podcasts with this message:
Traceback (most recent call last):
  File "C:\Programs\Python27\m4b.py", line 337, in <module>
    main()
  File "C:\Programs\Python27\m4b.py", line 334, in main
    split(args, log, output_dir, encoded_file, chapters)
  File "C:\Programs\Python27\m4b.py", line 290, in split
    chapter_name = unicode(chapter_name, 'utf-8')
UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0: invalid start byte

Here is an example file I tried to split: http://dl.dropbox.com/u/19007038/13%20Inte...C%20The%20E.m4a
(I know it's an m4a extension, but the same issue happens on m4b's)

Thanks to valekhz or anyone else who can help with this!

 

M4B to MP3 (chapter separation)

Reply #40
As with everyone who's posted, major props to valekhz for building and maintaining this.

I got this working on Mac (Snow Leopard 10.6) with some fiddling:
  • OS 10.6 comes with python 2.6 pre-installed, I had to upgrade python to 2.7.x
  • I used macports to install all the requirements
  • The check for 'darwin' compatibility was removed from libmp4v2.py sometime between v0.2 and v0.5, I added it back to get around the "NotImplementedError: O/S 'darwin' not supported":
    Code: [Select]
    elif sys.platform.startswith('darwin'):
        dll = ctypes.CDLL('libmp4v2.dylib')
  • LAME 3.98.2, the default version with macports was broken, I got "lame: output buffer too small"  I had to downgrade to v3.98.0
  • I needed to manually add the mp4v2 location to DYLD_LIBRARY_PATH


Which all sums up to something like this:
  • Download Python 2.7.2 from python.org and install it
  • Add the code snippet, above, at line 16 to libmp4v2.py


...and then, open a fresh Terminal window (to pick up the new Python version that the Python installer adds to your path), and

Code: [Select]
sudo port install ffmpeg
sudo port install mp4v2
export DYLD_LIBRARY_PATH=/opt/local/lib
cd /tmp
curl -O http://trac.macports.org/export/38059/trunk/dports/audio/lame/Portfile
sudo port install


Then cd back to where you downloaded and unzipped valekhz-m4b-converter and do the expected

Code: [Select]
python m4b.py filename.m4a


Voila.  You can add the export DYLD_LIBRARY_PATH=/opt/local/lib into your ~/.bash_profile for future convenience.  Possibly you don't need to do all of this in OS 10.7 but I'm dragging my feet on the upgrade.

Thanks again, valekhz!


M4B to MP3 (chapter separation)

Reply #41
Absolutely brilliant!!!!  Thanks for all the hard work valekhz.

M4B to MP3 (chapter separation)

Reply #42
I tried all different versions but still it makes just one mp3 file
Can someone help me out?
i can see that it reads the chapter names but it wont make multiple files

Code: [Select]
C:\Python27>python m4b.py --debug game.m4b
INFO: m4bsplit started.
DEBUG: Options:
ffmpeg: ffmpeg
custom_name: %(title)s
skip_encoding: False
filename: ['game.m4b']
encoder: ffmpeg
ext: mp3
encode_opts: -y -i %(infile)s -acodec libmp3lame -ar %(sample_rate)d -ab %(b
it_rate)dk %(outfile)s
output_dir:
debug: True
pipe_wav: False
no_mp4v2: False
INFO: Initiating script for file 'game.m4b'.
INFO: Loading metadata using libmp4v2...
INFO:
Metadata:
  Chapters: 135
  Bit rate: 64 kbit/s
  Sampling freq: 22050 Hz
DEBUG:
  Chapter data:
  <Chapter Title="00 - Prologue 1", Start=0:00:00, End=0:10:00, Dura
tion=0:10:00>
<Chapter Title="Part 1.1", Start=0:10:00, End=0:20:00, Duration=0:10:00>
<Chapter Title="Part 1.2", Start=0:20:00, End=0:24:33.005000, Duration=0:04:33.0
05000>
-----------------------
-----------------------
<Chapter Title="Part 38.3", Start=16:09:59.963000, End=16:17:54.605000, Duration
=0:07:54.642000>
INFO: Encoding audio...
DEBUG: Encoding with command: ffmpeg -y -i game.m4b -acodec libmp3lame -ar 22050
 -ab 64k game\temp\game.mp3
ffmpeg version N-41416-g718607b Copyright © 2000-2012 the FFmpeg developers
  built on Jun  8 2012 12:46:19 with gcc 4.6.3
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-ru
ntime-cpudetect --enable-avisynth --enable-bzlib --enable-frei0r --enable-libass
 --enable-libcelt --enable-libopencore-amrnb --enable-libopencore-amrwb --enable
-libfreetype --enable-libgsm --enable-libmp3lame --enable-libnut --enable-libope
njpeg --enable-librtmp --enable-libschroedinger --enable-libspeex --enable-libth
eora --enable-libutvideo --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-
libvorbis --enable-libvpx --ena  libavutil   51. 56.100 / 51. 56.100
  libavcodec 54. 25.100 / 54. 25.100
  libavformat 54.  6.101 / 54.  6.101
  libavdevice 54.  0.100 / 54.  0.100
  libavfilter 2. 78.101 /  2. 78.101
  libswscale   2.  1.100 /  2.  1.100
  libswresample  0. 15.100 /  0. 15.100
  libpostproc 52.  0.100 / 52.  0.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'game.m4b':
  Metadata:
major_brand : M4A
minor_version  : 0
compatible_brands: 3gp5isom
creation_time  : 2011-04-30 13:52:34
genre   : Audiobook
media_type   : 2
encoder : Chapter and Verse V 1.4
title   : 01 - A Game of Thrones - Part 1
artist   : George R.R. Martin
album   : A Game of Thrones
composer : George R.R. Martin
track   : 1/2
  Duration: 16:17:54.64, start: 0.000000, bitrate: 60 kb/s
Chapter #0.0: start 0.000000, end 600.000000
Metadata:
  title   : 00 - Prologue 1
Chapter #0.1: start 600.000000, end 1200.000000
Metadata:
  title   : Part 1.1
-----------------------------
-------------------------------
Metadata:
  title   : Part 38.1
Chapter #0.133: start 57600.000000, end 58200.000000
Metadata:
  title   : Part 38.2
Chapter #0.134: start 58200.000000, end 58674.642721
Metadata:
  title   : Part 38.3
Stream #0:0(und): Audio: aac (mp4a / 0x6134706D), 22050 Hz, stereo, s16, 59
kb/s
Metadata:
  creation_time  : 2011-04-30 13:52:34
Stream #0:1(und): Subtitle: mov_text (text / 0x74786574)
Metadata:
  creation_time  : 2011-04-30 13:54:41
Output #0, mp3, to 'game\temp\game.mp3':
  Metadata:
major_brand : M4A
minor_version  : 0
compatible_brands: 3gp5isom
TDEN : 2011-04-30 13:52:34
TCON : Audiobook
media_type   : 2
TRCK : 1/2
TIT2 : 01 - A Game of Thrones - Part 1
TPE1 : George R.R. Martin
TALB : A Game of Thrones
TCOM : George R.R. Martin
TSSE : Lavf54.6.101
Chapter #0.0: start 0.000000, end 600.000000
Metadata:
  title   : 00 - Prologue 1
Chapter #0.1: start 600.000000, end 1200.000000
Metadata:
  title   : Part 1.1
Chapter #0.2: start 1200.000000, end 1473.005442
Metadata:
  title   : Part 1.2
Chapter #0.3: start 1473.005442, end 1800.000000
  ------------------
-------------------
Metadata:
  title   : Part 38.2
Chapter #0.134: start 58200.000000, end 58674.642721
Metadata:
  title   : Part 38.3
Stream #0:0(und): Audio: mp3, 22050 Hz, stereo, s16, 64 kb/s
Metadata:
  creation_time  : 2011-04-30 13:52:34
Stream mapping:
  Stream #0:0 -> #0:0 (aac -> libmp3lame)
Press [q] to stop, [?] for help
size=  458397kB time=16:17:54.65 bitrate=  64.0kbits/s
video:0kB audio:458396kB global headers:0kB muxing overhead 0.000119%
INFO: Splitting chapter  1/135 '00 - Prologue 1'...
DEBUG: Splitting with command: ffmpeg -y -acodec copy -t 600.0 -ss 0.0 -i game\t
emp\game.mp3 game\_tmp_1.mp3
ERROR:
An error occurred while splitting audio file.
  Command: ffmpeg -y -acodec copy -t 600.0 -ss 0.0 -i game\temp\game.mp3 game\_t
mp_1.mp3
  Return code: 1
  Output: ---->
ffmpeg version N-41416-g718607b Copyright © 2000-2012 the FFmpeg developers
  built on Jun  8 2012 12:46:19 with gcc 4.6.3
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-ru
ntime-cpudetect --enable-avisynth --enable-bzlib --enable-frei0r --enable-libass
 --enable-libcelt --enable-libopencore-amrnb --enable-libopencore-amrwb --enable
-libfreetype --enable-libgsm --enable-libmp3lame --enable-libnut --enable-libope
njpeg --enable-librtmp --enable-libschroedinger --enable-libspeex --enable-libth
eora --enable-libutvideo --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-
libvorbis --enable-libvpx --ena  libavutil   51. 56.100 / 51. 56.100
  libavcodec 54. 25.100 / 54. 25.100
  libavformat 54.  6.101 / 54.  6.101
  libavdevice 54.  0.100 / 54.  0.100
  libavfilter 2. 78.101 /  2. 78.101
  libswscale   2.  1.100 /  2.  1.100
  libswresample  0. 15.100 /  0. 15.100
  libpostproc 52.  0.100 / 52.  0.100
Unknown decoder 'copy'

M4B to MP3 (chapter separation)

Reply #43
Many thanks to valekhz! Note for customers. Valekhz's script is working fine. If smtg not work follow instruction from begin or see run options.

On Ubuntu 12.04 you just run in terminal in any folder:

sudo apt-get install python2.7 ffmpeg libavcodec-extra-53 mp4v2-utils libmp4v2-dev libmp4v2-2 git
git clone https://github.com/valekhz/m4b-converter.git
cd ./m4b-converter
python m4b.py --no-mp4v2 PATH_TO_YOUR_M4B_FILE_ABSOLUTE_OR_RELATIVE_FROM_NEW_DIR_m4b-converter


First off, many thanks to valekhz for building this tool. There is only one other program out there that I found (bigsoftasses Audio Converter), but it doesn't have command-line support and so this tool still reigns supreme.

In hopes that valekhz is still open to tweaking the tool, I ran into one problem. For some reason, the tool crashes on The Economist podcasts with this message:
Traceback (most recent call last):
  File "C:\Programs\Python27\m4b.py", line 337, in <module>
    main()
  File "C:\Programs\Python27\m4b.py", line 334, in main
    split(args, log, output_dir, encoded_file, chapters)
  File "C:\Programs\Python27\m4b.py", line 290, in split
    chapter_name = unicode(chapter_name, 'utf-8')
UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0: invalid start byte

Here is an example file I tried to split: http://dl.dropbox.com/u/19007038/13%20Inte...C%20The%20E.m4a
(I know it's an m4a extension, but the same issue happens on m4b's)

Thanks to valekhz or anyone else who can help with this!


--no-mp4v2 works for me


M4B to MP3 (chapter separation)

Reply #45
As with everyone who's posted, major props to valekhz for building and maintaining this.

I got this working on Mac (Snow Leopard 10.6) with some fiddling:


Thank you!
(especially that you registered here just for sharing this useful extra information for OS X users)
Not really a Signature.

M4B to MP3 (chapter separation)

Reply #46
If this is still relevant, I found an easier way.
(The script works great though)
This is much more simpler.
1. Get bigsoftasses Audio Converter from here: http://www.bigsoftasses.com/audio-converter.html
    Or the full version for free from here (Windows only): http://adf.ly/CQezy
* To install it, run 'Software' and follow the onscreen instructions.
  Run the program.
  Register with any name, and key from 'Serial.txt'.
2. Follow this article: http://www.bigsoftasses.com/articles/convert-m...-converter.html

I do not encourage piracy, and please support the developers by buying the product, like I have.
Please use it for testing purposes only, and uninstall it when you're done.

M4B to MP3 (chapter separation)

Reply #47
I tried all different versions but still it makes just one mp3 file
Can someone help me out?
i can see that it reads the chapter names but it wont make multiple files

Code: [Select]
C:\Python27>python m4b.py --debug game.m4b
INFO: m4bsplit started.
DEBUG: Options:
ffmpeg: ffmpeg
custom_name: %(title)s
skip_encoding: False
filename: ['game.m4b']
encoder: ffmpeg
ext: mp3
encode_opts: -y -i %(infile)s -acodec libmp3lame -ar %(sample_rate)d -ab %(b
it_rate)dk %(outfile)s
output_dir:
debug: True
pipe_wav: False
no_mp4v2: False
INFO: Initiating script for file 'game.m4b'.
INFO: Loading metadata using libmp4v2...
INFO:
Metadata:
  Chapters: 135
  Bit rate: 64 kbit/s
  Sampling freq: 22050 Hz
DEBUG:
  Chapter data:
  <Chapter Title="00 - Prologue 1", Start=0:00:00, End=0:10:00, Dura
tion=0:10:00>
<Chapter Title="Part 1.1", Start=0:10:00, End=0:20:00, Duration=0:10:00>
<Chapter Title="Part 1.2", Start=0:20:00, End=0:24:33.005000, Duration=0:04:33.0
05000>
-----------------------
-----------------------
<Chapter Title="Part 38.3", Start=16:09:59.963000, End=16:17:54.605000, Duration
=0:07:54.642000>
INFO: Encoding audio...
DEBUG: Encoding with command: ffmpeg -y -i game.m4b -acodec libmp3lame -ar 22050
 -ab 64k game\temp\game.mp3
ffmpeg version N-41416-g718607b Copyright © 2000-2012 the FFmpeg developers
  built on Jun  8 2012 12:46:19 with gcc 4.6.3
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-ru
ntime-cpudetect --enable-avisynth --enable-bzlib --enable-frei0r --enable-libass
 --enable-libcelt --enable-libopencore-amrnb --enable-libopencore-amrwb --enable
-libfreetype --enable-libgsm --enable-libmp3lame --enable-libnut --enable-libope
njpeg --enable-librtmp --enable-libschroedinger --enable-libspeex --enable-libth
eora --enable-libutvideo --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-
libvorbis --enable-libvpx --ena  libavutil   51. 56.100 / 51. 56.100
  libavcodec 54. 25.100 / 54. 25.100
  libavformat 54.  6.101 / 54.  6.101
  libavdevice 54.  0.100 / 54.  0.100
  libavfilter 2. 78.101 /  2. 78.101
  libswscale   2.  1.100 /  2.  1.100
  libswresample  0. 15.100 /  0. 15.100
  libpostproc 52.  0.100 / 52.  0.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'game.m4b':
  Metadata:
major_brand : M4A
minor_version  : 0
compatible_brands: 3gp5isom
creation_time  : 2011-04-30 13:52:34
genre   : Audiobook
media_type   : 2
encoder : Chapter and Verse V 1.4
title   : 01 - A Game of Thrones - Part 1
artist   : George R.R. Martin
album   : A Game of Thrones
composer : George R.R. Martin
track   : 1/2
  Duration: 16:17:54.64, start: 0.000000, bitrate: 60 kb/s
Chapter #0.0: start 0.000000, end 600.000000
Metadata:
  title   : 00 - Prologue 1
Chapter #0.1: start 600.000000, end 1200.000000
Metadata:
  title   : Part 1.1
-----------------------------
-------------------------------
Metadata:
  title   : Part 38.1
Chapter #0.133: start 57600.000000, end 58200.000000
Metadata:
  title   : Part 38.2
Chapter #0.134: start 58200.000000, end 58674.642721
Metadata:
  title   : Part 38.3
Stream #0:0(und): Audio: aac (mp4a / 0x6134706D), 22050 Hz, stereo, s16, 59
kb/s
Metadata:
  creation_time  : 2011-04-30 13:52:34
Stream #0:1(und): Subtitle: mov_text (text / 0x74786574)
Metadata:
  creation_time  : 2011-04-30 13:54:41
Output #0, mp3, to 'game\temp\game.mp3':
  Metadata:
major_brand : M4A
minor_version  : 0
compatible_brands: 3gp5isom
TDEN : 2011-04-30 13:52:34
TCON : Audiobook
media_type   : 2
TRCK : 1/2
TIT2 : 01 - A Game of Thrones - Part 1
TPE1 : George R.R. Martin
TALB : A Game of Thrones
TCOM : George R.R. Martin
TSSE : Lavf54.6.101
Chapter #0.0: start 0.000000, end 600.000000
Metadata:
  title   : 00 - Prologue 1
Chapter #0.1: start 600.000000, end 1200.000000
Metadata:
  title   : Part 1.1
Chapter #0.2: start 1200.000000, end 1473.005442
Metadata:
  title   : Part 1.2
Chapter #0.3: start 1473.005442, end 1800.000000
  ------------------
-------------------
Metadata:
  title   : Part 38.2
Chapter #0.134: start 58200.000000, end 58674.642721
Metadata:
  title   : Part 38.3
Stream #0:0(und): Audio: mp3, 22050 Hz, stereo, s16, 64 kb/s
Metadata:
  creation_time  : 2011-04-30 13:52:34
Stream mapping:
  Stream #0:0 -> #0:0 (aac -> libmp3lame)
Press [q] to stop, [?] for help
size=  458397kB time=16:17:54.65 bitrate=  64.0kbits/s
video:0kB audio:458396kB global headers:0kB muxing overhead 0.000119%
INFO: Splitting chapter  1/135 '00 - Prologue 1'...
DEBUG: Splitting with command: ffmpeg -y -acodec copy -t 600.0 -ss 0.0 -i game\t
emp\game.mp3 game\_tmp_1.mp3
ERROR:
An error occurred while splitting audio file.
  Command: ffmpeg -y -acodec copy -t 600.0 -ss 0.0 -i game\temp\game.mp3 game\_t
mp_1.mp3
  Return code: 1
  Output: ---->
ffmpeg version N-41416-g718607b Copyright © 2000-2012 the FFmpeg developers
  built on Jun  8 2012 12:46:19 with gcc 4.6.3
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-ru
ntime-cpudetect --enable-avisynth --enable-bzlib --enable-frei0r --enable-libass
 --enable-libcelt --enable-libopencore-amrnb --enable-libopencore-amrwb --enable
-libfreetype --enable-libgsm --enable-libmp3lame --enable-libnut --enable-libope
njpeg --enable-librtmp --enable-libschroedinger --enable-libspeex --enable-libth
eora --enable-libutvideo --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-
libvorbis --enable-libvpx --ena  libavutil   51. 56.100 / 51. 56.100
  libavcodec 54. 25.100 / 54. 25.100
  libavformat 54.  6.101 / 54.  6.101
  libavdevice 54.  0.100 / 54.  0.100
  libavfilter 2. 78.101 /  2. 78.101
  libswscale   2.  1.100 /  2.  1.100
  libswresample  0. 15.100 /  0. 15.100
  libpostproc 52.  0.100 / 52.  0.100
Unknown decoder 'copy'


I know this was a year ago, but I had the same issue, and tracked down the cause, so to document it for anyone else who comes along later like me:

The problem is that the ffmpeg folks changed how they parse the command line somewhere around early November of 2011, so if your version of ffmpeg is too recent, it won't work with the ffmpeg commands generated by this script. See here: https://bugs.archlinux.org/task/27647 for more details.

So your options are to "downgrade" ffmpeg, or update the script to rearrange the ffmpeg commands.

M4B to MP3 (chapter separation)

Reply #48
I know this was a year ago, but I had the same issue, and tracked down the cause, so to document it for anyone else who comes along later like me:

The problem is that the ffmpeg folks changed how they parse the command line somewhere around early November of 2011, so if your version of ffmpeg is too recent, it won't work with the ffmpeg commands generated by this script. See here: https://bugs.archlinux.org/task/27647 for more details.

So your options are to "downgrade" ffmpeg, or update the script to rearrange the ffmpeg commands.


(Another 18 months later) Thanks for that @nmallears.  I ran into the same issue using the most current build of ffmpeg.  Since I wanted to use the latest ffmpeg build, I dug in and determined the solution is pretty easy.  The command line change is simple.

In m4b.py, in the function split, change the assignment for the split_cmd variable from
Code: [Select]
split_cmd = '%(ffmpeg)s -y -acodec copy -t %(duration)s -ss %(start)s -i %(outfile)s %(infile)s'

to
Code: [Select]
split_cmd = '%(ffmpeg)s -y -t %(duration)s -ss %(start)s -i %(outfile)s -acodec copy %(infile)s'

The codec command line option needs to be moved to be relevant for the input file in our case.    The changes to the ffmpeg command line parser now include enforcement that parameters pertaining to the input file must follow the input filename on the command line.  Likewise, commands pertaining to the output file (none in this case) must follow it as well.
I moved the switches for -acodec copy farther down the parameter list.  m4b.py Works correctly now with ffmpeg version N-67227-g39680ca Built Oct 27th, 2014 and Python 2.7.8  By the way, don't try Python 3.x as more work would have to be done to get it to run.

Props go out to valekhz for coming up with this slick solution.  Sure beats paying 25 USD for a commercial app that likely uses ffmpeg anyway