Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of using ffmpeg to hls to do simple Video Live in go backend

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how to convert go backend to hls to do simple video live streaming with ffmpeg". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Preface

For now, let's test the ffmpeg-to-hls playback mode to see the latency and compatibility. We will mainly test the playback using Google browser in Windows, Linux and macOS. The back end combines our previous cgo part to build a simple http server, which is then provided to the front end to call.

2. Wsl install ffmpeg and convert rtsp to hlssudo apt-get install ffmpeg

Possible error:

"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]"

For the solution, you can choose to compile and install the direct source code:

Wget https://ffmpeg.org/releases/ffmpeg-4.1.tar.bz2tar-xjvf ffmpeg-4.1.tar.bz2cd ffmpeg-4.1sudo apt-get install yasm./configuremake & & sudo make installffmpeg-version

Ffmpeg converts rtsp to 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. Front and back end sample code 3.1 back end go code

We use go to create a simple http service, and then use ffmpg to transform the hls to the front end.

When authentication is required, the rtsp address can be preceded by a user name and password, such as rtsp://username:password@xxx. The user name and password are separated by:, and the original address is separated by @.

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 frontend code frontend broadcast Play video in m3u8 format. Video-js. Vjs-tech {position: relative! important } / / videojs simply use var myVideo = videojs ('myVideo', {bigPlayButton: true, textTrackDisplay: false, posterImage: false, errorDisplay: false }) myVideo.play () var changeVideo = function (vdoSrc) {if (/ / .m3u8 $/ .test (vdoSrc)) {/ / determine whether the video source is in m3u8 format myVideo.src ({src: vdoSrc) Type: 'application/x-mpegURL' / / the value of the new type needs to be given when readding the video feed})} else {myVideo.src (vdoSrc)} myVideo.load () MyVideo.play ();} / var src = ". / test.m3u8"; / / document.querySelector ('.qiefang'). AddEventListener ('click', function () {/ / changeVideo (src); / /}) 4. Results and evaluation

After running the backend code, you can visit localhost:9000 to view the video. After testing, the delay is still relatively high (my test is about 5s-8s). If you want to add ptz control, it may be weird to have no real-time feeling. It is only suitable for simple webcast and other things, and doesn't care much about a certain delay.

This is the end of the content of "how go backend uses ffmpeg to hls to do simple video live streaming". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report