批量转换 epub 至 mobi 文件

Windows

# 下载 Calibre , 找到 ebook-convert.exe
# https://calibre-ebook.com/download

$app = ".\ebook-convert.exe" #  ebook-convert.exe 路径 
$root = ".\epub\" # 电子书路径 

Get-ChildItem "$root\*.epub" | %  FullName | % {

    $newPath = [System.IO.Path]::ChangeExtension($_, ".mobi")

    if (-not [System.IO.File]::Exists($newPath))
    {
        & $app $_ $newPath
        if (-not $?)
        {
            throw [System.ApplicationException]::new("文件 $_ 转换失败")
        }
    }
}

Ubuntu

# 安装 
sudo -v && wget --no-check-certificate -nv -O- https://download.calibre-ebook.com/linux-installer.sh | sudo sh /dev/stdin

$root = $(xdg-user-dir DOWNLOAD)

$bookRoot = Join-Path $root -ChildPath "epub"

if(-not (Test-Path $bookRoot)){
    throw [System.IO.FileNotFoundException]::new("电子书目录不存在")
}

$books = Get-ChildItem $bookRoot -Filter *.epub

$books | % {

    $path = $_.FullName
    $newPath = [System.IO.Path]::ChangeExtension($path, ".mobi")
    if (-not (Test-Path $newPath)) {
        & "ebook-convert" $path $newPath
        if (-not $?) {
            throw [System.ApplicationException]::new("文件 $path 转换失败")
        }
    }
}

macOS

# brew install --cask calibre

# 书本目录
$downloads = Join-Path $HOME "Downloads"
$ebooksDir = join-Path $downloads -ChildPath "ebooks"
$azw3sDir = join-Path $ebooksDir -ChildPath "azw3s"

if ($(-not $azw3sDir) -or $(-not $(Test-Path $azw3sDir)) ) {
    throw "目录不存在, $($azw3sDir)"
}

$books = Get-ChildItem $azw3sDir -Filter *.azw3 | Sort-Object -Property LastWriteTime

Write-Host "检测到待转换图书 $( $books | measure | % Count) 本 " 

$books | ForEach-Object {

    $book = $_
    $bookName = $book.Name
    Write-Host "正在转换: $($bookName)"

    $epubName = [System.IO.Path]::ChangeExtension($bookName, "epub")
    $epubFile = Join-Path $ebooksDir -ChildPath $epubName
    $epubDir = [System.IO.Path]::GetDirectoryName($epubFile)

    $runArgs = "`"$($book.FullName)`" `"$($epubFile)`""
    Write-Host $runArgs
    $status = Start-Process "ebook-convert" -ArgumentList $runArgs -WorkingDirectory $epubDir -Wait -PassThru
    if ($status.ExitCode -ne 0) {
        throw "转换失败, 错误码: $($status.ExitCode)"
    }
}
上一篇
下一篇