Go語(yǔ)言利用ffmpeg轉(zhuǎn)hls實(shí)現(xiàn)簡(jiǎn)單視頻直播
1. 前言
上一次我們找到一些開(kāi)源方案,目前我們先測(cè)試一下ffmpeg轉(zhuǎn)hls播放的方式,看下延遲情況及兼容性情況,主要測(cè)試Windows、Linux和macOS中使用谷歌瀏覽器播放的情況。
后端結(jié)合我們之前的cgo部分,建立一個(gè)簡(jiǎn)單的http服務(wù)器,然后提供給前端調(diào)用。
2. wsl安裝ffmpeg并轉(zhuǎn)換rtsp為hls
sudo apt-get install ffmpeg
可能報(bào)錯(cuò):
“E: Failed to fetch http://archive.ubuntu.com/ubuntu/pool/universe/f/flite/libflite1_2.1-release-3_amd64.deb Connection failed [IP: 91.189.88.142 80]”
解決辦法,可以選擇直接源碼編譯安裝:
wget https://ffmpeg.org/releases/ffmpeg-4.1.tar.bz2 tar -xjvf ffmpeg-4.1.tar.bz2 cd ffmpeg-4.1 sudo apt-get install yasm ./configure make && sudo make install ffmpeg -version
ffmpeg轉(zhuǎn)換rtsp為hls:
ffmpeg -i "rtsp://username:password@40.40.40.101/media/video1" -c copy -f hls -hls_time 2.0 -hls_list_size 0 -hls_wrap 15 "./test.m3u8"
3. 前后端示例代碼
3.1 后端go代碼
我們使用go創(chuàng)建簡(jiǎn)單的http服務(wù),然后利用ffmpg轉(zhuǎn)換hls提供給前端。
需要鑒權(quán)時(shí)rtsp地址前加上用戶名密碼時(shí)即可,比如rtsp://username:password@xxx,用戶名和密碼之間用:隔開(kāi),和原本的地址用@隔開(kāi)。
main.go:
import (
"fmt"
"net/http"
"os/exec"
"bytes"
"io/ioutil"
)
func Index(w http.ResponseWriter, r *http.Request) {
content, _ := ioutil.ReadFile("./index.html")
w.Write(content)
}
func main () {
http.HandleFunc("/index", Index)
http.Handle("/", http.FileServer(http.Dir(".")))
go func() {
http.ListenAndServe(":9000", nil)
}()
cmd := exec.Command("ffmpeg", "-i", "rtsp://admin:admin@40.40.40.101/media/video1", "-c", "copy", "-f", "hls", "-hls_time", "2.0", "-hls_list_size", "0", "-hls_wrap", "15", "./test.m3u8")
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
fmt.Println(fmt.Sprint(err) + ": " + stderr.String())
return
}
fmt.Println("Result: " + out.String())
}
3.2 前端代碼
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>前端播放m3u8格式視頻</title>
<link rel="external nofollow" rel="stylesheet">
<script src='https://vjs.zencdn.net/7.4.1/video.js'></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/videojs-contrib-hls/5.15.0/videojs-contrib-hls.min.js" type="text/javascript"></script>
<!-- videojs-contrib-hls 用于在電腦端播放 如果只需手機(jī)播放可以不引入 -->
</head>
<body>
<style>
.video-js .vjs-tech {position: relative !important;}
</style>
<div>
<video id="myVideo" class="video-js vjs-default-skin vjs-big-play-centered" controls preload="auto" data-setup='{}' style='width: 100%;height: auto'>
<source id="source" src="http://localhost:9000/test.m3u8" type="application/x-mpegURL"></source>
</video>
</div>
<!--<div class="qiehuan" style="width:100px;height: 100px;background: red;margin:0 auto;line-height: 100px;color:#fff;text-align: center">切換視頻</div>-->
</body>
<script>
// videojs 簡(jiǎn)單使用
var myVideo = videojs('myVideo', {
bigPlayButton: true,
textTrackDisplay: false,
posterImage: false,
errorDisplay: false,
})
myVideo.play()
var changeVideo = function (vdoSrc) {
if (/\.m3u8$/.test(vdoSrc)) { //判斷視頻源是否是m3u8的格式
myVideo.src({
src: vdoSrc,
type: 'application/x-mpegURL' //在重新添加視頻源的時(shí)候需要給新的type的值
})
} else {
myVideo.src(vdoSrc)
}
myVideo.load();
myVideo.play();
}
// var src = "./test.m3u8";
// document.querySelector('.qiehuan').addEventListener('click', function () {
// changeVideo(src);
// })
</script>
4. 結(jié)果及評(píng)估
運(yùn)行后端代碼后訪問(wèn)localhost:9000即可查看視頻,經(jīng)測(cè)試延遲還是比較高的(我測(cè)試大致在5s-8s),如果要加上ptz控制的話沒(méi)有實(shí)時(shí)感恐怕比較怪異,只適合簡(jiǎn)單的網(wǎng)絡(luò)直播之類的,不太在乎一定的延遲。

以上就是go后端利用ffmpeg轉(zhuǎn)hls做簡(jiǎn)單視頻直播的詳細(xì)內(nèi)容,更多關(guān)于Go ffmpeg轉(zhuǎn)hls視頻直播的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解決Golang json序列化字符串時(shí)多了\的情況
這篇文章主要介紹了解決Golang json序列化字符串時(shí)多了\的情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12
Golang實(shí)現(xiàn)自己的Redis數(shù)據(jù)庫(kù)內(nèi)存實(shí)例探究
這篇文章主要為大家介紹了Golang實(shí)現(xiàn)自己的Redis數(shù)據(jù)庫(kù)內(nèi)存實(shí)例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
go開(kāi)發(fā)過(guò)程中mapstructure使用示例詳解
mapstructure是一個(gè)Go語(yǔ)言庫(kù),用于將映射(如map或struct)解碼為結(jié)構(gòu)體,便于處理JSON、YAML等配置文件數(shù)據(jù),通過(guò)字段名或結(jié)構(gòu)體標(biāo)簽控制解碼,支持嵌套結(jié)構(gòu)體、靈活處理多種數(shù)據(jù)源,需要注意錯(cuò)誤處理,該庫(kù)適合于Go開(kāi)發(fā)中配置數(shù)據(jù)的讀取和轉(zhuǎn)換2024-10-10
一文詳解Go語(yǔ)言中對(duì)象池的正確打開(kāi)方式
對(duì)象池是一種設(shè)計(jì)模式,它維護(hù)一組已經(jīng)創(chuàng)建好的對(duì)象,當(dāng)需要使用對(duì)象時(shí),直接從對(duì)象池中獲取,使用完畢后再放回對(duì)象池,而不是頻繁地創(chuàng)建和銷(xiāo)毀對(duì)象,下面我們就來(lái)看看Go語(yǔ)言中對(duì)象池的具體使用吧2025-02-02

