The best way to dual boot Linux and Windows

I’m going to explain how to configure your PC in order to dual boot Linux and Windows Vista or Windows 7, assuming Windows is already installed.

If you already have Windows Vista or Windows 7 installed in a single partition, then the method described below is the best way of ensuring that your Windows installation will still be bootable even if the Linux installation or GRUB bootloader become damaged in future.

Furthermore, if your PC has a hidden factory restore partition for Windows, the method described below is the best way of ensuring that you will still be able to recover Windows in future using that hidden partition. The reason why this is the best method is because it does not alter the contents of the MBR (Master Boot Record), which, in addition to the Windows bootloader, may contain code created by the manufacturer to boot the hidden factory restore partition instead of the Windows Vista/7 partition if it detects that you pressed a defined key or keys while the PC is booting. If the contents of the original MBR are overwritten — even if it is only by using the Bootrec.exe tool on a standard Windows Vista/7 Installation DVD — then the manufacturer’s code will no longer be in the MBR and so it will no longer be possible to boot the hidden factory restore partition by pressing the key(s) specified by the manufacturer.

Note also that some Windows applications can make a PC using GRUB 2 unbootable if GRUB 2 is installed in the MBR. See the blog article Windows applications making GRUB 2 unbootable for details. The method described below does not install any GRUB 2 code in the MBR, and thus it will avoid this problem.

So, to reiterate, the method described below avoids two potential major problems: 1) it avoids the possibility of making a Windows factory restore partition unusable; 2) it avoids the possibility of Windows applications overwriting some of the GRUB 2 code.

If you already have Windows installed, the procedure to prepare the PC and install Linux is divided into the following three main stages.

Stage 1: Reduce the size of the existing Windows partition

Do not use the partition managers Parted, GParted or KDE Partition Manager to reduce the size of the existing Windows partition, or you will damage your Windows installation.

Step A: Use the Windows defragmenter or a third-party defragmenter to defragment the Windows partition.

Step B: Use Windows’ Disk Management to shrink the partition (see Resize a Partition for Free in Windows 7 or Vista). The problem with this method is that Windows will only shrink the partition until it reaches the MFT (Master File Table), which means there may be free space in the Windows partition that you would have wanted to use in a Linux partition but cannot. However, if you are satisfied with the resulting size of the Windows partition then you can skip Step C below and proceed directly to Stage 2.

Step C: If you cannot shrink the Windows partition to the size you want by using Windows’ Disk Management, then you need to move the MFT (see Working Around Windows Vista’s “Shrink Volume” Inadequacy Problems). Basically you need to download a tool such as PerfectDisk (it has a free trial period) that will defragment the Windows partition and move the MFT in the process.

Stage 2: Create the new partitions for Linux

Boot a LiveCD or LiveDVD which has the partition editor GParted on it, and run GParted to create and format partitions for Linux (for example /, /boot, /home and swap) in the free space you created in Stage 1 above. I prefer to use GParted to create the partitions before running the Linux distribution’s installer, rather than using a partitioning tool integrated into the Linux installer itself. GParted has more functionality and enables better control over the partitioning process than a partitioning tool incorporated in the Linux distribution’s installer. Also, I have found on some occasions that installation of some Linux distributions fails if using the partitioning tool integrated in the Linux installer, but is successful if the partitions were created beforehand.

SystemRescueCD is a good LiveCD to use for running GParted, but any LiveCD/DVD which includes GParted will suffice. You can download the ISO file from the SystemRescueCd Web site http://www.sysresccd.org/Main_Page.

It is only possible to have up to four primary partitions on a hard disk, so, depending on the Linux partitions you decide to create, it is possible that you will need to create an extended partition containing logical partitions. If, for example, your hard disk has a hidden Windows factory restore partition and a partition for Windows itself, and you decide you want to have /boot and /home on separate partitions to the root directory, then one of many possible partitioning schemes would be:

– the hidden Windows factory restore partition (a Primary partition)

– the Windows partition (a Primary partition)

– the /boot partition (a Primary partition)

– an Extended partition containing the following Logical partitions:

– the swap partition

– the / (root) partition

– the /home partition

For example, on my main laptop I created and formatted the partitions /dev/sda3 to /dev/sda7 as follows:

/dev/sda1 – the hidden Windows factory restore partition

/dev/sda2 – the Windows C: drive partition

/dev/sda3 – the Linux /boot partition

/dev/sda4 – an Extended partition containing the following Logical partitions:

/dev/sda5 – the swap partition

/dev/sda6 – the / (root) partition

/dev/sda7 – the /home partition

Note that it is not essential to have /boot, / and /home in different partitions; they could all be in the same partition. However, I would recommend that at least /home be given a separate partition, so that personal files (videos, music, documents, etc.) would not be overwritten if you were to re-install Linux at some point.

You are free to choose your own partitioning scheme, but I create the Linux partitions in the order shown above so that I can assign specific sizes to the /boot, swap and / partitions, leaving the remainder of the disk space free for the /home partition.

If you do decide to put /boot on its own partition, it only needs to be small (I make it around 100 MB). On my laptop the /dev/sda4 Extended partition is around 143 GB so I created a partition of 60 GB for / (root) — which is more than ample — and a partition for /home using the rest of the available space on the laptop’s 320 GB hard disk. You’ll have to decide on the size of these partitions based on the size of the hard disk and the root partition’s space requirements recommended by the Linux distribution’s developers.

If you do not need Linux to be able to hibernate (‘suspend-to-disk’) then a swap partition of 512 MB would be sufficient for the typical desktop PC, especially when the latest PCs come with 4 GB or more RAM. Some people do not even bother having a swap partition at all with such large amounts of RAM. But if you do want Linux to be able to hibernate then try to find out if the Linux kernel which you will be installing was compiled with in-kernel LZO compression algorithm support (LZO compression algorithm support must not be compiled as a module). If the kernel was compiled with in-kernel LZO compression algorithm support then make the size of the swap partition the same as the RAM size, which is more than enough. For example, as my laptop has 4 GB of RAM I created /dev/sda5 as a 4 GB partition. If LZO compression algorithm support was not compiled into the kernel and you do not know how to rebuild the kernel after installing Linux then you will have to make the swap partition bigger than your RAM size (my guess would be at least 1.5 times the size to be on the safe side).

As to the choice of file system, I format /boot, / and /home as ext4 but you can choose a different native Linux file system if you want. You cannot use Windows’ FAT or NTFS for these Linux partitions.

Stage 3: Install the GRUB bootloader and Linux

Boot Windows Vista/7 and download the freeware tool EasyBCD from the NeoSmart Web site (you might like to make a small donation, to help the developer). You will need to download EasyBCD version 2 and upwards if you want to install a Linux distribution that uses GRUB 2; it is available to download from http://neosmart.net/dl.php?id=1. If you are going to install a Linux distribution that uses GRUB Legacy then the current version of EasyBCD should work too. Follow the instructions on the EasyBCD site on how to install Linux (http://neosmart.net/wiki/display/EBCD/Linux). N.B. You will reach a point in the Linux installation process when the Linux installer allows you to make a choice of where to install the GRUB bootloader: in the MBR (Master Boot Record) of the HDD or in the first sector of the Linux boot partition. You must select the latter, not the MBR. If the Linux installer offers you the option of installing GRUB to e.g. /dev/sda then that would install GRUB in the MBR, because no partition is specified. Make sure you specify the partition that will contain the /boot directory (/dev/sda3 in my particular case). If you created a separate partition for /boot then GRUB will be installed on that separate partition, whereas if you did not create a separate partition for /boot then GRUB will be installed on the partition containing the root directory.

The result of Stage 3 will be that, when you boot your PC, the Windows Vista/7 bootloader will load the GRUB bootloader, and the GRUB bootloader will load Linux. This process is called ‘chainloading’. When you boot the PC you will first see the Windows bootloader menu, and you can select Windows or Linux from it. If you select Linux then you will see the GRUB menu and you can select Linux from that menu, which will boot the distribution.

Whilst chainloading GRUB 2 from the Windows bootloader is more long-winded than booting GRUB 2 directly, you can now use Linux and Windows safe in the knowledge that GRUB 2 will not be overwritten by any Windows applications, and that a Windows factory restore partition will be bootable in an emergency.

A corner case

EDIT (June 21, 2012): A note about a ‘corner case’. This will only apply to a very small minority of users, and then probably only to some users of Gentoo Linux (in Gentoo Linux it is possible to have both /boot/grub/ and /boot/grub2/ simultaneously). If you select ‘GRUB2’ in the pull-down menu in the current version of EasyBCD (2.1.2), EasyBCD actually searches the sub-directories under /boot/ to find the GRUB 2 core.img file, and puts an entry in the Windows BCD pointing directly to that file, i.e. EasyBCD ignores the boot sector of the Linux boot partition. Therefore, when you select ‘Linux’ in the Windows boot manager’s menu, the Windows boot manager does not launch the GRUB code in the boot sector of the partition on which /boot/ resides, it launches the core.img file directly. Now, if your boot partition happens to have both the sub-directories /boot/grub/ and /boot/grub2/, and they both contain a GRUB 2 file core.img, EasyBCD 2.1.2 will create a BCD entry pointing to the one under /boot/grub/ rather than the one under /boot/grub2/. This may or may not be what you want. If you want GRUB 2 to use the core.img under /boot/grub2/, the workaround in EasyBCD 2.1.2 is to select ‘GRUB Legacy’ in the EasyBCD pull-down menu and specify the boot partition. This is counter-intuitive, but it forces EasyBCD to create an entry in the BCD that points to the boot sector of the boot partition instead of the core.img file in a sub-directory of /boot/ on that partition. The GRUB 2 code in the partition’s boot sector will then execute the core.img file in the required sub-directory. This workaround only works if the Linux boot partition is on the same drive as the Windows partition. The developer of EasyBCD will be updating EasyBCD to look for a core.img file in /boot/grub2/ before /boot/grub/, rather than the other way around, if they both exist when you select ‘GRUB2’ in the EasyBCD pull-down menu.

EDIT (September 5, 2012): The EasyBCD developer has fixed EasyBCD a) to cater for core.img being either in /boot/grub2/i386-pc/ or in /boot/grub/, and b) to look for core.img in /boot/grub2/ before looking for it in /boot/grub/. The version with the fix, EasyBCD 2.2 Beta – Build 179.exe or later, can be downloaded from NeoSmart Technologies’ EasyBCD Support Forum. I’ve tried it and it works!

EDIT (January 28, 2015): In October 2013 the GRUB 2 directory in Gentoo was changed from /boot/grub2/ to /boot/grub/, so the above two comments are redundant and you can ignore them.

Make sure you read the EasyBCD FAQ page

EDIT (September 5, 2012): Make sure you read the EasyBCD FAQ page. For example, it states that, to date, EasyBCD does not support the EFI/UEFI, only the traditional PC BIOS.

EDIT (December 18, 2013): According to the NeoSmart Knowledgebase: “As of EasyBCD 2.2, EFI/UEFI and GPT disks are fully supported, but some options may not be compatible with EFI machines.“. I have not tried Version 2.2 or above of EasyBCD with EFI/UEFI and GPT disks, so cannot corroborate this.

EDIT (June 26, 2015): Make sure the partition containing the Linux boot directory is a Primary partition, not a Logical partition. This is because of a Windows limitation.

WINE tips: How to associate IrfanView with an image file type in Linux

In my previous post I showed how to install IrfanView with a WINEPREFIX. This time I’m going to show you how to associate IrfanView with a particular image file type so that IrfanView launches and opens a file of that type when you double-click on the file.

IrfanView is an excellent free (but not open-source) application for Windows, and I’ve been using it in Windows since 1997. But there are so many excellent image viewing and editing applications native to Linux that I’ve never needed or wanted to install IrfanView under WINE. GQview, Gwenview, KolourPaint and the GIMP have so far covered all my needs. Nevertheless, IrfanView is my favourite utility for viewing and manipulating image files in Windows, and I can understand why some people may want to run it in Linux.

The usual method of setting file associations in Linux does not work for IrfanView. For example, if you use KDE’s Kickoff > System Settings > File Associations to associate JPG files with IrfanView, IrfanView will indeed launch if you double-click on a JPG file, but you’ll be left staring at an empty IrfanView window, as if you had simply launched IrfanView directly.

However, there is a straightforward work-around for this problem, and it involves the use of a Bash script. You associate the file type with a Bash script that you create specifically to accept a Linux parameter for the file name and convert it to a format that WINE can pass to IrfanView when launching it. Here is how to do it.

I created a text file ~/launch_IrfanView.sh containing the following Bash script:

#!/bin/sh
File="z:"${1//\//\\}
wine "/home/fitzcarraldo/.wine-irfanview/drive_c/Program Files/IrfanView/i_view32.exe" $File

I used a WINEPREFIX of $HOME/.wine-irfanview/ when I installed IrfanView, so replace “.wine-irfanview” with “.wine” if you installed IrfanView without a WINEPREFIX (i.e. if it is installed in the ~/.wine/ directory tree).

I made the file executable:

$ chmod +x /home/fitzcarraldo/launch_IrfanView.sh

I then edited the command for ‘IrfanView 4.27’ in ‘Kickoff’ > ‘System Settings’ > ‘File Associations’ > ‘image’ > ‘jpeg’ > ‘Application Preference Order’ | ‘IrfanView 4.27’ > ‘Edit’ > ‘Application’ to be as follows:

/home/fitzcarraldo/launch_IrfanView.sh %f

That works fine: when I double-click on a JPG file in a Dolphin window, IrfanView launches and opens the file.

Of course, if you want IrfanView to be the default viewer for PNG files then you would perform the aforementioned steps for ‘Kickoff’ > ‘System Settings’ > ‘File Associations’ > ‘image’ > ‘png’.

As I wrote initially, there are several excellent image viewers and editors for Linux, so I did not keep IrfanView as my default image viewer in Linux. But I have left IrfanView installed under WINE, as I may yet want to use it under Linux at some point.

WINE tips: Giving each Windows application its own environment

I’m going to tell you about the useful WINEPREFIX environment variable, and, as a bonus, you’ll also find out how to install Office 2007 and IrfanView in WINE, plus how to configure file associations so that double-clicking on a .doc and/or .docx file will launch Word 2007 and open the file.

BACKGROUND

I’ll wager most WINE users have not heard of the WINEPREFIX environment variable and are installing all Windows applications in the ~/.wine/ directory tree. Until a couple of months ago that’s what I was doing too, but these days I install all Windows applications in WINE using this clever WINE feature.

Some WINE users will know that it is possible to use the ‘Add application…’ button on the Applications Settings page (the Applications tab) in the winecfg window to specify per-application libraries (DLLs) and graphics settings. For example, a user could use this feature in order to set an override of riched20 (native) for Office 2007 but not for, say, IrfanView. This WINE feature allows you to ‘ring fence’ applications from each other, avoiding the possibility of incompatible DLLs and graphics settings interfering with the working of another application. However, WINE still keeps directories and files for all the Windows applications in the ~/.wine/ directory tree.

The WINEPREFIX environment variable enables you to go a stage further. By using a WINEPREFIX you can install each Windows application (or a group of applications) in its own, completely separate environment. For example you could install IE7 in ~/.wine-ie7/, IrfanView in ~/.wine-irfanview/, World of Warcraft in ~/.wine-wow/, Word 2007 in ~/.wine-word2007/ and so on. You could even have different versions of the same Windows application installed simultaneously if you wanted. Not only does a WINEPREFIX allow you to keep an application’s DLLs and all settings completely separate from those of other applications, thus avoiding or preventing conflicts, it means that uninstalling a Windows application becomes as simple as:

$ rm -rf ~/.wine-visio5

You no longer need to hope that wine uninstaller supports the application, or that the application’s uninstaller will work if launched directly via WINE.

If winecfg’s Applications Settings allows you to partially isolate an individual application, the use of a WINEPREFIX allows you to put an application into solitary confinement!

HOW TO USE A WINEPREFIX

Let’s look at a couple of examples.

Installing Office 2007 (including SP2) with its own WINEPREFIX:

$ cd
$ export WINEPREFIX=$HOME/.wine-office2007
$ export WINEARCH="win32" # If WINE was built for 64-bit and 32-bit Windows and you have 32-bit Office 2007.
$ winecfg
$ cd .wine-office2007/drive_c/
$ wget http://www.kegel.com/wine/winetricks
$ chmod +x winetricks
$ cp ~/Downloads/X12-30107.exe . # Office 2007 installer from Microsoft.
$ cp ~/Downloads/office2007sp2-kb953195-fullfile-en-us.exe . # Office 2007 SP2.
$ cp ~/Downloads/officelp2007sp2-kb953195-fullfile-en-us.exe . # Office 2007 SP2 language pack.
$ wine X12-30107.exe # Install Office 2007. If you have an Office 2007 installation DVD then use "wine /path/to/setup.exe" instead.
$ winecfg # Set up riched20.dll as per instructions in Wine AppDB for Office 2007 (it must be "Native (Windows)").
$ ./winetricks msxml3 # Install msxml3 as per instructions in Wine AppDB for Office 2007, to stop SP2 crashing.
$ wine office2007sp2-kb953195-fullfile-en-us.exe # Install Office 2007 SP2.
$ wine officelp2007sp2-kb953195-fullfile-en-us.exe # Install Office 2007 SP2 language pack.
$ ./winetricks wsh56js # Install Windows Scripting Host for the thesaurus.
$ ./winetricks allfonts # Install all the TTF fonts.
$ winecfg # Set usp10 to 'native,builtin' for the equation toolbar in Word.
$ ./winetricks fontsmooth-rgb # Install font smoothing.

I have a KDE Kickoff menu entry Applications > Wine > Programs > Microsoft Office > Word containing the following command:

env WINEPREFIX="/home/fitzcarraldo/.wine-office2007" WINEARCH="win32" wine C:\\windows\\command\\start.exe /Unix /home/fitzcarraldo/.wine-office2007/dosdevices/c:/users/fitzcarraldo/Start\ Menu/Programs/Microsoft\ Office/Microsoft\ Office\ Word\ 2007.lnk

The above command might look complicated, but WINE creates it automatically during the installation of Office 2007. WINE also creates similar Kickoff Menu entries for the other Office 2007 applications.

Having installed Office 2007 using a WINEPREFIX, if I wanted to launch e.g. Word from the command line I’d need to enter the following commands:

$ export WINEPREFIX=$HOME/.wine-office2007
$ export WINEARCH="win32"
$ wine $WINEPREFIX/drive_c/Program\ Files/Microsoft\ Office/Office12/WINWORD.EXE

instead of the following if I had not installed Office 2007 using a WINEPREFIX:

$ env WINEARCH="win32" wine $HOME/.wine/drive_c/Program\ Files/Microsoft\ Office/Office12/WINWORD.EXE

Installing IrfanView 4.27 with its own WINEPREFIX:

$ cd
$ export WINEPREFIX=$HOME/.wine-irfanview/
$ export WINEARCH="win32"
$ winecfg
$ cd .wine-irfanview/drive_c/
$ wget http://www.kegel.com/wine/winetricks
$ chmod +x winetricks
$ ./winetricks mfc42
$ cp ~/Downloads/iview427_setup.exe .
$ cp ~/Downloads/irfanview_plugins_427_setup.exe .
$ wine iview427_setup.exe
$ wine irfanview_plugins_427_setup.exe

I have a KDE Kickoff menu entry for IrfanView containing the following command:

env WINEPREFIX="/home/fitzcarraldo/.wine-irfanview" WINEARCH="win32" wine C:\\\\Program\\ Files\\\\IrfanView\\\\i_view32.exe

The above command might look complicated, but WINE creates it automatically during the installation of IrfanView.

By installing Office 2007 and IrfanView this way, I keep the DLLs installed by the IrfanView installer completely separate from the DLLs used by Office 2007.

File associations

If you find that Word opens a blank document (Document1) when you double-click on a .doc (or .docx) file then this is a file association issue. In KDE I simply selected Kickoff > System Settings > File Associations and changed all the associations for .doc and .docx from the following:

env WINEPREFIX="/home/fitzcarraldo/.wine-office2007" WINEARCH="win32" wine C:\\windows\\command\\start.exe /Unix /home/fitzcarraldo/.wine-office2007/dosdevices/c:/users/fitzcarraldo/Start\ Menu/Programs/Microsoft\ Office/Microsoft\ Office\ Word\ 2007.lnk

to the following:

env WINEPREFIX="/home/fitzcarraldo/.wine-office2007" WINEARCH="win32" wine C:\\windows\\command\\start.exe /Unix %U

A different approach is needed if you want IrfanView to open image files when you double-click on them, and I’ll cover that in my next post.

How to join together several Flash video (flv) files

YouTube users will be familiar with the 10-minute limit on videos uploaded to the site. People get around the restriction by splitting longer videos into several clips. Recently I wanted to watch a video that had been split into nine clips on YouTube, but I wanted to join them together to avoid interruptions. However, it turns out that, if video clips do not all have the same resolution, bit rate and frame rate, concatenating them is more complicated than I was expecting. Below I describe the things I tried and what worked for me.

I downloaded the clips by using the script youtube-dl, which is an easy way of downloading YouTube videos without having to watch them first. For example:

$ youtube-dl -o brodyworld_dreamer.flv http://www.youtube.com/watch?v=ue-0UUVzUbM

The nine clips have the following characteristics:

clip1.flv 35.7 MiB (480×360, 362 average kbps, 25-26 fps according to YouTube)
clip2.flv 43.2 MiB (480×360, 460 average kbps, 25-26 fps according to YouTube)
clip3.flv 46.3 MiB (480×360, 418 average kbps, 25-26 fps according to YouTube)
clip4.flv 27.8 MiB (320×240, 238 average kbps, 25-26 fps according to YouTube)
clip5.flv 20.0 MiB (320×240, 203 average kbps, 25-26 fps according to YouTube)
clip6.flv 45.5 MiB (480×360, 414 average kbps, 25-26 fps according to YouTube)
clip7.flv 43.0 MiB (480×360, 460 average kbps, 25-26 fps according to YouTube)
clip8.flv 45.4 MiB (480×360, 495 average kbps, 25-27 fps according to YouTube)
clip9.flv 28.0 MiB (480×360, 400 average kbps, 25-26 fps according to YouTube)

Google found several sites with instructions on how to concatenate .flv files. Below are a few of the suggested solutions, and my experience using them to try and concatenate these nine video clips.

GUI APPLICATIONS

Pitivi

This could import all the clips but, when I dragged them to the timeline pane at the bottom of the window, Pitivi would only recognise them as audio clips. Also, if I tried to play one of the clips in the right pane of the window, Pitivi would play only the audio track. I have found this happens with some .flv files that I download from YouTube.

So, I tried another application…

Avidemux

A pop-up window appeared when I tried to open the first clip:

H.264 detected.
If the file is using B-frames as reference it can lead to a crash or stuttering.
Avidemux can use another mode which is safe but YOU WILL LOSE FRAME ACCURACY.
Do you want to use that mode?

I clicked on ‘Use safe mode’ and Avidemux loaded the clip successfully and could play it. A different pop-up window appeared when I then tried to open the second clip:

Video dimensions don’t match. You cannot mix different video dimensions yet. Using partial video filter later will not work around this problem. The workaround is: 1) “Resize” /”Add Border” / “Crop” each stream to the same resolution; 2) Concatenate them together.

Too much hassle, so on to the next application…

Kino

This looked like it might work, as it converts each .flv clip to a DV file first:

“/home/fitzcarraldo/Downloads/clip1.flv” is not a DV file. Do you want to import it? Importing… (this might take a while).

It certainly does take a while, and each clip’s .dv file was 2 GiB. I gave up after the second clip. Too long and too manual for my liking.

Time to try the command line, then…

THE COMMAND LINE

Google finds several forum threads and blogs offering command line solutions for joining several flv files together (and for joining together several .avi files, or several .mpeg files, and so on). I tried several of the different command strings recommended, none of which worked with the nine clips I had downloaded. Some of the commands resulted in a concatenated file that would only play the first 10 minutes (i.e. the first clip), others only the first 20 minutes (i.e. the first two clips), and others resulted in a zero-length file.

My biggest hope were the three alternatives given in the FFmpeg FAQ section 3.15 How can I join video files?, but the error messages ‘Estimating duration from bitrate, this may be inaccurate’ and ‘VBV buffer size not set, muxing may fail’ during processing worried me. And they are hardly quick and easy commands to enter.

FFmpeg Method 1

$ ffmpeg -i clip1.flv -sameq intermediate1.mpg
$ ffmpeg -i clip2.flv -sameq intermediate2.mpg
$ ffmpeg -i clip3.flv -sameq intermediate3.mpg
$ ffmpeg -i clip4.flv -sameq intermediate4.mpg
$ ffmpeg -i clip5.flv -sameq intermediate5.mpg
$ ffmpeg -i clip6.flv -sameq intermediate6.mpg
$ ffmpeg -i clip7.flv -sameq intermediate7.mpg
$ ffmpeg -i clip8.flv -sameq intermediate8.mpg
$ ffmpeg -i clip9.flv -sameq intermediate9.mpg
$ cat intermediate1.mpg intermediate2.mpg intermediate3.mpg intermediate4.mpg intermediate5.mpg intermediate6.mpg intermediate7.mpg intermediate8.mpg intermediate9.mpg > intermediate_all.mpg
$ ffmpeg -i intermediate_all.mpg -sameq film.avi

The resulting file was 124 MiB and consisted of only the first twenty minutes, i.e. the first two clips. Both VLC and SMPlayer could play it.

FFmpeg Method 2

$ mkfifo intermediate1.mpg
$ mkfifo intermediate2.mpg
$ mkfifo intermediate3.mpg
$ mkfifo intermediate4.mpg
$ mkfifo intermediate5.mpg
$ mkfifo intermediate6.mpg
$ mkfifo intermediate7.mpg
$ mkfifo intermediate8.mpg
$ mkfifo intermediate9.mpg
$ ffmpeg -i clip1.flv -sameq -y intermediate1.mpg < /dev/null &
$ ffmpeg -i clip2.flv -sameq -y intermediate2.mpg < /dev/null &
$ ffmpeg -i clip3.flv -sameq -y intermediate2.mpg < /dev/null &
$ ffmpeg -i clip4.flv -sameq -y intermediate2.mpg < /dev/null &
$ ffmpeg -i clip5.flv -sameq -y intermediate2.mpg < /dev/null &
$ ffmpeg -i clip6.flv -sameq -y intermediate2.mpg < /dev/null &
$ ffmpeg -i clip7.flv -sameq -y intermediate2.mpg < /dev/null &
$ ffmpeg -i clip8.flv -sameq -y intermediate2.mpg < /dev/null &
$ ffmpeg -i clip9.flv -sameq -y intermediate2.mpg < /dev/null &
$ cat intermediate1.mpg intermediate2.mpg intermediate3.mpg intermediate4.mpg intermediate5.mpg intermediate6.mpg intermediate7.mpg intermediate8.mpg intermediate9.mpg |\
ffmpeg -f mpeg -i - -sameq -vcodec mpeg4 -acodec libmp3lame film.avi

The resulting file was 124 MiB and again only consisted of the first twenty minutes. SMPlayer could play it (although the film’s duration was shown as 00:00:00), but VLC could not.

FFmpeg Method 3

$ mkfifo temp1.a
$ mkfifo temp1.v
$ mkfifo temp2.a
$ mkfifo temp2.v
$ mkfifo temp3.a
$ mkfifo temp3.v
$ mkfifo temp4.a
$ mkfifo temp4.v
$ mkfifo temp5.a
$ mkfifo temp5.v
$ mkfifo temp6.a
$ mkfifo temp6.v
$ mkfifo temp7.a
$ mkfifo temp7.v
$ mkfifo temp8.a
$ mkfifo temp8.v
$ mkfifo temp9.a
$ mkfifo temp9.v
$ mkfifo all.a
$ mkfifo all.v
$ ffmpeg -i clip1.flv -vn -f u16le -acodec pcm_s16le -ac 2 -ar 44100 - > temp1.a < /dev/null &
$ ffmpeg -i clip2.flv -vn -f u16le -acodec pcm_s16le -ac 2 -ar 44100 - > temp2.a < /dev/null &
$ ffmpeg -i clip3.flv -vn -f u16le -acodec pcm_s16le -ac 2 -ar 44100 - > temp3.a < /dev/null &
$ ffmpeg -i clip4.flv -vn -f u16le -acodec pcm_s16le -ac 2 -ar 44100 - > temp4.a < /dev/null &
$ ffmpeg -i clip5.flv -vn -f u16le -acodec pcm_s16le -ac 2 -ar 44100 - > temp5.a < /dev/null &
$ ffmpeg -i clip6.flv -vn -f u16le -acodec pcm_s16le -ac 2 -ar 44100 - > temp6.a < /dev/null &
$ ffmpeg -i clip7.flv -vn -f u16le -acodec pcm_s16le -ac 2 -ar 44100 - > temp7.a < /dev/null &
$ ffmpeg -i clip8.flv -vn -f u16le -acodec pcm_s16le -ac 2 -ar 44100 - > temp8.a < /dev/null &
$ ffmpeg -i clip9.flv -vn -f u16le -acodec pcm_s16le -ac 2 -ar 44100 - > temp9.a < /dev/null &
$ ffmpeg -i clip1.flv -an -f yuv4mpegpipe - > temp1.v < /dev/null &
$ { ffmpeg -i clip2.flv -an -f yuv4mpegpipe - < /dev/null | tail -n +2 > temp2.v ; } &
$ { ffmpeg -i clip3.flv -an -f yuv4mpegpipe - < /dev/null | tail -n +2 > temp3.v ; } &
$ { ffmpeg -i clip4.flv -an -f yuv4mpegpipe - < /dev/null | tail -n +2 > temp4.v ; } &
$ { ffmpeg -i clip5.flv -an -f yuv4mpegpipe - < /dev/null | tail -n +2 > temp5.v ; } &
$ { ffmpeg -i clip6.flv -an -f yuv4mpegpipe - < /dev/null | tail -n +2 > temp6.v ; } &
$ { ffmpeg -i clip7.flv -an -f yuv4mpegpipe - < /dev/null | tail -n +2 > temp7.v ; } &
$ { ffmpeg -i clip8.flv -an -f yuv4mpegpipe - < /dev/null | tail -n +2 > temp8.v ; } &
$ { ffmpeg -i clip9.flv -an -f yuv4mpegpipe - < /dev/null | tail -n +2 > temp9.v ; } &
$ cat temp1.a temp2.a temp3.a temp4.a temp5.a temp6.a temp7.a temp8.a temp9.a > all.a &
$ cat temp1.v temp2.v temp3.v temp4.v temp5.v temp6.v temp7.v temp8.v temp9.v > all.v &
$ ffmpeg -f u16le -acodec pcm_s16le -ac 2 -ar 44100 -i all.a \
-f yuv4mpegpipe -i all.v \
-sameq -y film.flv
$ rm temp[123456789].[av] all.[av]

The resulting file was 175.4 MiB. Both VLC and SMPlayer showed the correct duration of 1:27:22 but could not play further than twenty minutes.

Mencoder Method 1

A Linux forum thread suggested the following mencoder command string would do the job:

$ mencoder -forceidx -of lavf -oac copy -ovc copy -o film.flv clip1.flv clip2.flv clip3.flv clip4.flv clip5.flv clip6.flv clip7.flv clip8.flv clip9.flv

This failed with the error message “Audio format 0x4134504d is incompatible with ‘-oac copy’, please try ‘-oac pcm’ instead or use ‘-fafmttag’ to override it.” so I changed it to:

$ mencoder -forceidx -of lavf -oac copy -ovc pcm -o film.flv clip1.flv clip2.flv clip3.flv clip4.flv clip5.flv clip6.flv clip7.flv clip8.flv clip9.flv

This produced the error message “All video files must have identical fps, resolution, and codec for -ovc copy.” and the resulting film.flv was 261.2 MiB. Both VLC and SMPlayer showed the film duration as 20 minutes, and played those 20 minutes.

Mencoder Method 2

Finally I found a command that worked successfully and without hassle:

$ mencoder -oac pcm -ovc xvid -vf scale -zoom -xy 480 -xvidencopts bitrate=460 -o film.avi clip1.flv clip2.flv clip3.flv clip4.flv clip5.flv clip6.flv clip7.flv clip8.flv clip9.flv

I chose a bitrate of 460 kbps, as it is near the top of the range of bitrates of the nine clips, and an X-axis resolution of 480 pixels (the Y-axis resolution is scaled automatically) even though some of the clips have a smaller resolution. The resulting file is a 1.1 GiB AVI file. The image is a bit less distinct than the original clips but SMPlayer and VLC show the film’s correct duration and play it to the end. There are slight hiccups at some of the joins, but these are hardly noticeable. SMPlayer lists the following information about the video:

General
 File: /home/fitzcarraldo/Downloads/film.avi
 Size: 1182950 KB (1155 MB)
 Length: 01:27:21
 Demuxer: avi

Clip info
 Software: MEncoder SVN-r30554-4.4.2

Video
 Resolution: 480 x 360
 Aspect ratio: 1.3333
 Format: XVID
 Bitrate: 430 kbps
 Frames per second: 25.000
 Selected codec: ffodivx

Initial Audio Stream
 Format: 1
 Bitrate: 1411 kbps
 Rate: 44100 Hz
 Channels: 2
 Selected codec: pcm

Audio Streams
 #: 0
 Language: <empty>
 Name: <empty>
 ID: 1

So there we have it. Not perfect, but at least something that actually works, and with little effort.

If you have successfully stitched together video files each having a different resolution and/or frame rate and/or bit rate and/or codec, do add a comment and let us know the command string or GUI application that you used, and whether or not the resulting video quality was good.