How to re-encode a MP3 audiobook to fit on a MP3 player?

August 12th, 2008 by matthias

Say, you have a large MP3 audio book (or song file collection; in this case, the Bible) and want it to fit on a MP3 player, but it does not fit. Then you can re-encode it, using lame on Linux. While a 128kbps MP3 file (normal audio book quality) has approx. 1MiByte/min, a “lame -V9″ variable birate maximum compression MP3 has just 0.4MiByte/min. When allplied to spoken language, the loss in quality is only a little.

To convert your audio book (stored in dirs and files below them), use the following:

for dir in *; do \
  for file in "$dir"/*; do \
    lame -V 9 "$file" "${file/.mp3/_V9.mp3}"; \
  done; \
done;

Then, to sort out the newly generated files, and rename them to short names, do the following:

mkdir AudioBookV9;
for dir in AudioBook/*; do \
  mkdir "${dir/AudioBook/AudioBookV9}"; \
  mv $dir/*_V9.mp3 "${dir/AudioBook/AudioBookV9}"; \
done;
for file in AudioBookV9/*/*; do \
  mv "$file" "${file/_V9.mp3/.mp3}"; \
done;

You now have all relevant files in directoy AudioBookV9, and if the size fits (check with “du -h AudioBookV9″), you want to transfer that to your MP3 player. So mount your MP3 player as a mass storage device (here, to /media/misc/), and do this:

for dir in AudioBookV9/*; do \
  sudo mkdir "/media/misc/${dir/AudioBookV9/}"; \
done;
for file in AudioBookV9/*/*; do \
  sudo cp "$file" "/media/misc/${file/AudioBookV9/}"; \
done;

Here, note that we don’t simply copy the whole AudioBookV9/* stuff recursively. This will work in most cases, but for the KINGZON MP3 Player M3ZH it did not. Here, the order in which files are written to the device is important, as “normal playing mode” means that the device plays files in this order, not in alphabetical order. Copying whole directories makes the files being written in the order they appear in the directory tables, so you’ll have all your chapters mixed up on the device. So use the workaround, or you’ll hear the end of your fascinating novel way too early ;)

Posted in Audiobearbeitung, Sprache: Englisch, alle Artikel | No Comments »

How to change the margins of PDF files while keeping the page size (”scale the content”)?

August 2nd, 2008 by matthias

This is on how to achieve that on Linux. There are multiple alternatives, but none of them proved perfect yet. Let’s start.

Alternative 1: using pstops

The task can be performed with pstops (whereof psnup is a simplified frontend).

  1. Convert your PDF file to a PS file, by printing to a file in Adobe Reader, or by using pdf2ps (Ghostscript-based) or pdftops (xpdf-based).
  2. Use pstops to adjust the marginspstops -p a4 "L@.9(1cm,1cm)" in.ps out.ps

    On mounting pages: here, the the task is to mount two A4 pages in A5 format on one A4 page, guaranteeing page margins of 3cm at left and right and 2cm at top and bottom. We need a width of 150mm and a height of 257mm. To scale 297mm (A4 height) to 150mm, use factor 0.505. Such n-up mounting together with freestyle adjustment of margins is not possible ith psnup, which has a simpler user interface.

    pstops -p a4 "2:0L@.505(18cm,2cm)+1L@.505(18cm,14.85cm)" in.ps out.ps

  3. Convert the PS file back to PDF by using pdftops.

The problem with psnup (from PSUtils Release 1 Patchlevel 17) and also of its frontend psnup is that it converts fonts to bitmap fonts (Adobe Type 3). This can be detected as rastered fonts when viewing the PDF file with Adobe Reader. It generates somewhat lower print quality, but is still acceptable. What is not acceptable (with respect to file size) is that pstops converts the whole file to an image if it has no idea how to treat it.

To debug pstops and psnup output, you can use the -b option, which will mark out the original pages’ borders.

Alternative 2: Using Adobe Reader and printer margins

This has not yet been worked out, but might be possible. When printing (to a file or otherwise) Adobe Reader will fit the pages into the printable area of the selected printer. Now the idea would be to choose the special printer “Custom …”. This allows you to enter a lp print command. Per the lp documentation, CUPS lp understands options to set the margins. For 2cm at top and bottom and 3cm at left and right, use this command:

/usr/bin/lp -o page-top=57 -o page-bottom=57 -o page-left=85 -o page-right=85

However, this currently does not work our, for an unknown reason. It does not change anything, i.e. the document is printed as if you selected “Page scaling: none” instead of “Page scaling: fit to printable area”. If this command is not possible, another alternative would be to set up a printer definition with exactly the margins you desire, in a way that lets you change these margins easily.

If you just need “larger” margins around your page (without exact measures), you can do the following:

  1. Print the file with Adobe Reader to a PS file. Use the option “Page scaling: fit o printable area”. Try several different printers including the “Custom …” special printer to find one that adds margins of the same size all around the page. This step will generate a PS file with larger margins than the original PDF file had, even though this is not correctly shown in the preview of Adobe Reader 8.1.1.
  2. Convert the file to a PDF file by using ps2pdf.
  3. Repeat from step 1 with your new PDF file until the margins are large enough for you.

This method has the obvious disadvantage of not allowing to specify the margins exactly, but at least the file retains vector fonts and graphics (unlike whenusing pstops, see above).

If you need the margin adjustents in combination with Adobe Readers n-up printing (e.g. 2 pages per sheet), and if you can adjust the margins in your source file (before generating the initial PDF), you can do the following: adust the margins in the original file so that, after the n-up scaling, these margins together with the selected printer’s margins, result in the margins you desire. When printing one two pages A4 on one sheet A4 with the special prnter “Custom …” in Adobe Reader 8.1.1, the following margins are used (measured in the output A4 page):

  • left 5,25mm
  • right 13,71mm
  • top 6,59mm
  • bottom 6,59mm (probably)

Alternative 3: Using Adobe Reader and printing the “current view”

Another alternative:

  1. Use Adobe Reader to print your page to a large sheet without scaling.
  2. Use ps2pdf to re-destill the PS file to PDF.
  3. Open the new PDF file in Adobe Reader and choose an appropriate view. Use the “print current view” option in Adobe Reader to print exactly thet view.
  4. Re-destill the PS output to PDF.
  5. That way, you can even achieve n-up mounting: say you generated to pages, each with content in different places and the rest white space. You can overlay these pages on top of each other using pdftk with the background option.

Alternative 4: Using ghostscript

The following would constitute an elegant solution: freely adjustable margins and vector fonts and images, and avoiding the GUI hassles. Sad enough, it does not work yet. The idea come from this thread.

gs \
  -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dSAFER \
  -dCompatibilityLevel="1.3" -dPDFSETTINGS="/printer" \
  -dSubsetFonts=true -dEmbedAllFonts=true \
  -sPAPERSIZE=a4 -sOutputFile="out.pdf" \
  -c "<</BeginPage{0.5 0.5 scale -90 rotate -2384 0 translate}>> setpagedevice" \
  -f in.pdf

The instruction works, apart from the important -c option part, which adds a postscript command. This instruction would also work with PDF files.

Posted in Acrobat Reader, DTP, Sprache: Englisch, alle Artikel | No Comments »

How can I do arbitrary vector-oriented modifications to a PDF file on Linux, though all the editors are still buggy?

August 1st, 2008 by matthias

If you have a PDF which you want to modify (and can’t get hold of the source format file), you might have tried scribus to open and change it. This works well for some simple cases, but there are many import errors for more complex files. Also, the same might happen with Inkscape if you have managed to convert the file to SVG format, e.g. using CorelDraw on a Windows virtual machine.

What’s always possible is to import the file in GIMP (”File -> Open”), using a high resolution such as 300dpi. But you might want to avoid the big file sizes and loss in quality, and want to do it vector-oriented.

Therefore, the following solution should work out:

  1. Convert a low resolution raster image version of the PDF file, e.g. by opening in GIMP (with 150dpi) and saving in JPG format.
  2. Create a OpenOffice.org Draw drawing with the same page size as your PDF file, and place the raster image file into the background.
  3. Now that you have the placement of things, change the appearance by adding other elements on top your background. You might want to get some vector oriented elements out of the PDF file by importing the file in scribus and saving as SVG or EPS file, importing that in OOo Draw.
  4. Export the OOo file to PDF, temporarily removing your background for that. The good thing is that all OOo Draw elements get high-quality vecor oriented PDF elements.
  5. Add your new PDF file on top of the original one, by saying something like:
    pdftk in.pdf stamp overlay.pdf output out.pdf;

As the original vector elements are never converted from PDF to another format and back, no import / export bugs can come in the way!

Posted in DTP, Graphikbearbeitung, OpenOffice, Sprache: Englisch, alle Artikel | No Comments »

How can I combine two PDF files so that they are “overlayed” one to the other?

July 21st, 2008 by matthias

This might become necessary if you want to add content to an already existing PDF file, e.g. when doing pre-press work for flyers etc.. For example, you might resize one PDF file to have additional place on the page, and put other content into that free space.

Get yourself two PDF files to overlay on each other, both of the same page size (else automatic resizing is employed), and both one-sided.

Then use this command to generate the combined PDF file with overlays:

pdftk file1.pdf background file2.pdf output out.pdf

Posted in Acrobat Reader, DTP, Sprache: Englisch, alle Artikel | No Comments »

How can I “magnify” a PDF document with DIN A4 pages to another one with DIN A3 pages?

June 5th, 2008 by matthias

Use Adobe Reader for that (here on Linux, but you could use another free Ghostscript based destiller on Windows, e.g. the PDF pseudo printer PrimoPDF).

  1. Open the document in Adobe Reader.
  2. Open the dialog “File -> Print …”.
  3. Select the printer “Custom …”, and in the combobox that appears at the right border of the dialog, select DIN A3 as paper size (you could also enter 420×297mm as custom paper size for this printer, or use a physical printer with both modes).
  4. Select “Page scaling: Fit to printable area”, and if necessary “Auto Rotate and Center”.
  5. Select “Print to file”, and select a file to print to.
  6. Press “OK”.
  7. The resulting file is a postscript file. The page size of a postscript file can be auto detected, or manually set. Here, re-destilling to PDF with ps2pdf gets the page size wrong (it uses DIN A4). Therefore, open the document in kghostview and print it from there to a PDF file (it has correct page size auto detection, see the menus).

One can probably use ps2pdf when telling it about the desired page size explicitly, but I haven’t tested that yet:

ps2pdf -sPAPERSIZE=a3 file.ps

Posted in Acrobat Reader, DTP, Sprache: Englisch, alle Artikel | No Comments »

Recommendation how to read PDF-books under Linux?

May 28th, 2008 by daniel

Use AdobeReader in fullscreen mode on a seperate desktop. Change the desktop with Strg+F1, Strg+F2…

Posted in Linux, Sprache: Englisch | No Comments »

How can I create an exact image of my harddisk on an external disk?

May 28th, 2008 by daniel

Attention! Check if these are the correct mountpoints!

  1. sudo dd if=/dev/hda of=/dev/sda

To see the copy progress do the following:
Get to know the process-id of the running dd command

  1. ps -ax

Open a new terminal window and execute the following:

  1. sudo kill -USR1 {dd-process-id}

Now you can see the copy progress in the terminal where the dd command is running.

Posted in Linux, Sprache: Englisch | No Comments »

How can I securely delete the whole harddisk?

May 28th, 2008 by daniel

  1. dd if=/dev/zero of=/dev/hda bs=65535

Posted in Linux, Sprache: Englisch | No Comments »

How can I check the harddisk for errors?

May 28th, 2008 by daniel

  1. sudo badblocks -vs /dev/hda1

Posted in Linux, Sprache: Englisch | No Comments »

How do I search in specific files for a searchphrase starting recursively from the current directory level?

May 28th, 2008 by daniel

Use “grep”:

  1. grep -rsinH –include={pattern} "{searchphrase}" {file_or_directory}

For example:

  1. grep -rsinH –include=\*.txt "searchphrase" ./

“rsinH” is equal to: –recursive –no-messages –ignore-case –line-number –with-filenames

As an alternative you can use grep in combination with find:

  1. for f in $(find . -name \*.txt); do  grep -sinH "{searchphrase}" $f; done;

Posted in Linux, Shellscripts, Sprache: Englisch | No Comments »