Java 監(jiān)控直播流rtsp協(xié)議轉(zhuǎn)rtmp、hls窃躲、httpflv協(xié)議返回瀏覽器
目錄
- 一:了解音視頻流協(xié)議:
- 二:方案一 rtsp 轉(zhuǎn)rtmp
- 1计贰、下載nginx + nginx-rtmp-module
- 3、cmd 到nginx根目錄啟動(dòng)nginx
- 8蒂窒、查攝像頭的rtsp協(xié)議格式
- 10躁倒、測試rtmp是否轉(zhuǎn)換成功
- 12、為什么放棄了用rtmp
- 四:方案三rtsp 轉(zhuǎn)httpflv(采用)
- 1洒琢、安裝nginx-flv-module
- 4.1 采用java代碼去執(zhí)行ffmepg命令
- 6秧秉、前端使用flv.js播放:
需求背景:
在做之前的項(xiàng)目的時(shí)候有一個(gè)對(duì)接攝像頭實(shí)時(shí)播放的需求,由于我們攝像頭的購買量不是很多衰抑,合笥康威視不給我們提供流媒體云服務(wù)器,所以需要我們自己去 一個(gè)去滿足我們能在瀏覽器看到監(jiān)控畫面呛踊。項(xiàng)目源代碼在以前公司沒有拷貝就不能復(fù)習(xí)砾淌,最近又在準(zhǔn)備面試,所以寫了這個(gè)博客來復(fù)盤和擴(kuò)展一下谭网,由于我現(xiàn)在沒有Liunx汪厨,我就用Windows來演示,生產(chǎn)環(huán)境還是要使用Liunx愉择,下面這些操作在Liunx也是一樣的流程劫乱,大家自行百度。
一:了解音視頻流協(xié)議:
<caption>媒體流協(xié)議對(duì)比</caption>
協(xié)議 | HttpFlv | RTMP | HLS | Dash |
---|---|---|---|---|
全稱 | FLASH VIDEO over HTTP | Real Time Message Protocol | HTTP Living Streaming | |
傳輸方式 | HTTP長連接 | TCP長連接 | HTTP短連接 | HTTP短連接 |
視頻封裝格式 | FLV |
FLV TAG
| TS文件 |
Mp4
3gp
webm
|
| 原理 |
同 RTMP 薄辅,使用HTTP協(xié)議(80端口)
|
每個(gè)時(shí)刻的數(shù)據(jù)收到后立刻轉(zhuǎn)發(fā)
|
集合一段時(shí)間的數(shù)據(jù),生成TS切片文件(三片)抠璃,并更新m3u8索引
| |
| 延時(shí) |
低
1~3秒
|
低
1~3秒
|
高
5~20秒(依切片情況)
| 高 |
| 數(shù)據(jù)分段 | 連續(xù)流 | 連續(xù)流 | 切片文件 | 切片文件 |
| Html5播放 |
可通過HTML5解封包播放
(flv.js)
| 不支持 |
可通過HTML5解封包播放
(hls.js)
|
如果dash文件列表是MP4站楚,
webm文件,可直接播放
|
| 其它 |
需要Flash技術(shù)支持搏嗡,不支持多音頻流窿春、多視頻流,不便于seek(即拖進(jìn)度條)
|
跨平臺(tái)支持較差采盒,需要Flash技術(shù)支持
|
播放時(shí)需要多次請(qǐng)求旧乞,對(duì)于網(wǎng)絡(luò)質(zhì)量要求高
| |
二:方案一 rtsp 轉(zhuǎn)rtmp
1、下載nginx + nginx-rtmp-module
nginx:下載地址: http://nginx-win.ecsds.eu/download/
nginx-rtmp-module:nginx 的擴(kuò)展磅氨,安裝后支持rtmp協(xié)議尺栖,下載地址: https://github.com/arut/nginx-rtmp-module
解壓nginx-rtmp-module到nginx根目錄下,并修改其文件夾名為nginx-rtmp-module(原名為nginx-rtmp-module-master)
2烦租、nginx配置文件
到nginx根目錄下的conf目錄下復(fù)制一份nginx-win.conf 重命名 nginx-win-rtmp.conf
nginx-win-rtmp.conf:
<pre class="prettyprint hljs cmake" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#user nobody;
multiple workers works !
worker_processes 2;
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 8192;
# max value 32768, nginx recycling connections+registry optimization =
# this.value * 20 = max concurrent connections currently tested with one worker
# C1000K should be possible depending there is enough ram/cpu power
# multi_accept on;
}
rtmp {
server {
listen 1935;
chunk_size 4000;
application live {
live on;
# 播放時(shí)進(jìn)行回調(diào)延赌,如果HttpRespone statusCode不等于200會(huì)斷開
# on_play http://localhost:8081/auth;
}
application hls {
live on;
# 開啟hls切片
hls on;
# m3u8地址
hls_path html/hls;
# 一個(gè)切片多少秒
hls_fragment 8s;
# on_play http://localhost:8081/auth;
# on_publish http://localhost:8081/auth;
# on_done http://localhost:8081/auth;
}
}
}
http {
#include /nginx/conf/naxsi_core.rules;
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr:$remote_port - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
# loadbalancing PHP
upstream myLoadBalancer {
server 127.0.0.1:9001 weight=1 fail_timeout=5;
server 127.0.0.1:9002 weight=1 fail_timeout=5;
server 127.0.0.1:9003 weight=1 fail_timeout=5;
server 127.0.0.1:9004 weight=1 fail_timeout=5;
server 127.0.0.1:9005 weight=1 fail_timeout=5;
server 127.0.0.1:9006 weight=1 fail_timeout=5;
server 127.0.0.1:9007 weight=1 fail_timeout=5;
server 127.0.0.1:9008 weight=1 fail_timeout=5;
server 127.0.0.1:9009 weight=1 fail_timeout=5;
server 127.0.0.1:9010 weight=1 fail_timeout=5;
least_conn;
}
sendfile off;
#tcp_nopush on;
server_names_hash_bucket_size 128;
Start: Timeouts
client_body_timeout 10;
client_header_timeout 10;
keepalive_timeout 30;
send_timeout 10;
keepalive_requests 10;
End: Timeouts
#gzip on;
server {
listen 5080;
server_name localhost;
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root nginx-rtmp-module/;
}
location /control {
rtmp_control all;
}
location /hls {
# Serve HLS fragments
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
expires -1;
add_header Access-Control-Allow-Origin *;
}
#charset koi8-r;
#access_log logs/host.access.log main;
## Caching Static Files, put before first location
#location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
# expires 14d;
# add_header Vary Accept-Encoding;
#}
For Naxsi remove the single # line for learn mode, or the ## lines for full WAF mode
location / {
#include /nginx/conf/mysite.rules; # see also http block naxsi include line
##SecRulesEnabled;
##DeniedUrl "/RequestDenied";
##CheckRule "$SQL >= 8" BLOCK;
##CheckRule "$RFI >= 8" BLOCK;
##CheckRule "$TRAVERSAL >= 4" BLOCK;
##CheckRule "$XSS >= 8" BLOCK;
root html;
index index.html index.htm;
}
For Naxsi remove the ## lines for full WAF mode, redirect location block used by naxsi
##location /RequestDenied {
## return 412;
##}
Lua examples !
location /robots.txt {
rewrite_by_lua '
if ngx.var.http_host ~= "localhost" then
return ngx.exec("/robots_disallow.txt");
end
';
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000; # single backend process
# fastcgi_pass myLoadBalancer; # or multiple, see example above
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl spdy;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_prefer_server_ciphers On;
# ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
# ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:ECDH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!eNULL:!MD5:!DSS:!EXP:!ADH:!LOW:!MEDIUM;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}</pre>
3除盏、cmd 到nginx根目錄啟動(dòng)nginx
<pre class="hljs vim" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">nginx.exe -c conf\nginx-win-rtmp.conf</pre>
測試:瀏覽器輸入 http://localhost:5080/stat,看到
代表安裝成功
4挫以、下載ffmpeg安裝
ffmpeg:一個(gè)處理音視頻強(qiáng)大的庫者蠕,我們需要用它來轉(zhuǎn)協(xié)議,下載地址: https://www.gyan.dev/ffmpeg/builds/ 掐松,這里可以下載essential和full版本踱侣,essential就是簡版,只包含ffmpeg.exe大磺、ffplay.exe抡句、
ffprobe.exe, 而full版本就包含了動(dòng)態(tài)庫和相關(guān)頭文件量没,方便我們?cè)陂_發(fā)中調(diào)用玉转。
5、配置ffmpeg環(huán)境變量
將ffmpeg解壓后里面的bin路徑復(fù)制到Path里面去
6殴蹄、測試ffmpeg
cmd ffmpeg -version
命令看到代表成功
7究抓、下載VLC播放器
下載地址: https://www.videolan.org/vlc/
8、查攝像頭的rtsp協(xié)議格式
我這里截圖是合疲康威視的
現(xiàn)在沒有測試的流刺下,我找了個(gè)點(diǎn)播的rtsp
rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4,用這個(gè)代替是一樣的
9稽荧、執(zhí)行ffmpeg命令
ffmpeg強(qiáng)大橘茉,命令也是復(fù)雜,我們cmd 執(zhí)行
<pre class="prettyprint hljs groovy" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">ffmpeg -re -rtsp_transport tcp -stimeout 20000000 -i "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4" -buffer_size 1024000 -max_delay 500000 -codec:v libx264 -r 25 -rtbufsize 10M -s 1280x720 -map:v 0 -an -f flv rtmp://127.0.0.1:1935/live/test</pre>
rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4姨丈,是輸入源頭
rtmp://127.0.0.1:1935/live/test 是輸出地址
如果沒有報(bào)錯(cuò)的話畅卓,到現(xiàn)在rtsp就已經(jīng)轉(zhuǎn)換好了
ffmpeg命令學(xué)習(xí): http://www.reibang.com/p/df3216a52e59 、 https://blog.csdn.net/fuhanghang/article/details/123565920
10蟋恬、測試rtmp是否轉(zhuǎn)換成功
我們打開VLC翁潘,媒體->打開網(wǎng)絡(luò)串流->輸入 rtmp://127.0.0.1:1935/live/test
-> 播放
11、測試是否成功
等待幾秒鐘看到有視頻播放就是成功了
12歼争、為什么放棄了用rtmp
rtmp的優(yōu)點(diǎn)是延遲低拜马,效率高,但是在瀏覽器需要安裝flash才能放沐绒,也就老版本的瀏覽器在用俩莽,rtmp可能會(huì)在別的地方支持,所以還是把他方式方法貼出來了乔遮。
三:方案二 rtsp轉(zhuǎn)hls
1扮超、nginx配置:
在前面已經(jīng)貼出來了,其中這幾個(gè)是針對(duì)hls的
2、執(zhí)行ffmepg命令
<pre class="prettyprint hljs awk" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">ffmpeg -i "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4" -vcodec libx264 -acodec aac -f flv rtmp://127.0.0.1:1935/hls/test</pre>
3瞒津、查看nginx根目錄 -> hls -> test.m3u8 是否生成
生成了代表一切正常
4蝉衣、m3u8在網(wǎng)頁上播放
<pre class="prettyprint hljs xml" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>前端播放m3u8格式視頻</title>
<link rel="stylesheet">
<script src="https://cdn.bootcss.com/video.js/6.6.2/video.js"></script>
<script src="https://cdn.bootcss.com/videojs-contrib-hls/5.15.0/videojs-contrib-hls.min.js"></script>
</head>
<body>
<video id="myVideo" class="video-js vjs-default-skin vjs-big-play-centered" controls preload="auto" width="1080" height="708" data-setup='{}'>
<source id="source" src="http://127.0.0.1:5080/hls/test.m3u8" type="application/x-mpegURL">
</video>
</body>
<script> // videojs 簡單使用
var myVideo = videojs('myVideo',{
bigPlayButton : true,
textTrackDisplay : false,
posterImage: false,
errorDisplay : false,
})
myVideo.play() // 視頻播放
myVideo.pause() // 視頻暫停 </script>
</html></pre>
source標(biāo)簽的src屬性: http://你的nginx ip:nginx http端口/hls/test.m3u8
rtsp轉(zhuǎn)HLS成功!
5巷蚪、認(rèn)識(shí)一下m3u8格式
m3u8文件里面存儲(chǔ)了一個(gè)索引病毡,以文本格式打開是這樣的
<pre class="prettyprint hljs css" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#EXTM3U
EXT-X-VERSION:3
EXT-X-MEDIA-SEQUENCE:56
EXT-X-TARGETDURATION:13
EXTINF:10.381,
test-56.ts
EXTINF:10.422,
test-57.ts
EXTINF:13.453,
test-58.ts</pre>
m3u8文件它不是視頻源,源頭是ts后綴文件
6屁柏、為什么放棄了用HLS
轉(zhuǎn)HLS協(xié)議及網(wǎng)頁加載過程:
ffmepg收到rtsp的流時(shí)候啦膜,會(huì)等一個(gè)切片的時(shí)間,一個(gè)切片時(shí)間到了淌喻,切片ts會(huì)放到服務(wù)器中僧家,同時(shí)m3u8文件中加一個(gè)索引,對(duì)應(yīng)著新進(jìn)入的切片裸删。網(wǎng)頁在加載m3u8的時(shí)候八拱,就是讀取m3u8中的的索引去加載ts文件,所以在不斷的請(qǐng)求ts涯塔,對(duì)ts進(jìn)行解析肌稻,不斷的和TCP握手,這就是為什么HLS延遲高和對(duì)網(wǎng)速的要求高的原因匕荸,我們監(jiān)控肯定是要延遲低的爹谭,HLS兼容性好,適合點(diǎn)播榛搔。
四:方案三rtsp 轉(zhuǎn)httpflv(采用)
1诺凡、安裝nginx-flv-module
這個(gè)插件需要編譯,教程: https://blog.csdn.net/KayChanGEEK/article/details/105095844
我這里已經(jīng)編譯好了践惑,直接下載啟動(dòng):
https://gitee.com/isyuesen/nginx-flv-file
2腹泌、nginx配置
看我git里面的https://gitee.com/isyuesen/nginx-flv-file/blob/master/conf/nginx.conf,和默認(rèn)的config差別主要是添加了這幾個(gè)
<pre class="prettyprint hljs nginx" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">rtmp {
server {
listen 1935;
# 流復(fù)用的最大塊大小
chunk_size 4000;
application liveapp {
live on;
# 推流開始
on_publish http://localhost:8081/auth;
# 推流關(guān)閉
on_publish_done http://localhost:8081/auth;
# 客戶端開始播放
on_play http://localhost:8081/auth;
# 客戶端結(jié)束播放
on_play_done http://localhost:8081/auth;
}
}
}</pre>
<pre class="prettyprint hljs nginx" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">location /live {
flv_live on;
chunked_transfer_encoding on;
add_header 'Access-Control-Allow-Credentials' 'true'; #add additional HTTP header
add_header 'Access-Control-Allow-Origin' '*'; #add additional HTTP header
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
add_header 'Cache-Control' 'no-cache';
}</pre>
3尔觉、做java權(quán)限認(rèn)證
nginx rtmp配置中有配置on_publish鉤子接口 http://localhost:8081/auth凉袱,這個(gè)回調(diào)HttpResponse stausCode如果不等于200會(huì)拒絕I/O,更多回調(diào)鉤子看: https://github.com/arut/nginx-rtmp-module/wiki/Directives#on_connect
<pre class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@PostMapping("/auth")
public void getVideo(String token, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
if (token.equals("tokenValue")) {
httpServletResponse.setStatus(200);
} else {
// 拒絕服務(wù)
httpServletResponse.setStatus(500);
}
}</pre>
4穷娱、執(zhí)行ffmepg命令:
<pre class="prettyprint hljs bash" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">ffmpeg -re -rtsp_transport tcp -i "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4" -f flv -vcodec h264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 640*360 -q 10 "rtmp://127.0.0.1:1935/liveapp/test"</pre>
4.1 采用java代碼去執(zhí)行ffmepg命令
依賴 javaCV
<pre class="prettyprint hljs xml" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5.2</version>
</dependency></pre>
<pre class="prettyprint hljs dart" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class App {
public static void main( String[] args ) throws IOException, InterruptedException {
String name = "test";
// rtsp地址
String rtspDir = "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4";
// rtmp地址
String rtmpDir = "rtmp://192.168.0.140:1935/liveapp/" + name + "?token=tokenValue";
String ffmpeg = Loader.load(org.bytedeco.ffmpeg.ffmpeg.class);
ProcessBuilder pb = new ProcessBuilder(ffmpeg,
"-re",
"-rtsp_transport",
"tcp",
"-i",
rtspDir,
"-f",
"flv",
"-vcodec",
"h264",
"-vprofile",
"baseline",
"-acodec",
"aac",
"-ar",
"44100",
"-strict",
"-2",
"-ac",
"1",
"-f",
"flv",
"-s",
"640*360",
"-q",
"10",
rtmpDir
);
pb.inheritIO().start().waitFor();
}
}</pre>
5绑蔫、測試http-flv鏈接
如果你跟著我做的运沦,那鏈接就是 http://127.0.0.1:18080/live?port=1935&app=liveapp&stream=test&token=tokenValue泵额,在VLC播放器中點(diǎn)擊媒體 -> 打開網(wǎng)絡(luò)串流 -> 輸入http://127.0.0.1:18080/live?port=1935&app=liveapp&stream=test&token=tokenValue -> 播放
有視頻證明你離成功就差最后一步
6、前端使用flv.js播放:
<pre class="prettyprint hljs xml" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>播放http-flv</title>
</head>
<body>
<video id="videoElement"></video>
<script src="https://cdn.bootcdn.net/ajax/libs/flv.js/1.6.2/flv.min.js"></script>
<script> if (flvjs.isSupported()) {
const videoElement = document.getElementById('videoElement');
const flvPlayer = flvjs.createPlayer({
type: 'flv',
url: 'http://127.0.0.1:18080/live?port=1935&app=liveapp&stream=test&token=tokenValue'
});
flvPlayer.attachMediaElement(videoElement);
flvPlayer.load();
flvPlayer.play();
} </script>
</body>
</html></pre>