jwplayer +ffmpeg+red5 实现摄像头的直播
研究了幾天,終于把直播功能做完了,頓時(shí)心里壓力少了許多。
起初,買了本視頻直播的書,看了一下,好麻煩,不知道從何做起。看有沒(méi)有簡(jiǎn)便的方法能夠?qū)崿F(xiàn)直播功能。要想通過(guò)簡(jiǎn)便的方法實(shí)現(xiàn),就必須要了解直播的原理。
思路:通過(guò)硬件(比如攝像頭,我這使用的是海康的攝像頭)采集視頻數(shù)據(jù)(視頻編碼是標(biāo)準(zhǔn)的視頻編碼H264),把視頻流推送到red5服務(wù)器(我用的red5服務(wù)器,你也可以使用nginx服務(wù)器,都一樣),在通過(guò)rtmp協(xié)議把推送到服務(wù)器的視頻流讀取過(guò)來(lái),再本地實(shí)現(xiàn)播放(我用的jwplayer播放器進(jìn)行播放,如何你想自己寫客戶端,可以通過(guò)actionscript語(yǔ)言去完成)
總結(jié)一下就是:如何通過(guò)jwplayer播放器播放推送到red5服務(wù)器的實(shí)時(shí)流。
有了思路,如何實(shí)現(xiàn):
1、red5服務(wù)器先搭建起來(lái),太簡(jiǎn)單了,此處不再贅述。
2、客戶端通過(guò)jwplayer播放器進(jìn)行播放,下載此插件。網(wǎng)上一搜,一大推。
?
剩下的問(wèn)題就是,如何把攝像頭的實(shí)時(shí)流推送到服務(wù)器。繼續(xù)網(wǎng)上搜,搜到了ffmpeg這個(gè)工具。這個(gè)工具就可以實(shí)現(xiàn)推流。
3、下載ffmpeg.exe
?
一切準(zhǔn)備就緒,就剩下代碼的實(shí)現(xiàn)。
服務(wù)端:red5服務(wù)器啟動(dòng),啥也不用動(dòng),最多下載個(gè)oflaDemo看看點(diǎn)播的效果。
客戶端:jwplayer通過(guò)rtmp協(xié)議讀取實(shí)時(shí)流。代碼如下:
jwplayer("jwplayer_flv").setup({
file: "rtmp://192.168.1.29/oflaDemo//hello",
flashplayer: '${ctx}/resource/yulan/jwplayer.flash.swf',
image : '',
primary : 'flash',
width : '800',
height : '600',
autostart: true,
mute: false,
repeat: false
});
file: "rtmp://192.168.1.29/oflaDemo//hello" ? 這句話可以暫時(shí)不用管,看到后面就會(huì)知道啥意思。
?
流的推送:ffmpeg
推送命令:
?
ffmpeg -i "rtsp://admin:admin123@192.168.10.91:554/h264/ch1/main/av_stream"? -b? 4096k ?-f flv -r 25 -s1280x720?-an"rtmp://localhost/oflaDemo/hello"
?
這句話如何在java代碼里實(shí)現(xiàn)呢?網(wǎng)上很多。
?
java代碼(下面的代碼是同事給的,畢竟代碼跑過(guò)一段時(shí)間,應(yīng)該沒(méi)啥問(wèn)題):
public void liveVideo() throws Exception {
String rtspVideo = "rtsp://admin:admin123@192.168.10.91:554/h264/ch1/main/av_stream";
String rtmpVideo = "rtmp://192.168.1.29/oflaDemo/hello";
String commend = FFMPEG+"ffmpeg -i " + "\"" + rtspVideo + "\" -b 4096k -f flv -r 25 -s 1280x720 -an " + "\"" + rtmpVideo + "\"";
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(commend);
StreamGobbler sg1 = new StreamGobbler(proc.getInputStream(), "Console");
StreamGobbler sg2 = new StreamGobbler(proc.getErrorStream(), "Error");
sg1.start();
sg2.start();
proc.waitFor();
proc.destroy();
}
?
public class StreamGobbler extends Thread { ?
InputStream is; ?
String type; ?
private Logger log = Logger.getLogger(this.getClass()); ?
private static int i = 1; ?
private static int j = 1; ?
public StreamGobbler(InputStream is, String type) { ?
? ?this.is = is; ?
? ?this.type = type; ?
} ?
public StreamGobbler() {
}
public void run() { ?
? ?try { ?
? ? ? ?InputStreamReader isr = new InputStreamReader(is); ?
? ? ? ?BufferedReader br = new BufferedReader(isr); ?
? ? ? ?i++; ?
? ? ? ?j++; ?
? ? ? ?String line = null; ?
? ? ? ?while ((line = br.readLine()) != null) { ?
? ? ? ? ? ?if (type.equals("Error")) { ?
// ? ? ? ? ? ? ? ?log.error(line);
// ? ? ? ? ? ?System.err.println(line);
? ? ? ? ? ?}else{ ?
// ? ? ? ? ? ? ? ?log.info(line); ?
// ? ? ? ? ? ?System.out.println(line);
? ? ? ? ? ?} ?
? ? ? ?} ?
? ?} catch (IOException ioe) { ?
? ? ? ?ioe.printStackTrace(); ?
? ?} ?
} ?
}?
?
從代碼可以看出,客戶端的“file: "rtmp://192.168.1.29/oflaDemo//hello" 這段代碼 和 java中"String rtmpVideo = "rtmp://192.168.1.29/oflaDemo/hello";" 這段代碼就對(duì)應(yīng)起來(lái)了。
不得不說(shuō),ffmpeg這個(gè)東西的確厲害。給實(shí)現(xiàn)直播功能省了不少事。
至此,一個(gè)直播功能就實(shí)現(xiàn)了。
說(shuō)明:這只是我個(gè)人對(duì)直播理解,有些可能不對(duì),我也是才看了幾天,理解上肯定有些偏頗。各位大牛勿噴啊。小弟心臟不好。如有問(wèn)題,可評(píng)論,吐槽。
最后在粘貼個(gè)直播的效果圖:
?
?
總結(jié)
以上是生活随笔為你收集整理的jwplayer +ffmpeg+red5 实现摄像头的直播的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: h5打开手机扫码功能
- 下一篇: 井字棋博弈问题