Streaming Video with TheoraCookbook.FFMPEG2Theora

This method focuses on streaming video from a DV or Firewire camera using Linux. The video stream will be Ogg Theora which means your users will be able to watch using Theora enabled player or in a java applet such as Cortado.

There are two ways of doing this. The hard way and the easy way. The hard way is to do everything by hand (installing everything from source). In fact when I wrote this document originally, that was the only way to do it. However things have progressed and now there is an easier way. The easy way is to use Debian (or maybe Ubuntu, I haven't tested this) and use the Debian Packages.

In case you can't use Debian or you want to make your life hard for no good reason, then I have included this process as well. To go this route it will help to be familiar with Linux. In fact, don't go much further unless down the hard road unless you know how to find your way around Linux and you should also know a little about modules, compiling software from the command line, and using a command line interface for executing software. Following this process as a 'newbie' could be tricky. I provide as much information about the process as I can but there are a lot of issues that you may need to resolve that I cannot account for here. Certainly as a 'moderately' experienced Linux user you should be ok. 

Getting Ready

  • A Firewire camera is not the same as a DV camera. However for the intents of this manual they work the same.
  • Firewire is known also as ieee1394

Requirements

You will need the following:
  • DV cam
  • Linux machine with firewire inputs
  • firewire cable
  • internet connection
  • access to a 'theora-enabled' Icecast2 server (you need the IP address of the server, the port you should use, and the password)

In addition to the above you need to have raw1394 support for your firewire devices. raw1394 is a kernel module. A kernel module is a small extension to the operating system that enables specific functionality that is not included in the kernel itself. Kernel modules work similar to plugins in other applications, except they extend the functionality of the entire operating system not just a single application.

You may not have raw1394 support. To check if you do open a terminal and type the following:

lsmod

You may see a long list of items similar (but not exactly the same) to this:

Module                  Size  Used by
md5                     7680  1
ipv6                  234784  12
lp                     13448  0
via_agp                11264  0
sworks_agp             10912  0
sis_agp                 9988  0
nvidia_agp              9756  0
intel_mch_agp          11792  0
efficeon_agp           10144  0
ati_agp                10380  0
amd64_agp              13640  0
amd_k7_agp              9740  0
ali_agp                 9216  0
autofs4                18820  1
af_packet              20104  0
i810_audio             36884  1
soundcore              11104  2 i810_audio
ac97_codec             20108  1 i810_audio

Look in this list for raw1394. If you see it then great! If not then type this:

modprobe raw1394

Then retype lsmod and you may see something like this:

Module                  Size  Used by
raw1394                28396  0
md5                     7680  1
ipv6                  234784  12
lp                     13448  0
via_agp                11264  0
sworks_agp             10912  0
sis_agp                 9988  0
nvidia_agp              9756  0
intel_mch_agp          11792  0
efficeon_agp           10144  0
ati_agp                10380  0
amd64_agp              13640  0
amd_k7_agp              9740  0
ali_agp                 9216  0
autofs4                18820  1
af_packet              20104  0
i810_audio             36884  1
soundcore              11104  2 i810_audio
ac97_codec             20108  1 i810_audio

The first line says raw1394 28396 0 which is good news. If you see something similar to this then this is also good! You can also try checking dmesg (just type dmesg on the command line and look for something signifying the raw1394 module is ok - if you see nothing mentioned then chances are raw1394 is not installed). Otherwise you are going to have to take the long road home and get raw1394 support in your kernel modules. This can be complicated as it depends on what version your kernel is etc etc. A good starting point for this research would be here:

http://www.linux1394.org/

Set-Up

Assuming you have the raw1394 module running, lets start setting up the software you need. We will need to install the following applications:

  • dvgrap
  • ffmpeg2theora
  • oggfwd.

The Easy Road 

With Debian 4 you can run this command line (using stable sources):

apt-get install libshout3 libshout3-dev dvgrab ffmpeg libtheora0 libtheora-dev oggfwd
ffmpeg2theora 

The above command should all appear on one line. Now, with luck you have everything you need to get started. So first we need to plug in the dv (or (firewire) camera to the computer. You need to attach the firewire camera to the firewire socket on the camera, and the other end of course goes into the firewire socket of your laptop or whatever computer you are using.

Now, turn on the camera. Check the raw1393 module is loaded by using lsmod (see the section near the beginning of this document on raw1394).

ok, so the thing is you will need know is to execute the following line on one command line. You will need to use the streaming server details you have for the Icecast2 (theora-enabled) server. The command is:

dvgrab --format raw - | ffmpeg2theora -a 0 -v 5 -f dv -x 320 -y 240 -o /dev/stdout - |

oggfwd  icecastserver  8000 pwd /theora.ogg

Remember the command will have to be all on one line (the above example is not). Also replace the details:

  • icecastserver
  • 8000
  • pwd

with the details you have from your Icecast server. icecastserver should be replaced with the hostname or IP address of the streaming server. 8000 is the port number and is probably the same. pwd should be replaced by the password of your server. Lastly, you can replace /theora.ogg with anything as long as it starts with a forward slash (/) and ends in .ogg.

Now, you should be streaming! To check the connection use VLC .

The Hard Road 

You might want to cancel your appointments for the day.... 

dvgrab

You can find the source code and installation packages for dvgrab here - http://www.kinodv.org/

There are a number of ways to install software on Linux. The method very much depends on the tpe of Linux you are running. I am going to go through the following methods as they are the most popular:

* installing dvgrab with Debian (also true for Ubuntu and Knoppix)
* installing for Gentoo
* installing for Redhat
* installing by source

Installing dvgrab on Debian

Installing dvgrab on Debian based systems should be quite easy. Debian has a package management application called apt. The process for installing software in debian usually follows this process using apt (you usually have to be online for this) :

step 1 - Type the following into the command line (terminal):

apt-cache search dvgrab

You will hopefully see something like this in the terminal window:

dvgrab - grab digital video data via IEEE1394 links

The command apt-cache search dvgrab looks for applications online in the Debian repositories (places where software is stored) and if it finds one with a name matching dvgrab then it lists those applications. To get familiar with how this works try the same command (apt-cache search) with different application names eg. apt-cache search mozilla

So, the output in the terminal window will either return nothing (a blank line) or some output such as that illustrated above. The above output gives the name of the application that matches your search (in this case dvgrab) and a short description ("grab digital video data via IEEE1394 links").

Now, if you see this then this is good news. The application is available from the repositories and we can install it. If the returned output was a blank line it means the repositories listed don't contain the software you are after, in which case you can do one of two things:

  • install by source (see details below)
  • learn about apt and alter the sources.list to include a repositry with your software. This can be tricky but its good to know. To start your research read this:
http://www.debian.org/doc/manuals/apt-howto/index.en.html

step 2 - So, assuming the repositry has dvgrab you need to install the software. Under Debian this is very easy, however you may have to (depending on how your operating system is set up) use the sudo or su commands for step2. This is because your user account may not allow you to install software but it will have to be done by root. I will explain this more below, in the meantime lets first look at the basic commands:

apt-get install dvgrab

This should download the required packages and install. The process may ask for some feedback (it may ask you to verify its the right package etc) in which case you should read the messages in the terminal window carefully before deciding. If all is well, you will then have installed dvgrab.

To test if dvgrab is installed type this in the terminal:

dvgrab --version

You will hopefully get an output similar to this:

dvgrab 1.8

This means you have dvgrab version 1.8 installed (your output might contain a different version number).

If you can't get the software installed via apt-get it might be because your user account does not have permission to install software. In which case you will need to use the su or sudo commands. These allow you to install software as the root user. su will log you in as the root user and then you can execute any commands you like under root. Be careful if you choose this way as you can accidently do a lot of damage to your operating system if you carelessly type the wrong command as root. To use su just type the command in the terminal and hit return. Then you will be prompted for the root password. if you do not know the root password for your system then you cannot use this method unless the system administrator sets it up for you. Once you have entered the root password you can follow the install steps above and all should be ok.

The other process to consider using is with sudo. sudo is short for super-user-do. So any command entered after sudo will be executed as root (superuser). So, type this:

sudo apt-get install dvgrab

You will then be prompted for a password. Enter your user account password not the root password. Then, if all is well, apt will install dvgrab. If all is not well and you get a permission error then you need to contact the system administrator and ask them if they will give you permission to use sudo (they do this by altering the sudoers file which is found at /etc/sudoers , for more info on this see http://www.courtesan.com/sudo/man/sudoers.html )

Installing dvgrab on Gentoo

Gentoo is another variety of Linux and it has its own package manager called emerge. The emerge system is pretty easy to use also and it usually contains more up-to-date versions of the software that Debian. To check if a package is available in emerge you should go to the Gentoo Online Package Management Database ( http://packages.gentoo.org/ ) and on the left you will see a search form. Enter dvgrab in the form field and click the "search" button (or press return).

Hopefully, you will get a page explaining to you some details of the dvgrab package for emerge. Now, to install type:

emerge dvgrab

You may have to use su or sudo to do this (see notes above in FFmpeg2Theora.Installing under Debian). Now test by typing :

dvgrab --version

Installing dvgrab on Redhat

I'm not so experienced on Redhat so if there are any Redhat gurus reading this I would appreciate any help in providing better information for using rpm to install dvgrab.

To install you need to use the Redhat Package Manager which is also known as rpm. To find package for Redhat you need to go online with a browser and look for a package database. rpmfind is a good one:

http://rpmfind.net/

There is a search form on this page that you should enter dvgrab and press return. You will be presented with a list of files that relate to your search term.Scroll down the page and find the package that suits the hardware of your machine. If you have a Pentium processor choose a version with 386 in its name. If you dont know the type of hardware you have then try 386 or try installing from source (see below). The example file I will use is dvgrab-1.7-3.i386.rpm

Now download the file. To install the downloaded file, type the following (make sure your terminal is in the same directory as the downloaded file):

rpm -i dvgrab-1.7-3.i386.rpm

Now the install process should just run its course. To test the install try this in the terminal:

dvgrab --verbatim

and refer to the notes on testing in the above sections.

Installing dvgrab from source

This is always the fallback method for installing if all else goes horribly wrong.

To install from source first you need to download the latest sources. Open this page in your browser: http://www.kinodv.org/

Then goto the dvgrab download section and download the latest sources. In this case the latest source file is dvgrab-1.8.tar.gz.

Now, in a terminal (ensuring you are in the same directory as the downloaded sources) type the following:

tar zxvf dvgrab-1.8.tar.gz

Remember to replace dvgrab-1.8.tar.gz with the name of your downloaded file.

This command decompresses the archive file and creates a new directory with the source files in it. You will now have to change directories and go into the newly created directory. In a terminal type:

cd dvgrab-1.8

Remember your new directory might be called something other than dvgrab-1.8 , to check type ls in the terminal and you will see a list of files and directories. From this you can see the right name of the new directory.

Now type:

./configure

If all is good, you can then type:

make ; make install;

and the software will be installed. However probably you have this error:

configure: error: avc1394.h not found, install libavc1394-devel

If you get this it means one of the dependencies hasn't been met. So you have to install this libavc1394-devel. Usually what I do in this situation is google for the lost package. If we do this, the first link is to this page: http://sourceforge.net/projects/libavc1394/

This is the homepage for the libavc1394 software. At the time of writing the latest version is libavc1394-0.5.1 . Download this file and decompress the file like so (make sure your terminal is in the right directory):

tar zxvf libavc1394-0.5.1.tar gz

The change directories to the newly created source directory:

cd libavc1394-0.5.1

then do the configure:

./configure

then type the make and make install as so:

make ; make install;

You may need to use su or sudo to do this (see above sections). Now change back to the dvgrab source directory and do a configure again:

./configure

Then all should be good (no error messages) and so do the following:

make ; make install;

(you may need to use su or sudo for this too. Thats it! Now test with:

dvgrab --version

notes

  • if you would like to know more about dvgrab visit the developers website:
http://www.kinodv.org/
  • if you wish to read the manual that was installed with dvgrab the use the man command like so:
man dvgrab

oggfwd

http://www.v2v.cc/~j/ffmpeg2theora/oggfwd/

There are two ways of installing oggfwd, from source of by downloading a pre-compiled binary. The first way is similar to the process we went through with dvgrab, the second (installing the binary) means that you simply download the application and run it without any need to install. If you are a Debian user you can use :

apt-get install oggfwd 

First however you need to install a libshout-2.1 which is a library of code that oggfwd uses. To install libshout-2.1 first download it from here (Debian users could first try apt-get install libshout3-dev):
http://v2v.cc/~j/ffmpeg2theora/oggfwd/libshout-2.1+svn.tar.gz

Download this and decompress using the following:

tar zxvf libshout-2.1+svn.tar.gz

Now change directories to the newly created directory:

cd libshout-2.1

and run the configure script:

./configure

If all is well do a make and make install (it may need to be done using su or sudo, check the notes above for dvgrab).

make ; make install; 

Installing Oggfwd from source

First download the file oggfwd.tar.gz and then decompress it using the terminal command:
tar zxvf oggfwd.tar.gz

Then change directories to the newly created directory

cd oggfwd

Now compile the binary

make

Now install the compiled binary

make install

This last step may need to be done using su or sudo (see the dvgrab section above).

To test if it is installed type

oggfwd

and you should see an output in the terminal like:

usage: oggfwd hostname port# password target
sample: oggfwd icf.de 8100 abc123 /cast.ogg 

Using the Oggfwd pre-compiled binary

To use the pre-compiled binary simply download the file oggfwd.linux.bin and execute it like so:
./oggfwd.linux.bin

and you should see something like this:

usage: oggfwd hostname port# password target
sample: oggfwd icf.de 8100 abc123 /cast.ogg

Now, to use oggfwd.linux.bin you need to change its permissions, copy it to the directory that contains all your other binaries, and change its name to oggfwd.

Change the permissions and move it to /usr/bin (renaomg it to oggfwd at the same time):

mv oggfwd.linux.bin /usr/bin/oggfwd
chmod 755 /usr/bin/oggfwd

Now, if you simply type oggfwd the application should start and give the same output in the terminal as described above.

ffmpeg2theora

ffmpeg2theora requires two applications to be installed before you can install it. They are ffmpeg and libtheora. It has to be said that installing ffmpeg can be complex because it has so many compile options. We will go through the process with just some simple options enabled, you may wish to re-install it with more complex choices at another date.

ffmpeg

You must install the latest version of ffmpeg from the development site http://ffmpeg.sourceforge.net/index.php

If you know how to use CVS then you can check out the CVS files and use them to build ffmpeg . If you don't know how to use CVS then I have downloaded the files for you from CVS and you can find them here: http://www.xs4all.nl/~adam/source/ffmpeg_101205.tar.gz

When the file is downloaded, decompress the file with the following command:

tar zxvf ffmpeg-0.4.9-pre1.tar.gz

Remember to replace ffmpeg-0.4.9-pre1.tar.gz with the actual name of your file.

Next, change directories to the source directory:

cd ffmpeg-0.4.9-pre1.tar.gz

You directory may be named slightly differently from the above example. Now, we need to do a configure but with a parameter included in the command, this is to enable ogg vorbis support.

./configure --enable-vorbis

Now do a make and make install (may need to be done using su or sudo, see the notes above for isntalling dvgrab).

make ; make install;

It might take a long time to compile and install.

To test it all went well type this:

ffmpeg -v

You should see an output in the terminal similar to this:

ffmpeg version 0.4.9-pre1, build 4718, Copyright (c) 2000-2004 Fabrice Bellard
  built on Sep  6 2005 23:39:39, gcc: 3.3.6 (Debian 1:3.3.6-7)

libtheora

Theora is the codec you need to stream, in other words its the format of the streamed video. To use theora you need to install the libraries that enable it. I think it is best to install the latest one from source, so we will only look at this method. You may wish to try other ways for Debian, Gentoo etc. It is also worth noting that you could try installing libtheora-mmx instead off libtheora as it is faster. According to the oggfwd and ffmpeg2theorammx version is 2-3 times quicker. If you want to try this you can follow the below steps, but instead of using the sources from http://www.theora.org you could try : http://downloads.xiph.org/releases/theora/libtheora-mmx-1.0alpha5.tar.bz2

If you wish to try the 'plain vanilla' sources get them from : http://www.theora.org/

At the time of writing the latest source file is libtheora-1.0alpha5.tar.gz, so we will download this and decompress like so:

tar zxvf  libtheora-1.0alpha5.tar.gz

Now change directory to the new source directory:

cd libtheora-1.0alpha5

Try running the configure script:

./configure

Now do a make and make install. It might need su or sudo (see above in dvgrab section).

make ; make install;

Hopefully all is well, and we can continue with installing ffmpeg2theora.
 

ffmpeg2theora

Download the package from here (we will only install from source): http://www.v2v.cc/~j/ffmpeg2theora/download.html

The file I am working with is ffmpeg2theora-0.15.tar.bz2, to decpmpress this try this command:

tar xvfj ffmpeg2theora-0.15.tar.bz2

Now change directories to the new source directory:

cd ffmpeg2theora-0.15

Now try the configure process

./configure

Then a make and make install:

make ; make install;

phew.....hopefully your are now done, to test ffmpeg2theora try this:

ffmpeg2theora

and you should see a lot of information appear on how to use the application, the output should be similar to this:

ffmpeg2theora 0.13

usage: ffmpeg2theora [options] input

Output options:
--output,-o alternative output
--width, -x scale to given size
--height,-y scale to given size
--aspect define frame aspect ratio: i.e. 4:3 or 16:9
--crop[top|bottom|left|right] crop input before resizing
--videoquality,-v [0 to 10] encoding quality for video
--videobitrate,-V [45 to 2000] encoding bitrate for video
--sharpness,-S [0 to 2] sharpness of images(default 2)
{ lower values make the video sharper. }
--keyint,-K [8 to 65536] keyframe interval (default: 64)
--audioquality,-a [-1 to 10] encoding quality for audio
--audiobitrate,-A [45 to 2000] encoding bitrate for audio
--samplerate,-H set output samplerate in Hz
--channels,-c set number of output sound channels
--nosound disable the sound from input
--endtime,-e end encoding at this time (in sec)
--starttime,-s start encoding at this time (in sec)
--v2v-prwww-data,-p encode file with v2v preset,
right now there is preview and pro,
'ffmpeg2theora -p info' for more informations

Input options:
--deinterlace,-d force deinterlace,
n otherwise only interlaced material is deinterlaced
--format,-f specify input format
--inputfps [fps] override input fps

Metadata options:
--artist Name of artist (director)
--title Title
--date Date
--location Location
--organization Name of organization (studio)
--copyright Copyright
--license Licence

Other options:
--nice set niceness to n
--debug output some more information during encoding
--help,-h this message

Examples:
ffmpeg2theora videoclip.avi (will write output to videoclip.avi.ogg)

cat something.dv | ffmpeg2theora -f dv -o output.ogg -

Live encoding from a DV camcorder (needs a fast machine)
dvgrab - | ffmpeg2theora -f dv -x 352 -y 288 -o output.ogg -

Live encoding and streaming to icecast server:
dvgrab --format raw - |
ffmpeg2theora -f dv -x 160 -y 128 -o /dev/stdout - |
oggfwd iccast2server 8000 password /theora.ogg

Plugging in

Now, with luck you have everything you need to get started. So first we need to plug in the dv (or (firewire) camera to the computer. You need to attach the firewire camera to the firewire socket on the camera, and the other end of course goes into the firewire socket of your laptop or whatever computer you are using.

Now, turn on the camera. Check the raw1393 module is loaded by using lsmod (see the section near the beginning of this document on raw1394).

ok, so the thing is you will need know is to execute the following line on one command line. You will need to use the streaming server details you have for the Icecast2 (theora-enabled) server. The command is:

dvgrab --format raw - | ffmpeg2theora -a 0 -v 5 -f dv -x 320 -y 240 -o /dev/stdout - |

oggfwd  icecastserver  8000 pwd /theora.ogg

Remember the command will have to be all on one line (the above example is not). Also replace the details:

  • icecastserver
  • 8000
  • pwd

with the details you have from your Icecast server. icecastserver should be replaced with the hostname or IP address of the streaming server. 8000 is the port number and is probably the same. pwd should be replaced by the password of your server. Lastly, you can replace /theora.ogg with anything as long as it starts with a forward slash (/) and ends in .ogg.

Now, you should be streaming! To check the connection use VLC .