Tag Archives: howto

How to convert videos to WebM with FFmpeg/AVConv

After lots of trial and error each time I convert a video to WebM, I finally got around to posting this so I don’t forget next time. In a nutshell, here’s the conversion command that works for me:

avconv -i myvideo.mp4 -acodec libvorbis -aq 5 -ac 2 -qmax 25 -threads 2 myvideo.webm

What is this doing? Let’s go through it bit by bit. Assuming we have a video called myvideo.mp4, the simplest way to convert to WebM is with this little line:

avconv -i myvideo.mp4 myvideo.webm

Easy, but the quality will likely be rubbish hence the use of a few flags. The flags can be divided into three sorts: audio, video and the transcoding itself.

Audio flags

FFmpeg/AVConv outputConcentrating on the audio first, we should specify the audio codec which for WebM is Ogg Vorbis: -acodec libvorbis

In later versions the audio codec is Ogg Vorbis by default but personally I specify it just in case.

The quality can be adjusted with the -aq flag from -1 to 10 with a higher number meaning better quality. I’ve found 4 or 5 to be more than adequate.

The number of channels, e.g. mono (1) and stereo (2), is controlled with the -ac flag.

Video flags

Moving on to the video quality and thankfully it’s nice and simple. Like the audio, we can specify a quality level. With the libvpx library used for WebM, this is actually a quantization level, set with the -qmin and -qmax flags ranging from 0 to 51. In my tests, setting qmin makes no difference so I ignore it. Setting qmax effectively controls the maximum compression that will be applied to each frame. In other words, a higher qmax means higher compression, which results in lower quality and smaller file size. Obviously you should adjust this to whatever’s best for your circumstances, but I’ve found 25 to be a good starting point.

Note that with both the audio and video, setting flags for bitrate (-ab for audio, -b for video) makes little or no difference. Instead, setting quality flags indicates the use of a variable bitrate.

Transcoding flags

Finally, I tend to also use the -threads flag. This simply sets how many CPU threads to use when transcoding. If your PC has multiple cores then a higher number means faster processing but with less spare capacity for running other programs. Incidentally it’s also possible to do 2-pass encoding with WebM using the -pass flag.

FFmpeg naming confusion

Note that due to what seem to be political reasons, using the ffmpeg command in Ubuntu results in a sad message.

Was:

*** THIS PROGRAM IS DEPRECATED ***
This program is only provided for compatibility and will be removed in a future release. Please use avconv instead.

Now:

ffmpeg: command not found

It turns out that FFmpeg is very much alive, as is Libav (avconv), both with similar goals but with slightly different ways of doing it. I don’t follow the details and arguments but for practical purposes, using either the ffmpeg or avconv command is fine for converting videos to WebM with the above flags (at the time of writing). Of course, this may change eventually but for the sake of regular users, I hope not.

Unfortunately whatever FFmpeg/Libav disagreement there was has resulted in ffmpeg being removed from Ubuntu and possibly other Linux distros. For the transcoding commands in this post at least, the parameters are the same so if you have problems using avconv try with ffmpeg and vice versa.

Static social media “share” buttons without JavaScript

Hand-shaped cursorSometimes you want a “share” link in your website or email newsletter but you don’t want the extra JavaScript and iframes that come with the standard social media buttons. Here’s how to do it, giving your users faster page loading times and a bit more privacy.

Twitter

Firstly Twitter, who like to make it easy for us. It’s possible to use twitter.com/home or twitter.com/share but the most useful sharing URL is this:

https://twitter.com/intent/tweet?
  • Parameters: url, text
  • Encode parameters? Yes

The difference is that the user will see a friendly “Share a link with your followers” heading rather than the usual “What’s happening?”. Not only that, but the text within the Tweet is highlighted to make it immediately editable. Nice touch.

Example

<a href="https://twitter.com/intent/tweet?text=Static%20social%20media%20%22share%22%20buttons&url=http%3a%2f%2fdaniemon.com%2fblog%2fstatic-social-media-share-buttons%2f">
    Share on Twitter
</a>

Try it out here: Twitter buttonShare on Twitter

Documentation


Facebook

Despite having an official “Share” button in the past, Facebook have now deprecated it in favour of the ubiquitous “Like” button. For backwards compatibility, however, the “Share” URL still works:

https://www.facebook.com/sharer/sharer.php?
  • Parameters: u (URL)
  • Encode parameters? Yes

Example

<a href="https://www.facebook.com/sharer/sharer.php?u=http%3a%2f%2fdaniemon.com%2fblog%2fstatic-social-media-share-buttons%2f">
    Share on Facebook
</a>

Try it out here: Facebook buttonShare on Facebook

Documentation


LinkedIn

Apparently sharing links on LinkedIn is very good for SEO so this could be an important one to include. The base URL is:

http://www.linkedin.com/shareArticle?
  • Parameters: url, title, summary
  • Encode parameters? Yes

Example

<a href="http://www.linkedin.com/shareArticle?url=http%3a%2f%2fdaniemon.com%2fblog%2fstatic-social-media-share-buttons%2f&title=Static%20social%20media%20%22share%22%20buttons&summary=How%20to%20build%20social%20media%20sharing%20links%20without%20JavaScript%20and%20iframes.">
    Share on LinkedIn
</a>

Try it out here: LinkedIn buttonShare on LinkedIn

Documentation


Pinterest

The relatively new kid on the block is Pinterest. Note that for it to work properly, you should include the media parameter and point it to an image at least 750 pixels wide. The base URL is:

http://pinterest.com/pin/create/button/?
  • Parameters: url, description, media
  • Encode parameters? Yes

Example

<a href="http://pinterest.com/pin/create/button/?url=http%3a%2f%2fdaniemon.com%2fblog%2fstatic-social-media-share-buttons%2f&description=Static%20social%20media%20%22share%22%20buttons&media=http%3a%2f%2fdaniemon.com%2fblog%2fwp-content%2fuploads%2f2012%2f09%2fstatic-social-media-buttons.png">
    Share on Pinterest
</a>

Try it out here: Pinterest buttonShare on Pinterest

Documentation


Google+

Let’s not forget Google who have a bookmarking service, however this has now largely been usurped by Google+ which uses this URL:

https://plus.google.com/share?
  • Parameters: url, hl (human language, e.g. “en”, “ja”, etc. – optional)
  • Encode parameters? Yes

Example

<a href="https://plus.google.com/share?url=http%3a%2f%2fdaniemon.com%2fblog%2fstatic-social-media-share-buttons%2f">
    Share on Google+
</a>

Try it out here: Google+ buttonShare on Google+

Documentation


Hatena Bookmarks

Hatena Bookmarks is a popular bookmarking site in Japan, particularly among the tech crowd. It uses this URL:

http://b.hatena.ne.jp/entry/
  • Parameters: None – just append your target URL
  • Encode parameters? No

Example

<a href="http://b.hatena.ne.jp/entry/http://daniemon.com/blog/static-social-media-share-buttons/">
    Share on Hatena Bookmarks
</a>

Try it out here: Hatena Bookmarks buttonShare on Hatena Bookmarks

Documentation

Other similar resources

How to wipe a hard drive with Linux

An opened hard driveI have an old hard drive I want to throw away but I don’t want any remaining photos, financial documents or other personal data getting in the wrong hands. In other words, I want to completely and securely wipe the drive. With Linux there are several options and after a bit of research, here’s what I consider the best way.

Firstly, programs such as fdisk, cfdisk or GParted are not sufficient to fully erase data—they just edit a drive’s partition table. What we want is something more thorough. The standard way to do this is to overwrite data on a drive with randomly-generated data. There are several command-line programs to do this (see below for a comparison) but I’ve chosen shred. Its default is three passes (overwriting all data three times) which I’m comfortable with, but you can specify more if you like, e.g. -p 10 for 10 passes.

WARNING! You don’t need me to tell you that accidentally erasing the wrong data could cause you big problems. Please be careful and check, then double-check each command and especially drive name before pressing Enter.

Step 1.
Plug in the hard drive and find its name (sdb, sdc, etc.). Use

dmesg

and look for something like sdb: sdb1

Step 2.
Unmount each partition of the drive (sdb1, sdb2, etc.), e.g.

sudo umount /dev/sdb1

Step 3.
Type the following command for writing random data to the drive three times (default). DON’T PRESS ENTER.

sudo shred -f -v /dev/[your drive name]

Step 4.
Double-check that the drive you’ve specified is the correct one to wipe. OK, now you can press Enter.

Programs for deleting data

badblocks

Purpose: “Search a device for bad blocks”

sudo badblocks -w -t random -p 1 -s /dev/sdb

Summary: A single (-p 1) overwrite (-w) with random data (-t random), showing progress (-s).
Time for 6GB: 16 mins 20 secs

dd

Purpose: “Convert and copy a file”
Notes: You can’t see your progress or specify multiple passes.
Got stuck with faulty drive.

sudo dd if=/dev/urandom of=/dev/sdb

Summary: A single overwrite with random data.
Time for 6GB: 1 hr 5 mins

shred

Purpose: “Overwrite a file to hide its contents, and optionally delete it”

sudo shred -f -v -n 1 /dev/sdb

Summary: Force (-f) a single (-n 1) overwrite with random data, showing progress (-v).
Time for 6GB: 10 mins 6 secs

wipe

Purpose: “Securely erase files from magnetic media”
Notes: Not in default Ubuntu (sudo apt-get install wipe). After running this, I had a few errors running the other commands on the same drive.

sudo wipe -kD -i -q -Q 1 /dev/sdb

Summary: A single (-q -Q 1) overwrite with random data, showing progress (-i), keeping the device’s inode intact (-kD).
Time for 6GB: 9 mins 37 secs

Further information

Smashing a hard disk with a hammerWiping the drive as explained above is good enough for most purposes but forensic experts have amazing skills and tools at their disposal. It may theoretically still be possible to access some of your deleted data. The most secure way to prevent people accessing any data left on your drive is physically drilling, crushing and breaking up the drive, then disposing of the parts in various locations. And encrypting the drive in the first place with TrueCrypt, for example, is also a good idea.

Related links

Readability for Android

So you want Readability bookmarklets on your Android device, huh? Not so fast! At the time of writing, it’s not possible to access the official Readability bookmarklets on Android but there is a workaround. Here’s how to do it in three easy steps.

Step 1: Copy the Readability script

In Opera Mobile or Mini (I don’t think this works in other browsers), select and copy the code below:

javascript:((function()%7Bwindow.baseUrl%3D'http%3A//www.readability.com'%3Bwindow.readabilityToken%3D''%3Bvar%20s%3Ddocument.createElement('script')%3Bs.setAttribute('type'%2C'text/javascript')%3Bs.setAttribute('charset'%2C'UTF-8')%3Bs.setAttribute('src'%2CbaseUrl%2B'/bookmarklet/read.js')%3Bdocument.documentElement.appendChild(s)%3B%7D)())

Step 2: Create a bookmark

Open the Bookmarks menu and click on the Add button (don’t click Save yet).

Step 3: Edit the bookmark

Screenshot of Opera Mobile Readability bookmarklet

  1. Change the title to Readability (or something similar).
  2. Delete the address and paste in the code copied from above.
  3. Click Save.

Voila! You should now be able to go to any article or blog post, click the Readability bookmarklet and enjoy a better reading experience.

How to create a DVD-compatible video in PiTiVi

Screenshot of PiTiVi settingsI wanted to edit one of my geeky songs in PiTiVi and burn it to DVD. After searching around and trying a few things it seemed to work, so for reference, here’s how I did it.

Important: Save the PiTiVi project before you start, as I’ve found it to be prone to crashing.

  1. Once the video and audio are ready, click “Render project”
  2. Click “Choose File” and enter a filename with the extension .mpg
  3. Click “Modify” and select either 576p PAL or 480p NTSC depending on your region
  4. Change the export container to “FFmpeg MPEG-2 PS format (DVD VOB)”
  5. Change the audio codec to “FFmpeg ATSC A/52A (AC-3)”
  6. In the audio settings, change the Bit Rate to 256000
  7. The video codec should be fixed at “FFmpeg MPEG-2″
  8. In the video settings, change the Bit Rate to 6548000
  9. Click “OK”, then “Render”

You can now use a program such as Bombono to burn straight to DVD, or DeVeDe (slightly better, in my opinion) to create an ISO which you can burn to DVD with Brasero or other tool.

Incidentally, before rendering I used “noise removal” and “normalize” in Audacity to create a clean soundtrack. I imported this into PiTiVi and removed the original audio. Result: success!

How to format PDFs for the Kindle

A PDF shown on a Kindle screenLike many, I love my Kindle. What a wonderful piece of technology. OK, the browser’s a bit slow and not powered by Opera but even so, having a world-wide 3G connection is fantastic, as is the great reading experience. However, some PDFs don’t look so good on that small screen. Here are some (almost) ideal settings that I’ve found make PDFs more readable for the Kindle.

I’ve tried to find the optimal PDF settings using lots of trial and error with OpenOffice/LibreOffice. The result is not perfect, in particular on pages with very few lines of text, so if you have any suggestions for improvement please leave a comment below. Anyway, enough talk…

Best PDF settings for the Kindle

Page size:

  9.1cm x 12.3cm

Page margins:

  Left: 1cm
  Right: 1cm
  Top: 1cm
  Bottom: 2cm

Font size:

  9pt (I recommend Georgia or other serif font)

Line spacing:

  120% (or 12.5pt)

There you go – nice and simple but it makes a big difference. Don’t forget to add page breaks for each new section or chapter. Of course, you can also add headers but I’d avoid footers because of the Kindle’s progress bar at the bottom of the screen.

And finally, here’s a pre-made template you can download and use:

How to show Japanese text in Evince

A quick tip that might help others (or me, the next time I forget)…

I found that Evince, Ubuntu’s default PDF viewer, doesn’t display Japanese characters, at least on my non-Japanese system. After a quick search it seems the answer is nice and simple – install the poppler-data package.

So, either use the Synaptic Package Manager or the following terminal command:

sudo apt-get install poppler-data

And that’s it!

This should work for displaying Chinese and Korean characters as well.

How to create Ogg videos in Linux (with GUI)

Following up on my previous post about converting videos to Ogg Theora, there’s also an easy way for those who like a GUI.

Screenshot of the OggConvert programAlthough it’s possible to use VLC for Ogg encoding, I had problems with the audio and video being out of sync. Fortunately I found another way – enter OggConvert.

Written in Python by Tristan Brindle, there are packages for the major Linux distributions as well as a version for Windows. Not only that, it’s open source (GNU LGPL), preserves metadata when converting and is extremely easy to use:

  1. Fire up OggConvert
  2. Select the source video (or audio) file
  3. Click convert
  4. Voila! Enjoy your Ogg video

By default, the target file is saved in the same directory and with the same name as the source file but with an .ogg or .ogv extension. The default quality settings seem a bit low but should be adequate for most purposes.

When done, your Ogg video is now ready to use in your HTML5 pages:

<video src="video.ogg" width="320" autobuffer controls>
  <p>Sorry, your browser can't play this video but you can
  <a href="video.ogg">download it here</a>.</p>
</video>

How to create Ogg videos in Linux

Ogg Theora is the codec most supported by modern HTML5-capable browsers, so how can you convert your videos to it? Pretty easily, it seems.

There’s a cross-platform command-line application called ffmpeg2theora which does a good job of converting videos to Ogg Theora. Usage couldn’t be simpler:

ffmpeg2theora input.mpeg

If the audio is out of sync with the video, either try adding the –sync option or convert it to MPEG format using ffmpeg first, and then use ffmpeg2theora.

For those all-important tags (except ‘comment’) you can use parameters like so:

ffmpeg2theora input.mpeg --title "My spiffing video" --artist "Daniel" --license "http://creativecommons.org/licenses/by-nc-sa/3.0" --date "2010" --organization "Opera Software" --location "Tokyo, Japan" 

Or alternatively there’s oggz, available in the oggz-tools package in Ubuntu:

oggz-comment input.ogv TITLE="My spiffing video" ARTIST="Daniel" LICENSE="http://creativecommons.org/licenses/by-nc-sa/3.0" DATE="2010" ORGANIZATION="Opera Software" LOCATION="Tokyo, Japan" COMMENT="Best viewed on a screen" -o output.ogv

To embed your converted video in an HTML5 page, use something like this:

<video src="video.ogv" autobuffer controls>
    <p>Your browser can't display the open Ogg Theora codec but you can download it from here: <a href="video.ogv">Video</a></p>
</video>

N.B. For Mac, there’s Exom which has ‘HTML5′ as one of its output options (thanks Bruce).

UPDATE: If you’d prefer to not use the command line, there’s a GUI alternative for easy Ogg conversion.

How to install Truecrypt on Fedora

I’ve tried on several versions of Fedora but never really been able to install Truecrypt. Either it’s resulted in lots of errors or I’ve given up after getting tired of fruitless searching and reading.

At last I’ve found a guide that’s not too long and works!

It’s written for Truecrypt 6.2 on Fedora 11 but worked perfectly for me with Truecrypt 6.3a on Fedora 12:

penguinenclave.blogspot.com/2009/07/truecrypt-62-install-guide-for-fedora.html

Of course, it would be best if Truecrypt was available in Linux repositories but from what I understand, the way it’s written makes it very difficult for distros to package and maintain. Even so, the greatness of Truecrypt still makes it worth doing yourself.