Saturday, November 30, 2013

Blogger or Wordpress?

I like the default look of this blog on Blogger: simple, stylish and easy to read. Still, there's a few things that I don't like:
  • E.g. the 'pop-ups' urging you to involve Google+ in your posts. I don't mind being reminded once but they keep at it in a most annoying way.
  • Also, I'm amazed that I am reduced to using the on-line editor to prepare posts. Why can I not simply use my favorite editor vim to prepare a post and then upload it?
  • Finally, it's OK to use <pre> and <code> for source code but for longer excerpts, syntax coloring and highlighting of selected lines is highly desirable. Syntaxhighlighter is OK but it is not exactly easy to use while prettify is too primitive. Compared to that, Wordpress provides an easy to use interface to SyntaxHighlighter which has everything you need and then some.

So I wondered whether I would perhaps be better off with Wordpress?

Tim Brooks provides a detailed comparison of Wordpress and Blogger from which I conclude that I'd better stick to Blogger unless I use the self-hosting method: installing wordpress, which is open source under the GPL license, on my own server. On Ubuntu, it seems trivial to install Wordpress.

Sapore di mare

This film ("Time for Loving" in English) can cause double nostalgia: for the 1960's in which the story is set, and for the 1980's when it was made. The latter is evident to me from how the story is developed, it reminds me of some toe-curling French comedies like "Les Bronzés". There the humor is so bad that it becomes hilarious. So it is with "Sapore di mare": I loved it although it is very mediocre.

The story is about youngsters on holiday in Viareggio on the Tuscan coast and how they enjoy themselves with members of the opposite sex. There are the obligatory stereotypes: an older woman with a nerd, a 'player' from a rich family, a naive and initially innocent girl with a modest background, an experienced English girl (!) etc. Apparently there is a sequel, so it must have been a big success at the time.

☆☆☆

Thursday, November 28, 2013

The best tutorial on Monads

I like monads and Haskell, although I'm not a frequent user, and I remember that it took me a long time to grok monads. It may be that I'm a bit slow. Anyway, I finally got there.

Yesterday, however, I stumbled upon a tutorial that, had I read it before, would have saved me a lot of time:

You Could Have Invented Monads! (And Maybe You Already Have.).
So, if you are struggling with monads, give it a try. I wish I did at the time.

Concatenating .avi files

I thought it would require the use of a sophisticated video editing tool. However, it turns out that in Ubuntu it's dead easy.

    avimerge -i file1.avi file2.avi -o outputfile.avi

Avimerge lives in the transcode package, so you may have to do the following first.

    sudo apt-get install transcode  


Wednesday, November 27, 2013

La Grande Bellezza

The english title is The Great Beauty. The beauty here refers to the city of Rome, and probably more specifically its night life. The main character, Jep Gambardella, is a writer who wrote only one book, which was a huge success. After that, he got distracted by the temptations of Rome and became a journalist and society figure who 'knows everyone'. The film consists of several loosely connected little stories, often moving and/or funny involving e.g. a 'saint' nun, modeled on 'mother Teresa', a gastronome cardinal, a midget editor etc. Many scenes play on the terrace of Jep's apartment, overlooking the Colosseum, a fantastic view at night.

One quote that I liked:

.. a friend, every now and again, needs to make their friend feel as a child.

The strong point of the film is the atmosphere it creates. It has been compared with other great movies about Rome, see the wikipedia article. Although, it is not as good as the 'originals', it is well worth watching.

☆☆☆☆

Thursday, November 14, 2013

Ripping a DVD and hard-wiring subtitles

One way to back up a DVD is just to copy its 'raw' contents, resulting in a file like movie.iso which contains all DVD data files organized as an iso-9660 file system. We use the dd command to copy from the raw DVD device.
DEV=/dev/sr0 # name of DVD reader device
ISO=movie.iso # name of target file
umount ${DEV} # dd works on unmounted devices
dd bs=32000000 if=${DEV} of=${ISO} ||
  { echo "dd from $DEV failed"; exit 1;} 
To get access to the files, we can mount movie.iso in read-only mode.
ME=$(id -u) # our user id to 'own' the mounted file system
sudo mount -t iso9660 \
  -o ro,loop,nosuid,nodev,uid=${ME},gid=${ME},mode=0777 \
  ${ISO} /mnt || { echo "mount ${ISO} failed"; exit 1; }
Now we can explore /mnt and find it has a directory video_ts which contains files as in the example below.
$ ls /mnt/video_ts

video_ts.bup  vts_01_0.vob  vts_02_1.vob  vts_02_6.vob
video_ts.ifo  vts_01_1.vob  vts_02_2.vob  vts_02_7.vob
video_ts.vob  vts_02_0.bup  vts_02_3.vob  vts_02_8.vob
vts_01_0.bup  vts_02_0.ifo  vts_02_4.vob
vts_01_0.ifo  vts_02_0.vob  vts_02_5.vob

$ du -sm . # how many megabytes do these files take?
7590
There are two video sequences represented by the .vob files with names starting with vts_01 and vts_02 respectively. Looking at the total sizes of these files, it is clear that the main movie is stored in vts_02_0.vob...vts_02_8.vob.
It is also clear that, partly due to the use on most DVD's of the older mpeg-2 codec, the video sequences take a lot more space, almost 8GB, than if they were encoded with a more efficient codec. Still, media players such as vlc can play the movie.iso file, including handling menus, choosing subtitles etc. as on the original DVD.
If we want to save space, however, we can convert the vts_02_0.vob...vts_02_8.vob sequence to a single .mp4 file in our home directory. For this we can use the avconv command.
cat vts_02_*.vob | avconv -i pipe:0 ~/movie.mp4
Due to the better codec, the size of movie.mp4 is now about 1.5GB, a huge improvement while maintaining quality. Unfortunately, if the original DVD provides a choice of subtitles, they will not be included in the copy. A simple solution would be to make a copy that has one of the subtitle languages hardwired into it.
Apparently, DVD's store subtitle information in so called SPU (SubPicture Units). Essentially, these are images containing the subtitle text that are presumably inserted at the appropriate moments in the video stream. The SPUs come with an index which indicates at what time which picture of the selected subtitle language has to be shown.
So, one way to solve the problem would be to find a program that is able to extract the subtitle data (and index) and then define a post-processing filter that would add the subtitles in the chosen language.
Surprisingly, there seems to be only one program under Linux that supports the definition of a filter as described above: the avidemux video editor. Fortunately, avidemux also contains a tool to extract the SPU data and index from the video input. Unfortunately the command line interface for avidemux does not seem to support subtitle manipulation, so we are forced to use the GUI (Graphical User Interface).

Click 'open' button to load video file(s).
Clicking the indicated button lets us select a video file, we take the first one from the vts_02_*.vob sequence. Normally, we'd expect to select all the following files in the sequence but apparently Avidemux cleverly does that automatically.

It may be that Avidemux takes a while to load the file(s) because it indexes them. Once the movie has been loaded, not much has changed on the interface but see the arrows on the next image.
Clicking on the indicated button will show information on the format of the movie, as shown in the pop-up on the right.

The movie is loaded.
Properties of the loaded movie.
Now, we are ready for the extraction of the subtitle information. Click on tools in the top bar (not shown) and select VOB -> VobSub. A window pops up, shown on the right below, where you select the first .vob file (the same as was used to load the movie into avidemux), the accompanying .ifo file: vts_02.ifo and finally, a file name prefix that will be used to store the subtitle information that is about to be extracted. Note that the latter file cannot be in (under) the /mnt directory since /mnt contains a read-only file system.
Clicking OK starts the process and will produce two files: vts_02.sub and vts_02.idx containing, respectively, the subtitle images and the index describing when they have to be displayed.
Finally, we define a filter which will add subtitles in a selected language to the video output to be produced by avidemux. For that we first define the (container) format and the video codec, as shown on the left below.
Clicking on Filters under Video brings a pop-up menu where we select the subtitle menu that inserts "vobsub" frames. Double clicking on the "vobsub" selection brings up another menu where we select the file ~/tmp/vts_02.idx containing the subtitles information. Once that is done, we can also select the subtitle language that is to be hardwired into the output.

After all that, we are ready to produce the output. This is done by clicking the Save (next to Open) button. "Save" may not be an intuitive name for what is essentially a video transformation process but, since avidemux is mainly a video editor, it actually makes sense.

Wednesday, November 13, 2013

Google Backup for ISP DNS Servers

I'm using a standard WIFI setup where the ISP also provides the DNS server. In ubuntu Network Manager, this can be seen under connection information where the address of the DNS server is listed as '192.168.1.1', i.e. the ISP-provided router.

Recently, there were some problems: I could connect to machines using their numeric address (e.g. 91.189.90.58) but not using their 'name' (e.g. 'www.ubuntu.com'). It followed that the ISP's DNS server was down. It came back up after a few hours, but I decided that I needed a backup DNS server.

Fortunately, Google provides free public DNS servers at addresses 8.8.8.8 and 8.8.4.4.

After a lot of searching, I finally found out that defining these extra backup DNS servers is not done via the network manager but via the configuration file for the DHCP client which lives in

/etc/dhcp/dhclient.conf
For my purpose, it was sufficient to add the following line to this file:
append domain-name-servers 8.8.8.8 and 8.8.4.4;
Et voilà: next time the ISP's DNS server packed up, the system seamlessly switched to Google's servers.

Tuesday, November 12, 2013

Nmcli and keeping an eye on the VPN

Virtual Private Network (VPN) connections are often used by particular applications such as P2P clients. If, for some reason, the VPN connection goes down while the underlying non-VPN connection to the ISP is still up, the P2P client may continue operating "in the open", which may be undesirable e.g. because the use of the client is illegal in the ISP's country.

Below is a small script that regularly checks the VPN connection and, if the connection is down, stops the application that we wish to only operate over the VPN.

Landstander wrote a bash script to do just that. It contained a few errors, which were corrected by sgleo87 here.

Thanks to this script, I learned that the (in)famous NetworkManager used by Ubuntu can be accessed using a command line program, called nmcli. The program can operate on 3 objects: 'dev' (devices), 'con' (connections) and 'nm' (the network manager proper). The main operations that can be performed on an object are 'status', 'enable' and 'sleep'. The script below uses the 'status' operation on 'con', which outputs a table containing data on each connection. One of the columns is called 'VPN' and for each connection there'll be a 'yes' or 'no' entry indicating whether the connection represented by the row is a VPN connection or not.

My version of the script is shown below. The main change is that this version does not attempt to restart the application when the VPN connection is back. The rationale is that since the admin needs to take action to restart the VPN, she might as well restart the application. There are also some more sanity checks and comments.

#!/bin/bash
PROGRAM=myprogram # the program to be stopped if VPN does
EMAIL_ADDRESS=jj@gmail.com # the admin's email address

function fatal() {
  echo "fatal error: $1" 2>&1; exit 1
}

while true 
do
  pid=$(pidof ${PROGRAM}) || fatal "pidof ${PROGRAM}"
  # nmcli 
  #   -t : terse
  #   -f VPN: print 'VPN' column
  #   con: object, the connections
  #   status: command, output status table
  # grep -c: prints count of matching lines
  ok=$(nmcli -t -f VPN con status |  grep -c yes) 
  echo -n "$(date): "
  if [ $ok -gt 0 ]
  then
    echo "ok"
  else
    echo "ERROR: VPN DOWN, KILLING ${PROGRAM}"
    kill -9 ${pid}
    # warn the admin
    mutt -s "VPN DOWN" ${EMAIL_ADDRESS} </dev/null
    exit 1
  fi
  sleep 1 # seconds to wait between checks
done

Using Gmail via Mutt

Often, using a command line mail client is much more efficient than e.g. a web-based one. Some example cases:
  • If, like me, you have a Gmail account, it is, as far as I know, not possible to easily use the Gnu Privacy Guard without resorting to encrypting files "by hand" and then uploading them as attachments. It wouldn't make much sense anyway, decrypting stuff on Google's servers for all of the NSA to see.
  • Another use case for a command line mail client is when you want to send mail from within a script. E.g. the following bash script fragment sends a message with an attachment and a subject line:
    mutt -a picture.jpg -s "a subject" jj@gmail.com <<EOF
      As promised, the picture is attached.
      Blah blah.
    EOF
    
    where mutt refers to the, imo, best command line mail client program.

Luckily, you can use the Gmail service as an IMAP server for your preferred command line mail client.

Below are the instructions to set up mutt such that it will use Gmail to send and receive messages. Note that any message sent/received will also be available on line, i.e. in the folders of your gmail account.

  1. First, you need to install mutt. In ubuntu
    sudo apt-get install mutt
    
    On my 13.10 ubuntu system, also the mail server postfix was installed and the system prompted for postfix configuration options. Since I was not going to use postfix, I selected 'no configuration'.
  2. Next you fill in the configuration file called .muttrc in your home directory. The instructions can be found on the GMailOverIMAP page of the Mutt developer resources site and are merely repeated here.
    set imap_user = 'yourusername@gmail.com'
    set imap_pass = 'yourpassword'
    
    set spoolfile = imaps://imap.gmail.com/INBOX
    set folder = imaps://imap.gmail.com/
    set record="imaps://imap.gmail.com/[Gmail]/Sent Mail"
    set postponed="imaps://imap.gmail.com/[Gmail]/Drafts"
    
That's it.

Showing source code in a post

Since I'm interested in computer science, I'll probably want to show some source code at some point in the future. There appear to be many possibilities, of which I looked at 3.
  1. The obvious way would be to use <pre> like so:
    int fib(unsigned int n) {
      if ( n <=1 )
        return n;
      else
        return fib(n-1) + fib(n-2);
    }
    
    unsigned int a[] = 
      { fib(1), fib(2), fib(3) };
    
  2. Using SyntaxHighlighter: this gives nice results but the method seems complicated and I wondered whether something simpler is available.
  3. Google-code's prettify seems simple enough. Here's the input: <script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script> <pre class="prettyprint lang-cpp"> int fib(unsigned int n) { ... </pre> And here's the result:
    int fib(unsigned int n) {
      if ( n <=1 )
        return n;
      else
        return fib(n-1) + fib(n-2);
    }
    
    unsigned int a[] = 
      { fib(1), fib(2), fib(3) };
    
    So adding one "script" line gives you syntax coloring and a box around the code.
I guess, I'll use (1) or (3).

Sunday, November 10, 2013

xkdc

I belatedly discovered xkcd and now I'm hooked: wasting time hitting that "random" button. Here's an example:

Friday, November 8, 2013

Gianni e le donne

This delightful comedy seems to be a prequel to Pranzo di ferragosto . The main character, Gianni, is the same. Only he's still married and his mother lives in her own home, which she manages to squander without her son getting any say.
The film is mainly about the attempts of the middle-aged Gianni to seduce a young woman, any good looking one. He's been put up to this project by his lawyer friend who seems as desperate as he is. Moreover, he discovers that a far older and fatter acquaintance of his manages to get it off with a voluptuous blonde running a neighborhood shop.
Needless to say, all his attempts come to nothing. The women he tries to woo usually make use of his services (cooking, walking the dog, finding bottles hidden by their daughters, ..) but fail to succumb to his charms.
I found Pranzo di ferragosto slightly more hilarious, but also this movie is well worth watching. ☆☆☆☆

Sunday, November 3, 2013

The Naked Civil Servant

This is a 1975 TV film based on the autobiography of Quentin Crisp. The strange title was coined by him. Since, for several years, his main source of income was modeling (naked) in (state) art schools, he considered himself a "naked civil servant".
Crisp was a very effeminate gay man, and he made no effort to hide the fact: dyeing his hair, using make-up etc. Before the second world war, and even afterwards, this honesty was not without risk: the film shows how he got beaten up on several occasions, just because he looked "abnormal". The term used at the time for his "condition" was "sexual perversion". The scene were the army refuses to take him because of this "perversion" is hilarious.
All in all a very entertaining movie and a reminder that anti-gay bigotry existed in Europe for longer than I thought. E.g., amazingly, practicing homosexuality was illegal in the UK until 1967: ☆☆☆.

Saturday, November 2, 2013

Il est plus facile pour un Chameau...

The title of this film refers to a statement by Jesus, according to Matthew 19:24 for the experts, that
"... it is easier for a camel to go through the eye of a needle than for a rich man to enter the kingdom of God. "
And that is the subject of the film: the rich heiress Frederica, played by Valeria Bruni Tedeschi feels uncomfortable about being rich. Her boyfriend, a committed leftist, is no help since he thinks property is theft. Besides moaning about her condition, e.g. she repeatedly bothers a priest with her "problem", there is precious little she actually does about it. The story did not convince me at all and a number of the characters seem superfluous.

The only parts I actually enjoyed were the flashbacks to Frederica's childhood which were delightful. Especially the one where she is kidnapped and manages to convince (a) her kidnappers that she is a communist too, and (b) her father that the kidnappers are nice people and that he should invite them for dinner.

All in all, a very mediocre film: ☆☆

Putting images in a post

Loading an image into a post on Blogger is easy: there's a fairly recognizable icon representing a picture above the editing window.
When you click that, you get to select an image, either locally or from a number of other sources.
Selecting a layout for the image.
Next you are asked to select a "layout" as shown on the left. If you choose "left" or "right", text will float around the image. If you choose "center", it will not. The text coming right after the image (in the HTML view) will "flow" next to the image. So the easiest way to insert images may be to first prepare all the text, then put your cursor where you want an image to appear and click the image icon.
Note that while uploading (from the HTML view) an image, you don't get the chance to put a caption for the image. You can do that afterwards by switching to the 'Compose' view and then clicking on the image.

By the way, the above screenshot was produced using the excellent gnome screenshot program which should be available on most Linux distributions. It certainly came with my Ubuntu 12.4 system.