go后端利用ffmpeg转hls做简单视频直播的方法(ffmpeg,go,hls,开发技术)

时间:2024-05-09 09:46:18 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

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控制的话没有实时感恐怕比较怪异,只适合简单的网络直播之类的,不太在乎一定的延迟。

go后端利用ffmpeg转hls做简单视频直播的方法

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:go后端利用ffmpeg转hls做简单视频直播的方法的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:Spring事务的七种传播行为是什么下一篇:

19 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18