linux下fms2流媒體伺服器搭建之六—–線上視頻錄製篇

來源:互聯網
上載者:User
    視頻錄製的源碼好像網上有很多,在此聲明,我這個也是抄別人的然後改改最佳化了一下,有點累,不多說了,直接貼源碼吧:

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 );
    }
  }; 
};

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.