FLV คือรูปแบบหนึ่งของ vdeo file ที่นิยมนำมาทำ Streaming บนเว็บกัน ตัวอย่างได้แก่ Youtube ซึ่งจะใช้ FLV เพื่อทำ video streaming ซึ่งท่านที่ติดตั้ง RED 5 แล้วท่านก็สามารถที่จะสร้างงานแบบเดียวกับ YouTube ได้เช่นกัน รูปแบบที่นำมาเสนอวันนี่้เป็นรูปแบบที่ง่ายมากและท่านสามารถนำไปปรับปรุงให้เหมาะกับ
งานของท่านได้ทันที แต่มีข้อจำกัดอย่างหนึ่งคือ จะใช้งานได้เฉพาะ RED5 ที่เป็นชุดติดตั้งเท่านั้น ไม่สามารถใช้ได้กับ Resin หรืือ Tomcat ขั้นตอนมีดังนี้
1. หา directory ชื่อ "fitcDemo" หรือ "oflaDemo" ภายใต้ directory ที่ท่านติดตั้ง RED5 ทั้งสองเป็นตัวอย่างจากผู้สร้าง RED5 ซึ่งสามารถนำมาใช้งานได้เลย
2. หากยังไม่มี directory ชื่อ "streams" ภายใต้ directoy ทั้งสองก็ทำการสร้างขึ้นมาได้เลย แล้วนำ FLV Video ที่ท่านต้องการเผยแพร่มาไว้ที่นี่
3. download FLV player จากที่นี่ เป็น Flash File (.fla) โดยท่านสามารถนำไปแก้ไขด้วยตัวของท่านเอง
4. ปรับแต่ง Player ดังนี้
4.1 เปิดไฟล์ที่ได้ด้วย Flash จะได้ดังภาพ ท่านจะเห็น Component ที่ถูกนำมาวางไว้บน stage แล้ว คือ Video Player คลิ๊กขวาบน Component เพื่อให้สามารถใช้งานกับ Video ของท่าน
4.2 สังเกตุด้านล่างของจอภาพจะเห็นปุ่มที่ "Launch Component Inspector" คลิ๊กที่ปุ่มแล้วจะได้ผลดังภาพ ทำการปรับปรุง url ให้ัตรงกับที่เราจะใช้งาน
สมมุติว่า Server ของท่านเป็น localhost และ video ของท่านชื่อว่า myvideo.flv และท่่านนำไปไว้ใน oflaDemo/streams จะได้ url คือ
rtpm://localhost/oflaDemo/myvideo.flv
ขอให้สังเกตุว่าเราไม่จำเป็นต้องเขียน directory ใ้ห้เต็ม เพียงแต่อ้างอิงชื่อ video เลย โปรแกรมจะรับรู้เองว่าตัว video file อยู่ที่ไหน
5. ทำการ export ออกมาเป็น .html และ .swf แล้วนำไปเผยแพร่บนเว็บของท่าน เพียงเท่านี้ท่านก็จะได้ทำงานที่เป็น video stream แบบเดียวกับที่ "YouTube" แล้ว หรืออาจประยุกต์ทำเป็น Video Blog ก็ได้
6. ท่านต้องการเปลี่ยน video file จากรูปแบบอื่น เช่น .avi, .wav ฯล ให้เป็น .flv เรามีซอฟต์แวร์แนะนำคือ RivaEncoder ครับ เป็น Free Software โดยท่านสามารถ download ได้ที่ http://www.download.com/3001-2140_4-10381392.html หรือ จาก hosting ของเราก็ได้ที่นี ่้
Red5 is a popular program written in Java that lets you stream audio/video files (FLV/F4V/MP4/3GP/MP3/F4A/M4A/AAC), record streams (FLV), and supports various protocols (RTMP/RTMPT/RTMPS/RTMPE).
To get started, we will spin up Ubuntu 12.10 x64 cloud server:
apt-get install -y red5-server
This will install Red5 to /usr/share/red5
To start red5:
service red5-server start
Your server will now listen on these default ports:
RTMP: 1935
Debug proxy: 1936
HTTP servlet: 5080
RTMPT: 8088
At this point you are all done installing Red5, and now you need to add your Red5 application to /usr/share/red5/webapps
You can develop your own application using Eclipse IDE. There are many commercial applications available for Red5, as well as free applications.
There is also an online Red5 App Generator available here.
It will let you create RTMP streaming application:
Here is an example of how to install a free Red5 Chat application:
cd /usr/share/red5/webapps wget "http://downloads.sourceforge.net/project/freered5chat/red5chat_v2.2_Freedevelopment.net.zip" unzip red5chat_v2.2_Freedevelopment.net.zip mv red5chat_v2.2_Freedevelopment.net/* .
Restart Red5:
/etc/init.d/red5-server restart
Navigate over to your cloud server's IP port 5080 (http://198.199.91.87:5080/flash/ in our case):
For an upcoming project we needed to stream content from the user's webcam to be recorded on the server. In theory this sounded like a basic operation, having used FMS to this end before, but actually installing and setting up the Red5 server proved to be a little tricky. So I just wanted to outline the process I went through to get a Red5 Server running on ubuntu and how to setup your first recording stream.
RED5 IS A POWERFUL JAVA BASED VIDEO STREAMING AND MULTI-USER SOLUTION FOR THE FLASH PLAYER. RED5 INCLUDES SUPPORT FOR THE LATEST MULTI-USER API’S INCLUDING NETCONNECTION, NETSTREAM AND SHAREDOBJECT’S WHILE PROVIDING A POWERFUL RTMP / SERVLET IMPLEMENTATION. IT IS BASICALLY AN OPEN SOURCE FLASH MEDIA SERVER.
We're going to go through two major steps here, firstly installing Red5 on Ubuntu and then building a simple application to record the webcam.
Installing Red5
For this installation we're going to use a stock Ubuntu Server installation (8.04 Hardy Heron). We installed LAMP as well for the project but it's not required for Red5. Firstly we need to install a few dependencies including the Java runtime environment and the Java development kit (and yes these are terminal commands so get it up and running):
sudo apt-get install java-package sun-java6-jdk sun-java6-jre
sudo apt-get install subversion ant
Next we need to grab the code base for Red5. We tried the trunk version first but had some issues with it (as you'd expect from a HEAD revision) so ended up using v0.9.1. You can try other versions of the server by browsing the repository and replacing the reference in the svn checkout below:
svn co http://red5.googlecode.com/svn/java/server/tags/0_9_1/ ~/red5
Now we build using Ant:
export JAVA_HOME=/usr/lib/jvm/java-6-sun
export ANT_HOME=/usr/share/ant/
cd ~/red5
/usr/share/ant/bin/ant
Make sure there were no errors during the build. If there were consult the red5 wiki for help. Install:
sudo mkdir /usr/share/red5
sudo cp -R ~/red5/dist/* /usr/share/red5/.
sudo chmod 755 /usr/share/red5/red5.sh
Start the Red5 server:
export RED5_HOME=/usr/share/red5
/usr/share/red5/red5.sh
Next we need to create the startup script. This script will allow you to get the server starting on reboot etc. Put the following text into the file /etc/init.d/red5 and make the file executable sudo chmod 755 /etc/init.d/red5.
#!/bin/sh
# init script for Red5
# /etc/init.d/red5
RED5_USER=root
RED5_HOME=/usr/share/red5
RED5_PROG=red5
test -x $RED5_HOME/$RED5_PROG.sh || exit 5
case "$1" in
start)
echo -n "Starting Red5"
echo -n " "
cd $RED5_HOME
su -s /bin/bash -c "$RED5_HOME/$RED5_PROG.sh &" $RED5_USER
sleep 2
;;
stop)
echo -n "Shutting down Red5"
echo -n " "
su -s /bin/bash -c "killall -q -u $RED5_USER java" $RED5_USER
sleep 2
;;
restart)
$0 stop
$0 start
;;
esac
Now you can start/stop/restart from the command line by using:
sudo /etc/init.d/red5 [start/stop/restart]
Lastly once you've got the server running, you can browse to the test page on your serverhttp://localhost:5080 and you should see:
If you can't see this it and you've confirmed the Red5 server is running it could be a firewall issue. You may need to alter the IPTables on your machine but that's not going to be covered here.
Lastly we need to install the actual streaming application on our Red5 Server. Navigate tohttp://localhost:5080/installer/, select the oflaDemo and click Install.
To test it installed correctly, navigate to http://localhost:5080/demos/ofla_demo.html (correct the server name in the top right) and hit connect. You should see a list of available videos and a green light in the top right.
Congratulations you're all up and running with a Red5 Server!
Sample Application
The following class shows a very simple example of starting a streaming feed to the server and then streaming the same data back from the server into a Video object on the stage.
The fundamentals are very simple:
Create a NetConnection object and connect to the server
Create a NetStream when the connection succeeds and publish the camera feed to the stream as a "record" type
package
{
import flash.display.Sprite;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.events.NetStatusEvent;
import flash.media.Camera;
import flash.media.Video;
public class Red5Experiment extends Sprite
{
//
// Constants
public static const RED5_URL : String = "rtmp://SERVERNAME/oflaDemo/test/";
//
// Variables
private var _netConnection : NetConnection;
private var _netStream : NetStream;
private var _recvStream : NetStream;
private var _url : String = RED5_URL;
private var _filename : String = "test-red5";
private var _video : Video;
public function Red5Experiment()
{
_netConnection = new NetConnection();
_netConnection.connect( _url );
_netConnection.addEventListener( NetStatusEvent.NET_STATUS, netStatusHandler, false, 0, true );
_video = new Video();
addChild( _video );
}
////////////////////////////////////////////////////////
// FUNCTIONALITY
//
private function start():void
{
//
// Setup the camera
var cam:Camera = Camera.getCamera();
cam.setLoopback(true);
cam.setMode(176,144,15);
cam.setKeyFrameInterval(10);
cam.setQuality(0,70);
//
// Start publishing the video stream
_netStream = new NetStream(_netConnection);
_netStream.addEventListener( NetStatusEvent.NET_STATUS, netStatusHandler, false, 0, true );
_netStream.attachCamera( cam );
_netStream.publish(_filename, "record");
//
// Grab live stream back from server
_recvStream = new NetStream(_netConnection);
_recvStream.play(_filename);
_video.attachNetStream(_recvStream);
//
// Alternatively, Use the local camera feed
// _video.attachCamera(cam);
}
////////////////////////////////////////////////////////
// EVENT HANDLERS
//
private function netStatusHandler( event:NetStatusEvent ):void
{
trace( "Red5Experiment::netStatusHandler():"+event.info.code );
switch ( event.info.code )
{
case "NetConnection.Connect.Success":
start();
break;
}
}
}
}
The files are recorded to a directory within the webapps folder on your server. If you've followed the paths above you'll find the file /usr/share/red5/webapps/oflaDemo/streams/test/test-red5.flv on your server.
You now should have a complete streaming / recording media setup. All running off an open source server!
EDIT: I've created a zip of the sample application files: red5_sampleapplication