go后端利用ffmpeg转hls做简单视频直播的方法
导读:本文共2501字符,通常情况下阅读需要8分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: 1. 前言目前我们先测试一下ffmpeg转hls播放的方式,看下延迟情况及兼容性情况,主要测试Windows、Linux和macOS中使用谷歌浏览器播放的情况。后端结合我们之前的cgo部分,建立一个简单的http服务器,然后提供给前端调用。2. wsl安装ffmpeg并转换rtsp为hlssudoapt-getinstallffmpeg可能报错:“E: Fail... ...
目录
(为您整理了一些要点),点击可以直达。1. 前言
目前我们先测试一下ffmpeg转hls播放的方式,看下延迟情况及兼容性情况,主要测试Windows、Linux和macOS中使用谷歌浏览器播放的情况。后端结合我们之前的cgo部分,建立一个简单的http服务器,然后提供给前端调用。
2. wsl安装ffmpeg并转换rtsp为hls
sudoapt-getinstallffmpeg
可能报错:
“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]”
解决办法,可以选择直接源码编译安装:
wgethttps://ffmpeg.org/releases/ffmpeg-4.1.tar.bz2tar-xjvfffmpeg-4.1.tar.bz2cdffmpeg-4.1sudoapt-getinstallyasm./configuremake&&sudomakeinstallffmpeg-version
ffmpeg转换rtsp为hls:
ffmpeg-i"rtsp://username:password@40.40.40.101/media/video1"-ccopy-fhls-hls_time2.0-hls_list_size0-hls_wrap15"./test.m3u8"
3. 前后端示例代码
3.1 后端go代码
我们使用go创建简单的http服务,然后利用ffmpg转换hls提供给前端。
需要鉴权时rtsp地址前加上用户名密码时即可,比如rtsp://username:password@xxx,用户名和密码之间用:隔开,和原本的地址用@隔开。
main.go:
import("fmt""net/http""os/exec""bytes""io/ioutil")funcIndex(whttp.ResponseWriter,r*http.Request){content,_:=ioutil.ReadFile("./index.html")w.Write(content)}funcmain(){http.HandleFunc("/index",Index)http.Handle("/",http.FileServer(http.Dir(".")))gofunc(){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")varoutbytes.Buffervarstderrbytes.Buffercmd.Stdout=&outcmd.Stderr=&stderrerr:=cmd.Run()iferr!=nil{fmt.Println(fmt.Sprint(err)+":"+stderr.String())return}fmt.Println("Result:"+out.String())}
3.2 前端代码
<!DOCTYPEhtml><htmllang="zh-CN"><head><metacharset="UTF-8"><title>前端播放m3u8格式视频</title><linkhref="https://vjs.zencdn.net/7.4.1/video-js.css"rel="externalnofollow"rel="stylesheet"><scriptsrc='https://vjs.zencdn.net/7.4.1/video.js'></script><scriptsrc="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><videoid="myVideo"class="video-jsvjs-default-skinvjs-big-play-centered"controlspreload="auto"data-setup='{}'style='width:100%;height:auto'><sourceid="source"src="http://localhost:9000/test.m3u8"type="application/x-mpegURL"></source></video></div><!--<divclass="qiehuan">切换视频</div>--></body><script>//videojs简单使用varmyVideo=videojs('myVideo',{bigPlayButton:true,textTrackDisplay:false,posterImage:false,errorDisplay:false,})myVideo.play()varchangeVideo=function(vdoSrc){if(/\.m3u8$/.test(vdoSrc)){//判断视频源是否是m3u8的格式myVideo.src({src:vdoSrc,type:'application/x-mpegURL'//在重新添加视频源的时候需要给新的type的值})}else{myVideo.src(vdoSrc)}myVideo.load();myVideo.play();}//varsrc="./test.m3u8";//document.querySelector('.qiehuan').addEventListener('click',function(){//changeVideo(src);//})</script>
4. 结果及评估
运行后端代码后访问localhost:9000即可查看视频,经测试延迟还是比较高的(我测试大致在5s-8s),如果要加上ptz控制的话没有实时感恐怕比较怪异,只适合简单的网络直播之类的,不太在乎一定的延迟。
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
go后端利用ffmpeg转hls做简单视频直播的方法的详细内容,希望对您有所帮助,信息来源于网络。