用linux建立自己的mp3廣播站

來源:互聯網
上載者:User

概述:本文將介紹用linux下的開放源碼工具icecast和ices建立mp3廣播的方法。

原理:

  icecast是一個基於Mpeg III技術的Internet廣播系統,同shoutcast等商業軟體相比具有以下特點:

  1. Icecast是一個純粹的OpenSource軟體;
  2. 不需要運行自己的目錄伺服器;
  3. 佔用更少的系統資源;

  我們可以在伺服器上運行自己的流伺服器——icecast,使用ices提供流源,用戶端則可以使用最常用的winamp。

實驗環境:

  伺服器平台:RedHat 7.0

  流伺服器:icecast

  流源:ices

  mp3編碼工具:lame

步驟:

1. 獲得相關軟體

  流伺服器icecast:http://www.icecast.org/releases/icecast-1.3.10-1.i386.rpm

  流源工具ices:http://www.icecast.org/releases/ices-0.0.1.beta5.tar.gz

  編碼工具lame:http://home.elka.pw.edu.pl/~dmierzej/linux/lame/lame-3.87beta-1.i386.rpm

  lame庫:http://home.elka.pw.edu.pl/~dmierzej/linux/lame/lame-devel-3.87beta-1.i386.rpm

說明:

  A. 通常可以使用liveice、shout等軟體為流伺服器提供流(即作為流源),但是我在使用的過程中發現ices要穩定一些,雖然還是個beta版的。

  B. 通常你的每個mp3檔案不可能具有同樣屬性,此時廣播過程就會出現一些怪現象,比如聲音斷斷續續、有噪音等等,所以我們必須使用mp3編碼工具lame重新對其進行編碼,來產生屬性一致的mp3檔案。

2. 安裝icecase

  使用以下命令安裝icecast:

  rpm –ivh icecast-1.3.10-1.i386.rpm

3. 安裝ices

  使用以下命令安裝ices:

  # tar xvzf ices-0.0.1.beta5.tar.gz

  會在目前的目錄下產生ices-0.0.1.beta5目錄,用cd命令進入該目錄

  # cd ices-0.0.1.beta5

  然後執行:

  # ./configure  # make  # make install

  編譯好的檔案預設被安裝在/usr/local/icecast目錄下

4. 安裝lame

  執行以下命令來安裝lame mp3編碼工具,該工具用來重新對mp3檔案進行編碼,以產生屬性一致的mp3檔案。

  rpm –ivh lame-3.87beta-1.i386.rpm  rpm –ivh lame-devel-3.87beta-1.i386.rpm

5. 配置icecast

  icecast的主要設定檔是/etc/icecast.conf,有很多配置選項,但是其中的大多數只要採用預設配置即可,部分選項意義說明如下:

  Port:定義icecast服務綁定的連接埠,只設定一個即可,通常使用8000連接埠。

  encoder_password:流源串連時的密碼,使用ices為icecast提供流時ices必須提供提供這個密碼,出於安全性考慮可以設定一個較為複雜的密碼。

  admin_password:可以通過telnet登入icecast進行遠端管理,這個密碼就是登入icecast時必須輸入的密碼。

  oper_password:通過telnet登入icecast執行限制性命令時的密碼。

  max_clients:同一時間內允許串連到流伺服器的最大客戶數目。

  max_sources:允許同時廣播的最大流源數目,流源和流伺服器可以位於不同的機器上,並且可以將多個流源傳輸到同一個流伺服器進行廣播。

  max_admins:可以同時登入icecast進行遠端管理的串連數目。

  Logfile:設定記錄檔的位置。

  Hostname:設定icecast綁定的ip地址,預設綁定到所有ip地址。

  server_name:指定icecast伺服器的主機名稱或者是ip地址。

  client_timeout:設定當無流源時,伺服器斷開已串連客戶的逾時時間。

  stats_log:設定記錄icecast統計資料的記錄檔。

  stats_time:設定stats_log記錄檔的更新周期。

  max_clients_per_source:設定可以同時串連到同一流源的最大客戶數目。

  reverse_lookups:設定是否對可以串連進行反向DNS解析,為0時禁止反向DNS解析,為1時啟用反向dns解析。

  Location:設定icecast伺服器的地理位置,設定與否並不重要。

  rp_email:設定icecast伺服器管理員的郵件地址。

  Throttle:icecast伺服器可以使用的頻寬閥值,如果達到這一閥值,新的客戶串連將被拒絕,單位是兆/秒。

  Logfiledebuglevel:定義日誌的資訊詳細程度。

  server_url:定義icecast伺服器的RUL

  以下是我的設定檔,僅供參考:

  #icecast.conf  location Yuxi Yunnan China  rp_email bye2000@yxtc.edu.cn  server_url http://www.yxtc.edu.cn/  max_clients 900  max_clients_per_source 900  max_sources 10  max_admins 5  throttle 10.0  mount_fallback 1  encoder_password secret  admin_password secret  oper_password secret  port 8000  force_servername 0  logfile icecast.log  accessfile access.log  usagefile usage.log  logfiledebuglevel 0  consoledebuglevel 0  reverse_lookups 0  console_mode 0  client_timeout 30  kick_clients 0  staticdir /usr/share/icecast/static  templatedir /usr/share/icecast/templates  logdir /var/log/icecast  stats_log stats.log  statshtml_log stats.html  stats_time 60  kick_relays 10  transparent_proxy 0  acl_policy 1

6. 啟動icecast服務

  執行以下指令碼啟動icecast服務:

  # /etc/rc.d/init.d/icecast start

7. 對所有mp3檔案進行重新編碼

  採用lame對mp3檔案進行重新編碼,從而產生屬性一致的mp3檔案。

  這要用到Lame命令的以下參數:

  --mp3input:輸入檔案為mp3檔案

  -m s:指定所有幀均為立體聲

  -b 56:指定新產生mp3檔案的傳輸速率,這裡指定為56 kbps,單位是kbps

  --resample 24:指定新產生mp3檔案的採樣頻率,這裡指定為24 KHZ,單位是KHZ

  下面是一個lame的使用執行個體:

  lame --mp3input -m s -b 56 --resample 24 old.mp3 new.mp3

8. 產生播放清單

  可以用以下檔案產生播放清單:

  # find / -name ‘*.mp3’ > play_list_file

  將其中的play_list_file改成你的播放清單檔案名稱。

  以下是一個播放清單檔案樣本:

  #play_list example  /data/mp3/PowerStation/byemylove/1.mp3  /data/mp3/PowerStation/byemylove/10.mp3  /data/mp3/PowerStation/byemylove/11.mp3  /data/mp3/PowerStation/byemylove/2.mp3  /data/mp3/PowerStation/byemylove/3.mp3  /data/mp3/PowerStation/byemylove/4.mp3  /data/mp3/PowerStation/byemylove/5.mp3  /data/mp3/PowerStation/byemylove/6.mp3

  可以看出,只要包含mp3檔案的完整路徑和檔案名稱即可。

9. 發布流

  ices可以使用基於xml的設定檔,也可以使用帶選項的命令,這裡我們使用後者,常用的命令選項有:

  -B:指定ices以背景程式方式運行

  -b:指定使用的傳輸速率

  -d:對流的描述

  -n:設定廣播站的名稱

  -g:廣播音樂的類型

  -h:流伺服器ip地址

  -p:流伺服器監聽連接埠

  -P:串連流伺服器的密碼

  -r:設定全部隨機播放歌曲

  -S:指定播放清單類型,通常使用builtin

  -U:指定流伺服器的URL

  -F:指定播放清單檔案

  我的例子:

  /usr/local/icecast/bin/ices –B –b 56 –d MUSIC      –n “TOM Radio” –g music –h localhost  –p 8000 –P secrue –r –S builtin –U  http://www.yxtc.edu.cn -F /etc/icecast/list

10. 接收廣播

  可以用類似以下的方法讓使用者接收廣播:

  建立一個副檔名為m3u的檔案,比如我的檔案是tom.m3u,該檔案包含以下內容:

  http://www.yxtc.edu.cn:8000/

  然後在首頁上建立該檔案的超連結,這樣客戶一旦點擊該串連,就會啟動winamp、realplay等mp3播放器接收廣播了。

  推薦使用winamp接收廣播。

  好了,大功告成了,坐下來泡杯咖啡慢慢欣賞你的mp3廣播吧。

11. 補充說明:

  我寫了一個perl指令碼,該指令碼會自動搜尋系統中所有的mp3檔案,然後將非56K傳輸速率和24KHZ採樣頻率的mp3檔案用lame轉換成56K傳輸速率和24KHZ採樣頻率的mp3檔案,完成轉換後自動產生播放清單。

  使用方法:

  ./list_gen  list_file

  必須先用touch 命令建立該列表檔案

  該指令碼要求你的系統安裝lame工具。

  該指令碼源碼如下,你也可以從這裡下載:

  #!/usr/bin/perl    `rm -f /tmp/temp.mp3`;  if ( ! $ARGV[0] ) {      print " Usage: ./list_gen list_file_name ";      print "You must create the file first! ";      exit(0);  }  $files = `find / -name '*.mp3'`;  @file = split(/ +/,$files);  $num = @file;  for ($i=0; $i<=$num-1; $i++) {      $file_info=`file $file[$i]`;      if ( $file_info=~/MP3/) {          @file_type=split(/s+/,$file_info);          if ( $file_type[2]=~/56/ ){           }else{      `lame --mp3input -m s -b 56 --resample 24 $file[$i] /tmp/temp.mp3`;      `mv -f  /tmp/temp.mp3  $file[$i]`;          }          push (@list,"$file[$i] ");      }  }  if ( -e $ARGV[0] ) {      $num = @list;      open(LIST,">$ARGV[0]");      for ( $i=0; $i<=$num; $i++ ) {  print LIST "$list[$i]";      }      close(LIST);      print " play list create OK, good luck!!! ";      }else{      print " I can't find file you specified! ";      print "You must create play list file first! ";        }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.