dotfiles/bin/anytomobi
2013-02-16 01:07:51 +04:00

64 lines
1.7 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Convert ebooks to Kindles Mobi format
#
# Usage: anytomobi <file or folder> [destination folder]
#
# Author: Artem Sapegin, sapegin.me
#
# Require:
# - Calibre - http://calibre-ebook.com/download (install command line tools from Calibres settings)
#
# Manuals:
# - http://manual.calibre-ebook.com/cli/ebook-convert.html
# - http://manual.calibre-ebook.com/cli/ebook-meta.html
command -v ebook-convert >/dev/null 2>&1 || { echo >&2 "ebook-convert not installed: see comments for instructions."; exit 1; }
function convert_file() {
local infile="$1"
local intype=${infile##*.}
local intype=$(echo $intype | tr '[:upper:]' '[:lower:]')
local infilename=$(basename "$1")
local outfilename="${infilename%.*}.mobi"
if [[ "$intype" == "pdf" ]]; then
# Just copy PDF files to destination directory
if [[ "$outdir" != "." ]]; then
cp "$infile" "$outdir/"
fi
return
else
# Convert any other format (MOBI too) to old MOBI format to force left text alignment
ebook-convert "$infile" "$outdir/$outfilename" --output-profile=kindle_pw --mobi-file-type=old \
--mobi-ignore-margins --mobi-keep-original-images --no-inline-toc --remove-paragraph-spacing \
--change-justification=left --keep-ligatures --smarten-punctuation --pretty-print --filter-css=color
fi
echo
echo "Filename : $outfilename"
ebook-meta "$outdir/$outfilename"
}
function convert_dir() {
local dir=$1
for file in "$dir"/*.{mobi,pdf,epub,fb2}; do
if [[ -e "$file" ]]; then
convert_file "$file"
fi
done
}
outdir=${2-.}
mkdir -p "$outdir"
if [[ -f "$1" ]]; then
convert_file "$1"
elif [[ -d "$1" ]]; then
convert_dir "$1"
else
echo "Usage: `basename $0` <file or folder> [destination folder]"
exit 1
fi