Posts Tagged ‘Linux’

How to compile ffmpeg from source

Wednesday, June 9th, 2010

ffmpeg is a free and very powerful video encoding tool that is the basis of many other popular video encoding tools, with a command line interface.

Because of the frequent updates to ffmpeg and the codecs it uses, it is often best to download the latest source and compile it yourself. These updates usually include new features, bug fixes, support for new codecs and noticeable speed improvements. Most Linux distributions have outdated packages in their repositories so being able to compile the latest code from source is very useful

I use ffmpeg to output H.264 video with AAC audio in a MP4 container. I also occasionally convert audio to mp3. This means I will be compiling the following:

Preparing to compile on Linux

(If you already compile things on Linux, skip to the next section.)
First make sure that you have all of the tools and headers required to compile on Linux. On Debian based systems (including Ubuntu) you can usually run the following as root to get everything you need. First update your system. It is important that you get the latest kernel and the correct sources for it.

apt-get update
apt-get dist-upgrade

A reboot may be required here if your kernel updated.

Now get the compilers, tools and headers needed to compile software on Linux:

apt-get install build-essential linux-headers-`uname -r` git subversion yasm

NB: your “git” package may be called something like “git-core”

Compile ffmpeg (empty)

Because some libraries require ffmpeg to be installed, we will first install a blank copy with no additional codecs specified.

When I update, I often find that I need to recompile a blank version of ffmpeg before I can compile x264.

Download the latest ffmpeg source with subversion, configure, compile:

svn checkout svn://svn.ffmpeg.org/ffmpeg/trunk ffmpeg
cd ffmpeg
./configure
make

If this all worked with no errors (you can check with echo $?), run

make install

as root. Use su or sudo if you need to. I use su.

Compile faac and faad

Download and extract faac and faad from audiocoding.com.

As above, run

./configure
make
su -c 'make install'

on both libraries.

You can optionally do the same thing with LAME if you want to be able to encode mp3 audio.

Compile x264

Download and compile the latest x264 source with git:

git clone git://git.videolan.org/x264.git
cd x264
./configure
make
su -c 'make install'

Compile ffmpeg with the above codecs

Now that we have all of the codecs (x264, aac, mp3) successfully installed as libraries on the system, we can reinstall ffmpeg to incorporate these libraries. First go back to the ffmpeg source directory and clean off the previous compile.

cd ffmpeg
make clean

Now configure to enable all of the installed libraries and recompile:

./configure --enable-gpl --enable-nonfree --enable-pthreads --enable-libx264 --enable-libfaac --enable-libfaad --enable-libmp3lame
make

(if it tells you that –enable-libfaad is not an option, just remove it. It’s probably either been rolled into faac or ffmpeg has it built in)

If it all worked, run make install as root.

su -c 'make install'

Testing

As long as /usr/local/bin/ is on your $PATH environment variable, you should be able to just type ffmpeg to check that it is all installed. It should look something like this:

$ ffmpeg
FFmpeg version SVN-r23548, Copyright (c) 2000-2010 the FFmpeg developers
  built on Jun  9 2010 13:35:17 with gcc 4.4.4
  configuration: --enable-gpl --enable-nonfree --enable-pthreads --enable-libx264 --enable-libfaac --enable-libfaad --enable-libmp3lame
  libavutil     50.18. 0 / 50.18. 0
  libavcodec    52.75. 1 / 52.75. 1
  libavformat   52.68. 0 / 52.68. 0
  libavdevice   52. 2. 0 / 52. 2. 0
  libavfilter    1.20. 0 /  1.20. 0
  libswscale     0.11. 0 /  0.11. 0
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
 
Use -h to get full help or, even better, run 'man ffmpeg'

Using ffmpeg

I have written a small Python video encoding script to make encoding as easy as

enc -t cd file.vob

If anyone has any suggestions to improve this, such as any advantages of using shared libraries, please feel free to post these suggestions in the comments.

Remote recursive sha1sum with php

Wednesday, June 2nd, 2010

To calculate the SHA-1 sums, display them and make them available for download in a sums.gz file:

<?php echo(`find ./some_directory/ -type f | grep -v sums.gz | xargs sha1sum | gzip -c | tee sums.gz | zcat`); ?>

To check the sums:

<?php echo(`zcat sums.gz | sha1sum -c -`); ?>

How to install VMware Tools on a Linux guest

Saturday, June 13th, 2009

Installing VMware tools on Windows guests is easy. You just select to install it from your VMware client and it appears as a CD. You can then install it as you would any other software.

On Linux, it is a bit more difficult. Again, selecting to install it from your VMware client puts it in the CD drive. However, depending on your distribution, it may not be automatically mounted.

For an IDE virtual CD drive this mount, copy and then unmount would be something like

# mount -t iso9660 -o ro /dev/hda /media/cdrom
# cp /media/cdrom/VMwareTools-4.0.0-164009.tar.gz /root
# umount /media/cdrom

For a SCSI, SATA or SAS drive, this would be /dev/sda rather than /dev/hda. The “a” here assumes that it is the first drive. The 3rd SATA drive would be /dev/sdc, for example. In addition to this, partitions are labelled with numbers (such as /dev/sdc2 for the 2nd partition on the 3rd SATA drive) but this is not an issue for optical media such as CDs.

Now that the VMware tools archive has been copied off, it can be extracted.

# tar -xzf VMwareTools-4.0.0-164009.tar.gz

Most people add a “v” to the options for verbose output but not only do I find it pointless, it also slows down the extraction (particularly over SSH, particularly for archives with a large number of files). You do not need it to tell if an error occurred during the extraction, as it tells you anyway. The “x” option is to extract the archive (“c” would be to create one), the “z” means use gzip (“j” would be bzip2) and the “f” specifies the archive’s file name.

Before we install vmware tools, there are some dependencies that we must meet. I am doing this with Debian 5.0.1 AMD64 on a VMWare vSphere (ESX 4.0) host. If you do not meet these dependencies beforehand, you will probably have to break out of the installer with ctrl+c and re-run the whole thing several times, which is not good.

The “killall” tool must be installed. You can tell if it is by running which killall. If a path appears, it is installed. If nothing appears, it is not. In Debian, this is not installed by default and is in the “psmisc” package.

In addition to this, we will need to compile kernel modules during the configure stage of the installation. This is apparently not always required but I have not met a distribution yet on which it was not required to compile them from source (possibly RHEL?). This means that we will need gcc, make and the Linux kernel headers. To take care of these dependencies run

# apt-get update
# apt-get install psmisc gcc make linux-headers-`uname -r`

You may need to update your running kernel if it can no longer find the headers for your running kernel in the repository. You can do this with apt-get dist-upgrade.

Now that you have the dependencies installed, run ./vmware-install.pl. You probably just want to say “yes” to everything. (man yes)

Installing Debian

Saturday, February 28th, 2009

A couple of posts back, I touched briefly on how I personally install Debian Linux. With a little know how (this post), Debian becomes a far better advertisement for Linux than Ubuntu is, as Ubuntu is no faster than Windows. In fact, Ubuntu out of the box on my computer (Intel E8500, 4GiB of 800MHz RAM, etc) is actually a lot slower than Vista out of the box and as long as you can maintain Windows (i.e. not filling it up with crap), Windows stays in the lead for performance.

For the sake of this post, I will be installing Debian onto a 64bit VMware Server 2.0 virtual machine. If you do this, make sure that if you want sound, you add virtual sound hardware when creating the virtual machine, as I forgot (I’m sure previous versions of VMware added them by default).

Stage 1

Installing Debian is a 2 stage setup, as far as we are concerned. The first part is the actual installer, which takes us up to a working command line install of Linux, perfect for use as a server. The next part installs the applications we want to use as a desktop/laptop computer from the command line and ends in a working graphical desktop. Before we can start, we need to grab the netinst disk image and burn it to a CD (in the case of vmware, simply mount the iso file inside vmware).

To start the install, we turn the machine on with the disk in the drive and boot off it. This takes us to a menu, asking us if we want to do a text install, a graphical install (this menu option is new to Debian 5, though it was available from an “expert” mode before) or other options. I will choose the text install as that is what I am used to, though the graphical install should look quite similar.

Install menu

The Debian installer will prompt us for our language, keyboard layout, locale and so on. This is fairly self explanatory so I will leave it to the reader.

After this, we are asked how we want to partition the hard drive. The easiest thing to do is make sure that you have a block of unallocated space and just use the guided install for unallocated space. You can always go back and change things later if you want to, such as adding a /boot partition or removing a /swap partition. As I am using a new virtual machine with a completely blank disk, I am using the option of “Guided – use entire disk”. You don’t need to wipe your hard drive to install Debian and it is a good idea to set it up before installing with GParted if it is a disk that you already have data on. Just leave a hole in the partition table with GParted and install Debian to that unallocated space in guided mode.

If you are dual booting with another operating system such as Windows, I recommend that you add a 100MB ext2 /boot partition to the start of the drive. This is where GRUB will be installed to and having its own partition means that you can change to a different distribution of Linux or even remove Linux entirely and still keep the ever-useful GRUB on there, with tools like memtest86+.

Remember that you can probably only have 4 primary partitions on 1 drive and that you may want to use an extended partition containing 1 or more logical partitions for the 4th. This is a limit of IDE hard drives but most partitioning software and most SATA hard drives respect this limit.

Partitioning menu

When you are done, it will ask you twice to confirm that you want to write these changes to the disk:

Partitioning confirmation

It will then install the “base system” to the hard disk. This includes things like the Linux kernel and other packages essential for the operating system. If you get the business card CD rather than the netinst CD, you will need to set up your networking before this in the install because they are not on the business card CD and must be downloaded during the installation.

Installing base

After a few minutes, the base system will be installed and you will be asked to set up user accounts. First, you need to set a root password and confirm it. After this, you enter your full name, user name and user password. You will usually be using this user name and password for Linux, switching to root only when you need to make changes like install new software, usually by use of the “su -” command.

passwords

Assuming that we are on the netinst CD, we now set up networking so that we can install any additional packages. I suggest that you install using an Ethernet cable and DHCP, as wireless generally doesn’t work on Linux until you have installed some firmware such as ipw2200 (for Intel 2200 wireless adapters), b43 (for Broadcom wireless adapters), madwifi (for Atheros wireless adapters), etc.

Next, we pick a mirror to download the packages from. If ftp.uk.debian.org does not work for you for some reason or is slow, try using mirrorservice.org. If your ISP is Virgin Media, you may want to try debian.virginmedia.com for an even faster download.

mirror

This is where my guide gets different from any other guide you may see for installing Debian. At the task selection menu (tasksel), we deselect everything. These tasks tend to be bloated with random things that don’t even match the name of the task and even if you think one sounds like you want it (such as “Laptop”), you are probably better off installing the individual packages later on in stage 2 of this guide.

task selection

Because we have removed everything from the list, only a few small packages need to be downloaded and installed. To be honest, I don’t know why any have to be, as we didn’t select any tasks but that just goes to show how tasksel always gives you far more than you want.

task download

All that is left to do is to install the boot loader, GRUB, to the master boot record of the hard drive. This is what will give us a choice of operating system to run when we turn the computer on each time, assuming that we boot off this hard disk by default.

grub install

That’s it for stage 1 and it probably took under 5 minutes to install Debian Linux to your hard drive. Now we can take the CD out of the drive and reboot the machine to the hard drive that we just installed GRUB to.

Stage 2

When we reboot the machine, we see GRUB giving us a choice of Debian or Debian (single-user mode). Single user mode is similar to safe mode in Windows. You only really need to use it if the standard mode does not work. If you want, you can remove it from your /boot/grub/menu.lst later and just press “e” from GRUB to edit the normal mode and add “single” to the kernel line if you ever need single user mode. If you have Windows installed on your computer, it should also have an entry to let you use that and you can change it to the default option inside the menu.lst file if you want, as well as setting the timeout.

GRUB menu

A few seconds after selecting Debian from GRUB, the operating system has loaded entirely and we are at a login prompt. Because we did not install anything, it is all text based at the moment. This would be useful for a server but we probably want a graphical desktop. I’ve tried using a text-only desktop before and it isn’t pretty.

text login

You should log in with your main user account and password then, once logged in, run the command “su -” and enter the root password to gain super user access. In a Linux command prompt, password fields do not display anything when you are typing them, not even asterisks (*). When you are in a root shell, the prompt should change from a $ to a #.

The first thing to do is add the non-free and contrib repositories to apt. You can use an editor such as vim or nano to edit /etc/apt/sources.list to remove any CDROM lines and add “contrib non-free” to the main Debian repository. I don’t add it to the main deb-src or to updates and as of yet I haven’t worked out what “volatile” is, as it is new as far as I know. Here’s what the file should look like in vim, with the new text highlighted in red:

vim /etc/apt/sources.list

You should then run
# apt-get update
to download the latest package lists, followed by
# apt-get dist-upgrade
to upgrade all existing packages, such as the kernel to their latest versions. A reboot may be required here, which you can do easily with # reboot (you can shut down from the command line with # halt).

install list

Now we can begin installing the software to transform this command line into a graphical desktop, using # apt-get install <list of packages>. We want to install xorg, the graphics backend and frontend; gnome-core, the simple package for GNOME that comes with no large applications; gdm, the desktop manager for GNOME that lets us log in from a graphical interface among other things; alsa and alsa-oss for the sound and iceweasel for a web browser (this is firefox really). Other options are gnome-themes for some pretty window borders and gnome-backgrounds for some nice desktop wallpapers.

This will start to download and then install all of these packages, which may take a while depending on your internet speed but you are downloading them now rather than downloading the massive list of CDs or DVDs that you would have downloaded if you hadn’t have used the netinst or businesscard CDs, which will usually contain old packages that need updating anyway.

install progress

Once everything has successfully downloaded and installed, you can start gdm for the first time to turn on the graphical interface. This usually happens automatically on startup so you shouldn’t have to do it again. The command to start the gdm daemon is:
# invoke-rc.d gdm start

start gdm

As soon as this starts, you should see a graphical login prompt:

graphical login

From here you can choose alternative languages, switch between GNOME, KDE, Xfce and so on.

After you log in you should see a working graphical desktop and the installation is complete (ignore my broken sound, I forgot to add a sound card in vmware):

graphical desktop

This is what you would expect from a fresh install of ubuntu but you should notice that it is very snappy and probably takes 30 seconds or less between turning your computer on and having a completely loaded working desktop, without even having to wait for a busy cursor before you can start doing things.

You can watch videos with totem-gstreamer, browse the internet with iceweasel, check your emails with icedove (thunderbird), write code or text files with gEdit, view PDF documents with evince, edit Microsoft office documents with OpenOffice.org, etc.