...making Linux just a little more fun!

2-Cent Tips

2-cent Tip: Screenshots without X

Kapil Hari Paranjape [kapil at imsc.res.in]


Sat, 21 Mar 2009 07:50:04 +0530

Hello,

I had to do this to debug a program so I thought I'd share it.

X window dump without X

How does one take a screenshot without X? (For example, from the text console)

Use Xvfb (the X server that runs on a virtual frame buffer).

Steps:

  1. Run Xvfb
       $ Xvfb
     This will usually start the X server :99
       $ DISPLAY=:99 ; export DISPLAY
  2. Run your application in the appropriate state.
       $ firefox http://www.linuxgazette.net &
  3. Find out which window id corresponds to your application
       $ xwininfo -name 'firefox-bin' | grep id
     Or
       $ xlsclients
     Use the hex string that you get as window id in the commands
     below
  4. Dump the screen shot of that window
       $ xwd -id 'hexid" > firefox.xwd
  5. If you want to, then kill these applications along with the 
     X server
       $ killall Xvfb

'firefox.xwd' is the screenshot you wanted. Use 'convert' or on of the netpbm tools to convert the 'xwd' format to 'png' or whatever.

Additional Notes:

A. You can use a different screenshot program.

B. If you need to manipulate the window from the command line, then programmes like 'xautomation' and/or 'xwit' are your friends. Alternatively, use a WM like "fvwm" or "xmonad":

  DISPLAY=:99 xmonad &
This will allow you to manipulate windows from the command line if you know some Haskell!

Regards,

Kapil. --

[ Thread continues here (3 messages/3.04kB) ]


2-cent Tip: Lists of files by extension

Ben Okopnik [ben at linuxgazette.net]


Sat, 21 Mar 2009 15:36:49 -0400

Recently, I decided to sort, organize, and generally clean up my rather extensive music collection, and as a part of this, I decided to "flatten" the number of file types that were represented in it. Over the years, just about every type of audio file had made its way into it: FLAC, M4A, WMA, WAV, MID, APE, and so on, and so on. In fact, the first step would be to classify all these various types, get a list of each, and decide how to convert them to MP3s (see my next tip, which describes a generalized script to do just that.)

The process of collecting this kind of info wasn't unfamiliar to me; in fact, I'd previously done this, or something like it, with the "find" command when I was trying to establish what kind of files I'd want to index in a search database. This time, however, I took a bit of extra care to deal with names containing spaces, non-English characters, and files with no extensions. I also defined a list of files that I wanted to ignore (see the "User-modified vars" section of the script) and provided the option of specifying the directory to index (current one by default) and the directory in which to create the 'ext' files (/tmp/files<random_string>) by default; the script notifies you of the name.)

This isn't something that comes up often, but it can be very useful in certain situations.

#!/bin/bash
# Created by Ben Okopnik on Thu Mar 12 11:54:02 EDT 2009
# Creates a list of files named after all found extensions and containing the associated filenames
 
[ "$1" = "-h" -o "$1" = "--help" ] && { echo "${0##*/} [dir_to_read] [output_dir]"; exit 0; }
[ -n "$1" -a ! -d "$1" ] && { echo "'$1' is not a valid input directory"; exit 1; }
[ -n "$2" -a ! -d "$2" ] && { echo "'$2' is not a valid output directory"; exit 1; }
 
################ User-modified vars ########################
dir_root="/tmp/files"
ignore_exts="m3u bak"
################ User-modified vars ########################
snap=`pwd`
[ -n "$1" ] && snap="$1"
[ -n "$2" ] && dir_root="$2"
out_dir=`mktemp -d "${dir_root}XXX"`
echo "The output will be written to the '$out_dir' directory"
cd /
 
old=$IFS
IFS='
'
[ -n "`/bin/ls $out_dir`" ] && /bin/rm $out_dir/*
for n in `/usr/bin/find "$snap" -type f`
do
    ext="`echo ${n/*.}|tr 'A-Z' 'a-z'`"
    # Ignore all specified extensions
    [ -n "`echo $ignore_exts|/bin/grep -i \"\\<$ext\\>\"`" ] && continue
    # No extension means the substitution won't work; no substitution means
    # we get the entire path and filename. So, no ext gets spun off to 'none'.
    [ -n "`echo $ext|grep '/'`" ] && ext=none
    echo $n >> $out_dir/$ext
done
 
echo "Done."
-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


2-cent Tip: Converting from $FOO to MP3

Ben Okopnik [ben at linuxgazette.net]


Wed, 25 Mar 2009 10:03:21 -0400

Recently, while organizing my (very large) music library, I analyzed the whole thing and found out that I had almost 30 (!) different file types. Much of this was a variety of info files that came with the music (text, PDF, MS-docs, etc.) as well as image files in every conceivable format (which I ended up "flattening" to JPG) - but a large number of these were music formats of every kind, a sort of a living museum of "Music Formats Throughout the Ages." I decided to "flatten" all of that as well by converting all the odd formats to MP3.

Fortunately, there's a wonderful Linux app that will take pretty much every kind of audio - "mplayer" (http://www.mplayerhq.hu/DOCS/codecs-status.html#ac). It can also dump that audio to a single, easily-convertible format (WAV). As a result, I created a script that uses "mplayer" and "lame" to process a directory of music files called "2mp3".

It was surprisingly difficult to get everything to work together as it should, with some odd challenges along the way; for example, redirecting error output for either of the above programs was rather tricky. The script processes each file, creates an MP3, and appends to a log called '2mp3.LOG' in the current directory. It does not delete the original files - that part is up to you. Enjoy!

#!/bin/bash
# Created by Ben Okopnik on Mon Jul  2 01:16:32 EDT 2007
# Convert various audio files to MP3 format
#
# Copyright (C) 2007 Ben Okopnik <[email protected]>
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
 
########## User-modifiable variables ###########################
set="*{ape,flac,m4a,wma,qt,ra,pcm,dv,aac,mlp,ac3,mpc,ogg}"
########## User-modifiable variables ###########################
 
# Need to have Bash expand the construct
set=`eval "ls -1 $set" 2>/dev/null`
# Set the IFS to a newline (i.e., ignore spaces and tabs in filenames)
IFS='
'
# Turn off the 'fake filenames' for failed matches
shopt -s nullglob
 
# Figure out if any of these files are present. 'ls' doesn't work (reports
# '.' for the match when no matching files are present) and neither does
# 'echo [pattern]|wc -w' (fails on filenames with spaces); this strange
# method seems to do just fine. 
for f in "$set"; do ((count++)); done
[ -z "$count" ] && { echo "None of '$set' found; exiting."; exit 1; }
 
# Blow away the previous log, if any

[ ... ]

[ Thread continues here (1 message/4.19kB) ]



Talkback: Discuss this article with The Answer Gang

Copyright © 2009, . Released under the Open Publication License unless otherwise noted in the body of the article. Linux Gazette is not produced, sponsored, or endorsed by its prior host, SSC, Inc.

Published in Issue 161 of Linux Gazette, April 2009

Tux