user6779573
user6779573

Reputation:

How do I split a M4B file provided I have a CUE file?

I have a large M4B file and a CUE file for it. I want to either split it into many M4B files, or split it into many MP3 files (former preferred).

I want to do this in the command line (OS X, but can use Linux if needed) and not install sketchy software. I have ffmpeg and can install other command line audio programs.

Upvotes: 2

Views: 8373

Answers (3)

user943289
user943289

Reputation: 1

This is a useful bit of code, but needed a couple of alterations to work for me. 'CUEFILE' is not set, but as it invariably has the same basename as the .m4d file, we can use that. I also added a for loop to automatically convert all files in the dir. Works well.

#!/bin/bash

for i in *.m4b;
do
    INFILE=$i ;
    OUTPREFIX=$(basename "$i" .m4b) ;
    OUTEXT=.mp3 ;
    perl -ne 'sub p{printf(qq#"%s" -ss %f -to %f "%s_Chapter_%02d%s"\n#, "'"$INFILE"'", $_[0], $_[1], "'$OUTPREFIX'", $_[2], "'$OUTEXT'")}; if(/^\s+INDEX (\d+) (\d+):(\d+):(\d+)$/){$a=$b;$b=$4/60+$2*60+$3; p($a, $b, ++$c)if$b>0}; END{p($b, $b*100, ++$c)}' $OUTPREFIX.cue | xargs -n 6 ffmpeg -i
done;

Upvotes: 0

NateT
NateT

Reputation: 142

I don't know how strict the CUE sheet format is, but assuming yours follows this general format:

TRACK 1 AUDIO
  TITLE "Chapter 01"
  INDEX 01 0:0:00
TRACK 2 AUDIO
  TITLE "Chapter 02"
  INDEX 01 22:56:01

then you can do this with ffmpeg and a perl one-liner.

user@host:~$ INFILE=input.m4b
user@host:~$ OUTPREFIX=output
user@host:~$ OUTEXT=mp3
user@host:~$ perl -ne 'sub p{printf(qq#"%s" -ss %f -to %f "%s_Chapter_%02d%s"\n#, "'"$INFILE"'", $_[0], $_[1], "'$OUTPREFIX'", $_[2], "'$OUTEXT'")}; if(/^\s+INDEX (\d+) (\d+):(\d+):(\d+)$/){$a=$b;$b=$4/60+$2*60+$3; p($a, $b, ++$c)if$b>0}; END{p($b, $b*100, ++$c)}' CUEFILE.cue | xargs -n 6 ffmpeg -i

The perl one-liner ignores all lines in the cue sheet but the INDEX ... lines and also ignores the first index, assuming it is zero. It prints out arguments for ffmpeg. xargs takes the right number of arguments (6 at a time, in this case) and calls ffmpeg once for each chapter (or track, or whatever).

This method can be used with a cue sheet to split up and convert any audio/video file provided ffmpeg accepts the input and output file formats.

Upvotes: 0

John
John

Reputation: 1196

There is 2-step process which I did and it worked well.

Step 1 - Convert M4B to MP3

For this, you can use VLC Media Player (not sure how to do via command line so here are the GUI instructions).

  • Go to Media->Convert/Save
  • Add the M4B file, click "Save/Convert"
  • Under Profile, choose Audio-MP3
  • Select the destination (output) file
  • Press Start
  • ...wait...but you should see a progress bar

Step 1.5 - Fix missing tags Unfortunately, using VLC to convert the m4b to mp3 will remove all tags. So you might want to use something like puddletag (linux) to add the tags back in.

Step 2 - Split the MP3 using the .cue file

Once you have the MP3, use mp3splt (command-line)

The -c flag allows you to use the cue file for the split points.

Both of these are well worn programs (at least I have used both for years) so I do not think they are considered "sketchy."

Upvotes: 2

Related Questions