Register Guidelines E-Books Search Today's Posts Mark Forums Read

Go Back   MobileRead Forums > E-Book Formats > Kindle Formats

Notices

Reply
 
Thread Tools Search this Thread
Old 06-28-2013, 12:00 AM   #1
Blunaigel
Junior Member
Blunaigel began at the beginning.
 
Posts: 2
Karma: 10
Join Date: Jun 2013
Device: Kindle Paperwhite
Batch processing with kindlegen

Hi all,

how can I convert multiple epub files in a batch using kindlegen?


... well yeah the question is as simple as this.
Blunaigel is offline   Reply With Quote
Old 06-28-2013, 02:42 AM   #2
Doitsu
Wizard
Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.
 
Doitsu's Avatar
 
Posts: 1,992
Karma: 4633978
Join Date: Dec 2010
Device: Kindle PW2
Quote:
Originally Posted by Blunaigel View Post
how can I convert multiple epub files in a batch using kindlegen?
Create a text file with the following one liner in it and save it with the extension .CMD or .BAT:

Code:
FOR %%f IN ("*.epub") DO ( kindlegen "%%f" >> conversion.log )
Then copy the batch file and Kindlegen.exe to the folder with your epubs in it and double-click the batch file. If files fail to convert check the error messages in conversion.log.

Note that KindleGen will attach the source files to the .mobi files. If space is an issue, you'll need to use KindleStrip to strip off the source files.

EDIT: You can also use the undocumented -dont_append_source parameter to prevent kindlegen from attaching the source files to the .mobi file:

Code:
FOR %%f IN ("*.epub") DO ( kindlegen -dont_append_source "%%f" >> conversion.log )

Last edited by Doitsu; 04-25-2014 at 06:20 PM. Reason: Added: -dont_append_source information
Doitsu is offline   Reply With Quote
Old 06-29-2013, 01:15 AM   #3
Blunaigel
Junior Member
Blunaigel began at the beginning.
 
Posts: 2
Karma: 10
Join Date: Jun 2013
Device: Kindle Paperwhite
Thank you very much!

Can I include a command to tell kindlegen to use the "-c2" format?

(Sorry, I'm not really used to .bat-files. :/ )


EDIT: oh and how about batch-processing with kindlestrip?
Blunaigel is offline   Reply With Quote
Old 06-30-2013, 04:17 AM   #4
HarryT
eBook Enthusiast
HarryT ought to be getting tired of karma fortunes by now.HarryT ought to be getting tired of karma fortunes by now.HarryT ought to be getting tired of karma fortunes by now.HarryT ought to be getting tired of karma fortunes by now.HarryT ought to be getting tired of karma fortunes by now.HarryT ought to be getting tired of karma fortunes by now.HarryT ought to be getting tired of karma fortunes by now.HarryT ought to be getting tired of karma fortunes by now.HarryT ought to be getting tired of karma fortunes by now.HarryT ought to be getting tired of karma fortunes by now.HarryT ought to be getting tired of karma fortunes by now.
 
HarryT's Avatar
 
Posts: 64,009
Karma: 42472847
Join Date: Nov 2006
Location: UK
Device: PW2, iPad Retina Mini, iPhone 4, MS Surface Pro, Kobo H2O, N7
You can include any flags you want on the command line.
HarryT is offline   Reply With Quote
Old 06-30-2013, 01:05 PM   #5
Doitsu
Wizard
Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.
 
Doitsu's Avatar
 
Posts: 1,992
Karma: 4633978
Join Date: Dec 2010
Device: Kindle PW2
Quote:
Originally Posted by Blunaigel View Post

Can I include a command to tell kindlegen to use the "-c2" format?
As HarryT has already correctly pointed out, you can simply add the -c2 switch after Kindlegen, however, since Kindlegen will run several optimization passes for each file, I wouldn't recommend using the -c2 switch in batch files.

Quote:
Originally Posted by Blunaigel View Post
EDIT: oh and how about batch-processing with kindlestrip?
Assuming that you have ActivePython (or another Python 2.7 build) installed and Kindlestrip.py is the same folder as the batch file, you could use the following very simple batch file:

Code:
DEL conversion.log
FOR %%f IN ("*.epub") DO  (
	kindlegen "%%f" >> conversion.log
	IF EXIST "%%~nf.mobi" (
		ECHO ******************************* >> conversion.log
		kindlestrip "%%~nf.mobi" "%%~nf.new" >> conversion.log
		IF EXIST "%%~nf.new" (	
			DEL "%%~nf.mobi"
			REN "%%~nf.new" "%%~nf.mobi"
		)
	) 
)
The batch file works with KindleGen 2.9 and Kindlestrip 1.35.
Doitsu is offline   Reply With Quote
Old 06-30-2013, 03:52 PM   #6
RbnJrg
Evangelist
RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.
 
Posts: 461
Karma: 318996
Join Date: Mar 2013
Location: Rosario - Santa Fe - Argentina
Device: Kindle 4 NT
Quote:
Originally Posted by Doitsu View Post
As HarryT has already correctly pointed out, you can simply add the -c2 switch after Kindlegen, however, since Kindlegen will run several optimization passes for each file, I wouldn't recommend using the -c2 switch in batch files.



Assuming that you have ActivePython (or another Python 2.7 build) installed and Kindlestrip.py is the same folder as the batch file, you could use the following very simple batch file:

Code:
DEL conversion.log
FOR %%f IN ("*.epub") DO  (
	kindlegen "%%f" >> conversion.log
	IF EXIST "%%~nf.mobi" (
		ECHO ******************************* >> conversion.log
		kindlestrip "%%~nf.mobi" "%%~nf.new" >> conversion.log
		IF EXIST "%%~nf.new" (	
			DEL "%%~nf.mobi"
			REN "%%~nf.new" "%%~nf.mobi"
		)
	) 
)
The batch file works with KindleGen 2.9 and Kindlestrip 1.35.
Very nice script! And if you have a folder only with .mobi books and want to apply kindlestrip, how would it be the .bat file? Many thanks in advance.
RbnJrg is offline   Reply With Quote
Old 06-30-2013, 04:29 PM   #7
Doitsu
Wizard
Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.
 
Doitsu's Avatar
 
Posts: 1,992
Karma: 4633978
Join Date: Dec 2010
Device: Kindle PW2
Quote:
Originally Posted by RbnJrg View Post
Very nice script! And if you have a folder only with .mobi books and want to apply kindlestrip, how would it be the .bat file? Many thanks in advance.
That's very easy. You simply omit the KindleGen part, change the file selector to .mobi and end up with this very simple script.

Code:
DEL conversion.log
FOR %%f IN ("*.mobi") DO  (
		ECHO ******************************* >> conversion.log
		kindlestrip "%%~nf.mobi" "%%~nf.new" >> conversion.log
		IF EXIST "%%~nf.new" (	
			DEL "%%~nf.mobi"
			REN "%%~nf.new" "%%~nf.mobi"
		) ELSE ( 
		ECHO  "%%~nf.mobi" was NOT stripped! >> conversion.log
		)
)
Hopefully, none of the "real programmers" will see this, because it's a quick & dirty solution that doesn't follow the traditional programming guidelines.

BTW, as with the other batch file there's always the possibility that something might go horribly wrong.
Always keep backups and don't use it with the original files.
Doitsu is offline   Reply With Quote
Old 06-30-2013, 05:45 PM   #8
RbnJrg
Evangelist
RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.
 
Posts: 461
Karma: 318996
Join Date: Mar 2013
Location: Rosario - Santa Fe - Argentina
Device: Kindle 4 NT
Quote:
Originally Posted by Doitsu View Post
That's very easy. You simply omit the KindleGen part, change the file selector to .mobi and end up with this very simple script.

Code:
DEL conversion.log
FOR %%f IN ("*.mobi") DO  (
		ECHO ******************************* >> conversion.log
		kindlestrip "%%~nf.mobi" "%%~nf.new" >> conversion.log
		IF EXIST "%%~nf.new" (	
			DEL "%%~nf.mobi"
			REN "%%~nf.new" "%%~nf.mobi"
		) ELSE ( 
		ECHO  "%%~nf.mobi" was NOT stripped! >> conversion.log
		)
)
Hopefully, none of the "real programmers" will see this, because it's a quick & dirty solution that doesn't follow the traditional programming guidelines.

BTW, as with the other batch file there's always the possibility that something might go horribly wrong.
Always keep backups and don't use it with the original files.
Thank you very much Doitsu!!
RbnJrg is offline   Reply With Quote
Old 03-28-2014, 04:31 PM   #9
radius
Lector minore
radius trips the light fantastic.radius trips the light fantastic.radius trips the light fantastic.radius trips the light fantastic.radius trips the light fantastic.radius trips the light fantastic.radius trips the light fantastic.radius trips the light fantastic.radius trips the light fantastic.radius trips the light fantastic.radius trips the light fantastic.
 
radius's Avatar
 
Posts: 362
Karma: 128734
Join Date: Jan 2008
Device: Sony PRS-T3, Amazon Kindle PaperWhite 2
Quote:
Originally Posted by Doitsu View Post
As HarryT has already correctly pointed out, you can simply add the -c2 switch after Kindlegen, however, since Kindlegen will run several optimization passes for each file, I wouldn't recommend using the -c2 switch in batch files.
Why not? If this is a batch, then presumably you aren't running it in anything like an interactive mode. So if it takes longer, that just means you have time for two cups of coffee instead of one.

Is there any reason other than time/CPU+disk usage not to use the higher compression?
radius is offline   Reply With Quote
Old 04-21-2014, 07:37 PM   #10
Thalia Helikon
Enthusiast
Thalia Helikon figured out that Keyser Söze was the Kevin Spacey character in less than 20 minutes.Thalia Helikon figured out that Keyser Söze was the Kevin Spacey character in less than 20 minutes.Thalia Helikon figured out that Keyser Söze was the Kevin Spacey character in less than 20 minutes.Thalia Helikon figured out that Keyser Söze was the Kevin Spacey character in less than 20 minutes.Thalia Helikon figured out that Keyser Söze was the Kevin Spacey character in less than 20 minutes.Thalia Helikon figured out that Keyser Söze was the Kevin Spacey character in less than 20 minutes.Thalia Helikon figured out that Keyser Söze was the Kevin Spacey character in less than 20 minutes.Thalia Helikon figured out that Keyser Söze was the Kevin Spacey character in less than 20 minutes.Thalia Helikon figured out that Keyser Söze was the Kevin Spacey character in less than 20 minutes.Thalia Helikon figured out that Keyser Söze was the Kevin Spacey character in less than 20 minutes.Thalia Helikon figured out that Keyser Söze was the Kevin Spacey character in less than 20 minutes.
 
Thalia Helikon's Avatar
 
Posts: 29
Karma: 110336
Join Date: Dec 2011
Location: Los Angeles, CA
Device: Kindle n-T, Archos 7 Tablet, BlkB Curve & Torch, Nook Color Tablet
how would the code be different in a Linux terminal?
Thalia Helikon is offline   Reply With Quote
Old 04-24-2014, 12:01 PM   #11
eschwartz
Irrational Optimist
eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.
 
Posts: 6,495
Karma: 10683802
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch (Wifi only)
Quote:
Originally Posted by Thalia Helikon View Post
how would the code be different in a Linux terminal?
Try this:
Code:
rm conversion.log
for file in $@; do

	echo "*******************************" >> conversion.log

	if $(kindlestrip "$file" "$file">> conversion.log); then
		echo "$file was successfully stripped!" >> conversion.log
	else 
		echo "$file was NOT stripped!" >> conversion.log
	fi
done

Last edited by eschwartz; 04-25-2014 at 06:21 PM.
eschwartz is online now   Reply With Quote
Old 04-25-2014, 06:16 PM   #12
Doitsu
Wizard
Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.
 
Doitsu's Avatar
 
Posts: 1,992
Karma: 4633978
Join Date: Dec 2010
Device: Kindle PW2
@eschwartz:

Unfortunately, your script doesn't work, because kindlestrip.py requires two parameters: the original file and the stripped file. It also doesn't work with file names that contain spaces.

This script actually works:

Code:
#!/bin/bash

# IFS change for file names with spaces in them
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

# delete old log
rm -f conversion.log

# process all .mobi files in folder
for i in *.mobi; do
  python kindlestrip.py "$i" ${i%%.*}.new
  ret=$?
	if [ $ret -ne 0 ]; then
		echo "$i" was NOT stripped >> conversion.log
	else
		rm $i
		mv ${i%%.*}.new  ${i%%.*}.mobi
		echo "$i" was stripped >> conversion.log
	fi
done

# restore default IFS
IFS=$SAVEIFS

@Thalia Helikon:
You no longer need to use kindlestrip.py, because there's a recently discovered undocumented kindlegen parameter that'll prevent kindlegen from attaching the source files, -dont_append_source. You could use it in a shell script file to convert multiple .epubs with KindleGen:

Code:
#!/bin/bash

# IFS change for file names with spaces in them
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

# delete old log
rm -f conversion.log

# process all .epub files in folder
for i in *.epub; do
  kindlegen -dont_append_source "$i" >> conversion.log
  ret=$?
	if [ $ret -ne 0 ]; then
		echo "$i" was NOT compiled. Error code: $ret >> conversion.log
	fi
done

# restore default IFS
IFS=$SAVEIFS
(Of course, you'll also need to copy the kindlegen binary to ~/bin (or create a symbolic ink) and make the shell script executable.)
Doitsu is offline   Reply With Quote
Old 04-25-2014, 06:28 PM   #13
eschwartz
Irrational Optimist
eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.
 
Posts: 6,495
Karma: 10683802
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch (Wifi only)
So I added in the second parameter. Thanks, I didn't realize it required two. According to this post

Quote:
Originally Posted by pdurrant View Post
Download and install 32-bit (x86) ActivePython from here.
Download the kindlestrip 1.2.zip file. Unzip to give kindlestrip.py
Copy the book you want stripped to the same directory as kindlestrip.
Open a command window
use the cd command to switch to the same directory as kindlestrip
type python kindlestrip.py [BOOKNAME]
where [BOOKNAME] is the name of the ebook to be stripped.
it isn't required. I had scrolled through the kindlestrip thread for the first cli example.

Shouldn't this work just as well?

Code:
rm conversion.log

#fix for() loop not handling witespace
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

for file in $@; do

	echo "*******************************" >> conversion.log

	if $(kindlestrip "$file" "$file">> conversion.log); then
		echo "$file was successfully stripped!" >> conversion.log
	else 
		echo "$file was NOT stripped!" >> conversion.log
	fi
done

#restore default IFS
IFS=$SAVEIFS
I am unsure why you need to backup to filename.new

EDIT: Just realized what you meant when you said whitespaced files won't work -- because of the for loop. Fixed.

Last edited by eschwartz; 04-25-2014 at 06:48 PM.
eschwartz is online now   Reply With Quote
Old 04-25-2014, 06:44 PM   #14
Doitsu
Wizard
Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.
 
Doitsu's Avatar
 
Posts: 1,992
Karma: 4633978
Join Date: Dec 2010
Device: Kindle PW2
You may want to test your scripts before posting them. If you had done so, you'd have found out that they don't work, because had you actually executed kindlestrip.py you'd gotten the following syntax warning:

Code:
kindlestrip.py <infile> <outfile> <strippeddatafile>
<strippeddatafile> is optional.
Quote:
Originally Posted by eschwartz View Post
I am unsure why you need to backup to filename.new
I didn't backup the file to filename.new; filename.new is the stripped .mobi file, which obviously needs to be saved under a different file name.

EDIT: Apparently the <infile> <outfile> parameters can be identical. Since most command line utilities require different names for input and output files, I assumed that this was the case for this script also.

Last edited by Doitsu; 04-25-2014 at 06:57 PM.
Doitsu is offline   Reply With Quote
Old 04-25-2014, 07:07 PM   #15
eschwartz
Irrational Optimist
eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.
 
Posts: 6,495
Karma: 10683802
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch (Wifi only)
Quote:
Originally Posted by Doitsu View Post
You may want to test your scripts before posting them. If you had done so, you'd have found out that they don't work, because had you actually executed kindlestrip.py you'd gotten the following syntax warning:

Code:
kindlestrip.py <infile> <outfile> <strippeddatafile>
<strippeddatafile> is optional.


I didn't backup the file to filename.new; filename.new is the stripped .mobi file, which obviously needs to be saved under a different file name.
Unless I am typing this on my Kindle Touch, or indeed any other mobile device.

And why must the input file differ from the output? It looked to me like some artifact of the windows code to check for success by the presence of the .new file. I merely thought to cut out the middleman and do it with the if() test itself.
eschwartz is online now   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Scripting ebook-convert for batch parallel processing? joewiz Conversion 9 03-15-2013 05:34 AM
Tag editor with batch processing semenoof General Discussions 0 01-17-2013 03:12 AM
Batch processing of PDB files? Asterra iRex 6 12-04-2007 01:10 PM


All times are GMT -4. The time now is 11:29 AM.


MobileRead.com is a privately owned, operated and funded community.