Go語言利用ffmpeg轉(zhuǎn)hls實現(xiàn)簡單視頻直播
1. 前言
上一次我們找到一些開源方案,目前我們先測試一下ffmpeg轉(zhuǎn)hls播放的方式,看下延遲情況及兼容性情況,主要測試Windows、Linux和macOS中使用谷歌瀏覽器播放的情況。
后端結(jié)合我們之前的cgo部分,建立一個簡單的http服務(wù)器,然后提供給前端調(diào)用。
2. wsl安裝ffmpeg并轉(zhuǎn)換rtsp為hls
sudo apt-get install ffmpeg
可能報錯:
“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)建簡單的http服務(wù),然后利用ffmpg轉(zhuǎn)換hls提供給前端。
需要鑒權(quán)時rtsp地址前加上用戶名密碼時即可,比如rtsp://username:password@xxx,用戶名和密碼之間用:隔開,和原本的地址用@隔開。
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 用于在電腦端播放 如果只需手機播放可以不引入 --> </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 簡單使用 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' //在重新添加視頻源的時候需要給新的type的值 }) } else { myVideo.src(vdoSrc) } myVideo.load(); myVideo.play(); } // var src = "./test.m3u8"; // document.querySelector('.qiehuan').addEventListener('click', function () { // changeVideo(src); // }) </script>
4. 結(jié)果及評估
運行后端代碼后訪問localhost:9000即可查看視頻,經(jīng)測試延遲還是比較高的(我測試大致在5s-8s),如果要加上ptz控制的話沒有實時感恐怕比較怪異,只適合簡單的網(wǎng)絡(luò)直播之類的,不太在乎一定的延遲。
以上就是go后端利用ffmpeg轉(zhuǎn)hls做簡單視頻直播的詳細內(nèi)容,更多關(guān)于Go ffmpeg轉(zhuǎn)hls視頻直播的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Golang實現(xiàn)自己的Redis數(shù)據(jù)庫內(nèi)存實例探究
這篇文章主要為大家介紹了Golang實現(xiàn)自己的Redis數(shù)據(jù)庫內(nèi)存實例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01