linux下fms2流媒体服务器搭建之六-----在线视频录制篇

    视频录制的源码好像网上有很多,在此声明,我这个也是抄别人的然后改改优化了一下,有点累,不多说了,直接贴源码吧:


var server_uri:String = "rtmp://215.102.25.XX/gdrc/video";
//FMS地址;跟播放器那边设置一样

var videoName:String = "record";//录制的文件名,可以通过参数传递

var my_nc:NetConnection = new NetConnection ();
//新建一个连接对象,用来连接FMS;

my_nc.connect (server_uri);
//连接FMS;

var my_ns:NetStream = new NetStream (my_nc);
//新建流,用来捕获和播放视频;

var my_cam:Camera = Camera.get ();
video_r.attachVideo (my_cam);
//获得慑像头,并将摄像头获取的视频载入到video_r里.

//my_cam.setQuality (1024, 90);
//设置带宽和质量;

var my_mic:Microphone = Microphone.get ();
//获得MIC;

my_ns.attachVideo (my_cam);
my_ns.attachAudio (my_mic);
//将摄像头图象和MIC声音装载到流.

video_p._visible = false;
//将播放窗口设置为不可见.

//默认上网方式为adsl,这个设置需要在服务器端的main.asc添加一些代码,最后补上
updateBandwidth(2);

function updateBandwidth(b) {
 if ( b == 1 ) { //modem上网方式设置
  my_cam.setMode(160,120,2);0
  my_cam.setQuality(0, 75);
  my_cam.setKeyFrameInterval(3);
  my_mic.setRate(5);
  video_r._height = 100;
  video_r._width = 150;
 }
 else if ( b == 2 ) { //adsl上网方式设置
  my_cam.setMode(160,120,5);
  my_cam.setQuality(0, 85);
  my_cam.setKeyFrameInterval(5);
  my_mic.setRate(11);
  video_r._height = 130;
  video_r._width = 175;
 }
 else { //LAN专线上网方式设置
  my_cam.setMode(160,120,15);
  my_cam.setQuality(0, 90);
  my_cam.setKeyFrameInterval(10);
  my_mic.setRate(22);
  video_r._height = 150;
  video_r._width = 200;
 }
 my_ns.call('setBandwidth', 0, b);
}
/*本来做好了几个按钮可以让用户自己选择上网方式,但是实在太丑了就取消了,可以自行添加
Modem_btn.onPress = function() {
 updateBandwidth(1);
}

DSL_btn.onPress = function() {
 updateBandwidth(2);
}

LAN_btn.onPress = function() {
 updateBandwidth(3);
}
*/

my_nc.onStatus = function (thisObj:Object)
{
 //trace (thisObj.code);
 if (thisObj.code == "NetConnection.Connect.Success")
 {
  RecordStop ("空闲中");
 }
 else
 {
  trace ("错误:服务器未启动或URI输入错误.");
 }
}

function Record ()
{
 video_r._visible = true;
 video_p._visible = false;
 my_ns.publish (videoName, "record");
 StreamInfo ("NowRecord");
 btn_pr.btn_text.text = "停止";
 RecordInterval = setInterval (RecordTime, 1000);
 txt_videoname.text = "视 频 名:" + videoName;
}

var timeNum:Number = 0;

function RecordTime ()
{
 timeNum ++;
 txt_status.text = "正在录制" + " " + "时间:" + Math.floor (timeNum / 60) + "分" + (timeNum % 60) + "秒";
}

function RecordStop (__status:String)
{
 txt_status.text = "当前状态:" + __status;
 btn_pr.btn_text.text = "录制";
 StreamInfo ("NowStop");
 clearInterval (RecordInterval);
 timeNum = 0;
 if (__status == "录制完成")
 {
  Finished ();
 }
 else
 {
  trace ("a");
 }
}


function Finished ()
{
 my_ns.close();
 video_r.clear ();
 var thisObj = this;
 var FinishAlert:MovieClip = _root.attachMovie ("Alert", "FinishAlert", 100);
 with (FinishAlert)
 {
  alertTitle.html = true;
  alertTitle.htmlText = "<b>" + "录制完成!" + "</b>";
  _x = video_r._x + (video_r._width - _width) / 2;
  _y = video_r._y + (video_r._height - _height) / 2;
  info.text = "录制完成..请选择下一步操作."
  btn_sub.btn_txt.text = "播放";
  btn_cln.btn_txt.text = "重录";
  btn_sub.onRelease = function ()
  {
   thisObj.PlayVideo ();
   thisObj.closeAlert (FinishAlert);
  }
  btn_cln.onRelease = function ()
  {
   thisObj.Record ();
   thisObj.closeAlert (FinishAlert);
  }
  closed.onRelease = function ()
  {
   thisObj.closeAlert (FinishAlert);
  }
 }
 btn_pr.enabled = false;
}

function closeAlert (__alert:MovieClip)
{
 btn_pr.enabled = true;
 unloadMovie (__alert);
}

function StreamInfo (__status:String)
{
 btn_pr.onRelease = function ()
 {
  if (__status == "NowRecord")
  {
   RecordStop ("录制完成");
  }
  else if (__status == "NowStop")
  {
   Record ();
  }
  else if (__status == "NowPlay")
  {
   Record ();
  }
 }
}

function PlayVideo ()
{
 txt_status.text = "当前状态:" + "正在播放...";
 StreamInfo ("NowPlay")
 video_r._visible = false;
 video_p._visible = true;
 video_p.attachVideo (my_ns);
 my_ns.play ("record");
 btn_pr.btn_text.text = "重录";
 my_ns.onStatus = function (PlayInfo)
 {
  if (PlayInfo.code == "NetStream.Play.Stop")
  {
   my_ns.seek (0);
   my_ns.pause();
   txt_status.text = "当前状态:" + "播放完成...";
  }
 }
}

服务器端main.asc添加后的完整内容:

load("components.asc");

application.onAppStart = function() {};
application.onConnect = function(client) {
 application.acceptConnection(client);
 client.getFileLength = function(filename) {
  var tlen = Stream.length(filename);
  return tlen;
 };
 
 Client.prototype.setBandwidth = function(bw) {
   if ( bw == 1 ) { // modem settings
  this.setBandwidthLimit( 35000/8, 22000/8 );
   }
   else if ( bw == 2 ) { // DSL settings
   this.setBandwidthLimit( 800000/8, 100000/8 );
    }
    else { // LAN settings
   this.setBandwidthLimit( 400000, 400000 );
    }
  }; 
};

你可能感兴趣的:(linux)