Live Encoding Tutorial
From Xugglewiki
There are many ways to encode live video to send to a RTMP based streaming server. You can use Flash Media Encoder, or the build-in Flash webcam support.
However several folks have wanted a way to encode video over RTMP on Linux and Mac servers, or even an alternate way on Windows, and the latest Xuggler (3.4 and later) supports this. This tutorial shows how to do that.
Install Xuggler 3.4
Go to the downloads page and install Xuggler 3.4
Confirm you have the Xuggler version of FFmpeg
Run:
ffmpeg -i /invalid/file
Your output should look something like:
FFmpeg version SVN-r21606-xuggle-4.0.844, Copyright (c) 2000-2010 Fabrice Bellard, et al. built on Feb 2 2010 10:57:41 with gcc 4.4.1 configuration: --prefix=/home/aclarke/Stage --extra-version=xuggle-4.0.844 --extra-cflags=-I/home/aclarke/Work/internal/xuggle/java/xuggle-xuggler/build/native/i686-pc-linux-gnu/captive/home/aclarke/Stage/include --extra-ldflags=-L/home/aclarke/Work/internal/xuggle/java/xuggle-xuggler/build/native/i686-pc-linux-gnu/captive/home/aclarke/Stage/lib --enable-shared --enable-gpl --enable-nonfree --enable-version3 --disable-stripping --disable-optimizations --disable-mmx --disable-mmx2 --enable-libx264 --enable-libmp3lame --enable-libvorbis --enable-libtheora --enable-libspeex --enable-libfaac --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-pthreads libavutil 50. 8. 0 / 50. 8. 0 libavcodec 52.51. 0 / 52.51. 0 libavformat 52.50. 0 / 52.50. 0 libavdevice 52. 2. 0 / 52. 2. 0 libswscale 0. 9. 0 / 0. 9. 0 /invalid/file: no such file or directory
Make sure the --extra-version string above includes "xuggle" in it. If not, you have the wrong version of FFmpeg and should go back to the first step.
Record a live stream from a RTMP server
These instructions assume you have a Adobe FMS, Wowza or Red5 server running on the machine "localhost", and have an application named "live". The live application is installed by default on FMS, and can be installed on Wowza as well. For Red5, install the oflaDemo application, and replace live with oflaDemo.
- First, connect your current encoding tool to
rtmp://localhost/live. - Then, publish a stream named "testStream" with your favorite parameters.
- Finally execute the following command to record a FLV file to disk of that stream:
ffmpeg -i rtmp://localhost/live/testStream -acodec copy -vcodec copy -y test.flv
Hit 'q' when you've recorded enough, and test.flv will contain your recording.
Record a live stream from a RTMP server and re-encode to another format
These instructions assume you have a Adobe FMS, Wowza or Red5 server running on the machine "localhost", and have an application named "live". The live application is installed by default on FMS, and can be installed on Wowza as well. For Red5, install the oflaDemo application, and replace live with oflaDemo.
In this example we're going to re-encode to mpeg4 video with MP3 audio inside an AVI container:
- First, connect your current encoding tool to
rtmp://localhost/live. - Then, publish a stream named "testStream" with your favorite parameters.
- Finally execute the following command to record a FLV file to disk of that stream:
ffmpeg -i rtmp://localhost/live/testStream -acodec libmp3lame -ar 22050 -vcodec mpeg4 -y test.avi
Hit 'q' when you've recorded enough, and test.avi will contain your recording.
Publish a pre-recorded FLV file as a live stream to a RTMP server
These instructions assume you have a Adobe FMS, Wowza or Red5 server running on the machine "localhost", and have an application named "live". The live application is installed by default on FMS, and can be installed on Wowza as well. For Red5, install the oflaDemo application, and replace live with oflaDemo.
The file we are going to publish is named "testfile.flv"
- First, connect your current video playback tool to
rtmp://localhost/live. - Then, start playing a stream named "testStream" with your favorite parameters (it will be smoother if you buffer 2-3 seconds).
- Finally, execute this FFmpeg command to 'stream' the file to the RTMP server:
ffmpeg -i testfile.flv -re -acodec copy -vcodec copy -f flv rtmp://localhost/live/testStream
The -re tells FFmpeg to output in 'near real time' mode.
Publish a H264 MP4 file as a live stream to a RTMP server
These instructions assume you have a Adobe FMS, Wowza or Red5 server running on the machine "localhost", and have an application named "live". The live application is installed by default on FMS, and can be installed on Wowza as well. For Red5, install the oflaDemo application, and replace live with oflaDemo.
The file we are going to publish is named "testfile.mp4"
- First, connect your current video playback tool to
rtmp://localhost/live. - Then, start playing a stream named "testStream" with your favorite parameters (it will be smoother if you buffer 2-3 seconds).
- Finally, execute this FFmpeg command to 'stream' the file to the RTMP server:
ffmpeg -i testfile.mp4 -re -acodec copy -vcodec copy -f flv rtmp://localhost/live/testStream
The -re tells FFmpeg to output in 'near real time' mode.
Re-encode a file as H264 video and AAC audio and publish as a live stream to a RTMP server
These instructions assume you have a Adobe FMS, Wowza or Red5 server running on the machine "localhost", and have an application named "live". The live application is installed by default on FMS, and can be installed on Wowza as well. For Red5, install the oflaDemo application, and replace live with oflaDemo.
The file we are going to publish is named "testfile.avi". We are going to re-encode it using the 'default' H264 profile, and at 22050 kHz sample rate.
- First, connect your current video playback tool to
rtmp://localhost/live. - Then, start playing a stream named "testStream" with your favorite parameters (it will be smoother if you buffer 2-3 seconds).
- Finally, execute this FFmpeg command to 'stream' the file to the RTMP server:
ffmpeg -i testfile.avi -re -acodec libfaac -ar 22050 -vcodec libx264 -vpre default -f flv rtmp://localhost/live/testStream
Notes:
- The
-reoption tells FFmpeg to output in 'near real time' mode.. - The
-aroption tells FFmpeg to re-encode to 22050 kHz - The
-vpreoption tells FFmpeg to use the libx264-default.ffpreset file that is installed with it. - You can create your own preset files for FFmpeg to change encoding options however you like.
- The
-foption tells FFmpeg to output all data in the FLV container format.
Limitations
The Xuggler RTMP encoder has the following limitations today:
- We can only publish streams to the top level application -- nested rooms/scopes don't work.
- We can only publish streams in the FLV container; FFmpeg does not yet support the F4V container.
- There is no support for AMF-based security systems (like Akamai uses); you need unprotected streams on the RTMP server.
