2023-03-05:ffmpeg推送本地视频至lal流媒体服务器(以RTMP为例),请用go语言编写。

答案2023-03-05:

使用 github.com/moonfdd/ffmpeg-go 库。

先启动lal流媒体服务器软件,然后再执行命令:

go run ./examples/leixiaohua1020/simplest_ffmpeg_streamer/main.go

参考了雷霄骅的最简单的基于FFmpeg的推流器(推送RTMP),代码用golang编写。代码如下:

// https://github.com/leixiaohua1020/simplest_ffmpeg_streamer/blob/master/simplest_ffmpeg_streamer/simplest_ffmpeg_streamer.cpp
package mainimport ("fmt""os""os/exec""time""github.com/moonfdd/ffmpeg-go/ffcommon""github.com/moonfdd/ffmpeg-go/libavcodec""github.com/moonfdd/ffmpeg-go/libavformat""github.com/moonfdd/ffmpeg-go/libavutil"
)func main0() (ret ffcommon.FInt) {var ofmt *libavformat.AVOutputFormat//Input AVFormatContext and Output AVFormatContextvar ifmt_ctx, ofmt_ctx *libavformat.AVFormatContextvar pkt libavcodec.AVPacketvar in_filename, out_filename stringvar i ffcommon.FIntvar videoindex ffcommon.FInt = -1var frame_index ffcommon.FInt = 0var start_time ffcommon.FInt64T = 0var err error//in_filename  = "cuc_ieschool.mov";//in_filename  = "cuc_ieschool.mkv";//in_filename  = "cuc_ieschool.ts";//in_filename  = "cuc_ieschool.mp4";//in_filename  = "cuc_ieschool.h264";in_filename = "./out/cuc_ieschool.flv" //输入URL(Input file URL)//in_filename  = "shanghai03_p.h264";_, err = os.Stat(in_filename)if err != nil {if os.IsNotExist(err) {fmt.Println("create flv file")exec.Command("./lib/ffmpeg", "-i", "./resources/big_buck_bunny.mp4", "-vcodec", "copy", "-acodec", "copy", in_filename).Output()}}out_filename = "rtmp://localhost/publishlive/livestream" //输出 URL(Output URL)[RTMP]//out_filename = "rtp://233.233.233.233:6666";//输出 URL(Output URL)[UDP]libavformat.AvRegisterAll()//Networklibavformat.AvformatNetworkInit()//Inputret = libavformat.AvformatOpenInput(&ifmt_ctx, in_filename, nil, nil)if ret < 0 {fmt.Printf("Could not open input file.")goto end}ret = ifmt_ctx.AvformatFindStreamInfo(nil)if ret < 0 {fmt.Printf("Failed to retrieve input stream information")goto end}for i = 0; i < int32(ifmt_ctx.NbStreams); i++ {if ifmt_ctx.GetStream(uint32(i)).Codec.CodecType == libavutil.AVMEDIA_TYPE_VIDEO {videoindex = ibreak}}ifmt_ctx.AvDumpFormat(0, in_filename, 0)//Outputlibavformat.AvformatAllocOutputContext2(&ofmt_ctx, nil, "flv", out_filename) //RTMP//avformat_alloc_output_context2(&ofmt_ctx, NULL, "mpegts", out_filename);//UDPif ofmt_ctx == nil {fmt.Printf("Could not create output context\n")ret = libavutil.AVERROR_UNKNOWNgoto end}ofmt = ofmt_ctx.Oformatfor i = 0; i < int32(ifmt_ctx.NbStreams); i++ {//Create output AVStream according to input AVStreamin_stream := ifmt_ctx.GetStream(uint32(i))out_stream := ofmt_ctx.AvformatNewStream(in_stream.Codec.Codec)if out_stream == nil {fmt.Printf("Failed allocating output stream\n")ret = libavutil.AVERROR_UNKNOWNgoto end}//Copy the settings of AVCodecContextret = libavcodec.AvcodecCopyContext(out_stream.Codec, in_stream.Codec)if ret < 0 {fmt.Printf("Failed to copy context from input to output stream codec context\n")goto end}out_stream.Codec.CodecTag = 0if ofmt_ctx.Oformat.Flags&libavformat.AVFMT_GLOBALHEADER != 0 {out_stream.Codec.Flags |= libavcodec.AV_CODEC_FLAG_GLOBAL_HEADER}}//Dump Format------------------ofmt_ctx.AvDumpFormat(0, out_filename, 1)//Open output URLif ofmt.Flags&libavformat.AVFMT_NOFILE == 0 {ret = libavformat.AvioOpen(&ofmt_ctx.Pb, out_filename, libavformat.AVIO_FLAG_WRITE)if ret < 0 {fmt.Printf("Could not open output URL '%s'", out_filename)goto end}}//Write file headerret = ofmt_ctx.AvformatWriteHeader(nil)if ret < 0 {fmt.Printf("Error occurred when opening output URL\n")goto end}start_time = libavutil.AvGettime()for {var in_stream, out_stream *libavformat.AVStream//Get an AVPacketret = ifmt_ctx.AvReadFrame(&pkt)if ret < 0 {break}//FIX:No PTS (Example: Raw H.264)//Simple Write PTSif pkt.Pts == libavutil.AV_NOPTS_VALUE {//Write PTStime_base1 := ifmt_ctx.GetStream(uint32(videoindex)).TimeBase//Duration between 2 frames (us)calc_duration := int64(libavutil.AV_TIME_BASE / libavutil.AvQ2d(ifmt_ctx.GetStream(uint32(videoindex)).RFrameRate))//Parameterspkt.Pts = int64(float64(frame_index) * float64(calc_duration) / (libavutil.AvQ2d(time_base1) * libavutil.AV_TIME_BASE))pkt.Dts = pkt.Ptspkt.Duration = int64(float64(calc_duration) / (libavutil.AvQ2d(time_base1) * libavutil.AV_TIME_BASE))}//Important:Delayif pkt.StreamIndex == uint32(videoindex) {time_base := ifmt_ctx.GetStream(uint32(videoindex)).TimeBasetime_base_q := libavutil.AVRational{1, libavutil.AV_TIME_BASE}pts_time := libavutil.AvRescaleQ(pkt.Dts, time_base, time_base_q)now_time := libavutil.AvGettime() - start_timeif pts_time > now_time {libavutil.AvUsleep(uint32(pts_time - now_time))}}in_stream = ifmt_ctx.GetStream(pkt.StreamIndex)out_stream = ofmt_ctx.GetStream(pkt.StreamIndex)/* copy packet *///Convert PTS/DTSpkt.Pts = libavutil.AvRescaleQRnd(pkt.Pts, in_stream.TimeBase, out_stream.TimeBase, libavutil.AV_ROUND_NEAR_INF|libavutil.AV_ROUND_PASS_MINMAX)pkt.Dts = libavutil.AvRescaleQRnd(pkt.Dts, in_stream.TimeBase, out_stream.TimeBase, libavutil.AV_ROUND_NEAR_INF|libavutil.AV_ROUND_PASS_MINMAX)pkt.Duration = libavutil.AvRescaleQ(pkt.Duration, in_stream.TimeBase, out_stream.TimeBase)pkt.Pos = -1//Print to Screenif pkt.StreamIndex == uint32(videoindex) {fmt.Printf("Send %8d video frames to output URL\n", frame_index)frame_index++}//ret = av_write_frame(ofmt_ctx, &pkt);ret = ofmt_ctx.AvInterleavedWriteFrame(&pkt)if ret < 0 {fmt.Printf("Error muxing packet\n")break}pkt.AvFreePacket()}//Write file trailerofmt_ctx.AvWriteTrailer()
end:libavformat.AvformatCloseInput(&ifmt_ctx)/* close output */if ofmt_ctx != nil && ofmt.Flags&libavformat.AVFMT_NOFILE == 0 {ofmt_ctx.Pb.AvioClose()}ofmt_ctx.AvformatFreeContext()if ret < 0 && ret != libavutil.AVERROR_EOF {fmt.Printf("Error occurred.\n")return -1}return 0
}func main() {os.Setenv("Path", os.Getenv("Path")+";./lib")ffcommon.SetAvutilPath("./lib/avutil-56.dll")ffcommon.SetAvcodecPath("./lib/avcodec-58.dll")ffcommon.SetAvdevicePath("./lib/avdevice-58.dll")ffcommon.SetAvfilterPath("./lib/avfilter-56.dll")ffcommon.SetAvformatPath("./lib/avformat-58.dll")ffcommon.SetAvpostprocPath("./lib/postproc-55.dll")ffcommon.SetAvswresamplePath("./lib/swresample-3.dll")ffcommon.SetAvswscalePath("./lib/swscale-5.dll")genDir := "./out"_, err := os.Stat(genDir)if err != nil {if os.IsNotExist(err) {os.Mkdir(genDir, 0777) //  Everyone can read write and execute}}go func() {time.Sleep(1000)exec.Command("./lib/ffplay.exe", "rtmp://localhost/publishlive/livestream").Output()if err != nil {fmt.Println("play err = ", err)}}()main0()
}

2023-03-05:ffmpeg推送本地视频至lal流媒体服务器(以RTMP为例),请用go语言编写。相关推荐

  1. 视频网站ts流媒体服务器,推送本机视频流到流媒体服务器

    前言: 上一章简单的介绍了javacv并且演示了如何获取本机摄像头:http://blog.csdn.net/eguid_1/article/details/51659578 本章将在上一章的基础上, ...

  2. AI养猪,国标GB28181协议视频平台EasyGBS流媒体服务器携手RTMP流媒体服务器EasyDSS协同打造智慧养殖生态圈

    目前,世界上许多发达国家拥有着高度发达的养殖业.这些发达国家的养殖业,均有着高技术.低人工,高产能,低消耗等特点.这其中,高技术的科技力量已经成为现代化养殖场一个关键性的指标. 而目前养殖业的困境不是 ...

  3. ffmpeg推送rtsp流或者视频文件到rtsp服务器

    1.推送rtsp视频流 ffmpeg -i rtsp://admin:admin1234@192.168.1.64:554/h264/ch1/sub/av_stream -codec copy -f ...

  4. FFmpeg 推送摄像头 rtsp 流

    FFmpeg 推送摄像头 rtsp 流 Windows 环境下使用 FFmpeg 推送本地 USB 摄像头为 rtsp 流,并使用 vlc 播放. 本文主要使用环境是 Windows 下的 FFmpe ...

  5. WebRTC 如何推送本地视频流

    一.需求 使用 webrtc 协议做直播,常见的音视频源是摄像头和麦克风,高级一点的就是桌面分享.虽然使用桌面分享可以实现推送本地流(原理就是对屏幕录制),但依赖本地播放器,并且观众可以看到主播的任何 ...

  6. ffmpeg推拉流 视频合成

    ffmpeg推拉流 视频合成 多路合并- 推拉流指令 ffmpeg -re -rtsp_transport tcp -i "rtsp://admin:**********/h264/ch1/ ...

  7. FFmpeg推送命令

    ffmpeg推流命令 使用命令推送成rtp流 使用rtp发送 ffmpeg -i rtsp://admin:1qaz2wsx@192.168.10.250:554/Streaming/Channels ...

  8. git 推送本地分支到远程分支 git push origin

    ** 情形:**在本地分支local_branch修改了代码,之后要提交到远程分支remote_branch上,使用命令git push origin remote_branch报错. ** 报错:* ...

  9. Git for Windows之推送本地版本库到远程仓库

    Git for Windows之基础环境搭建与基础操作中介绍了Git基本环境的构建与基本的操作.生成了一个本地git版本库,本文将介绍如何将这个版本库推送到远程仓库(码云,github也可以). 1. ...

最新文章

  1. nginx安装-添加MP4播放模块
  2. 今日运势 酷q_一言及每日运势API开源
  3. Linux下远程桌面连接windows
  4. 聊聊 computed 影响性能的场景
  5. jvm MinorGC和查看GC日志
  6. Hyper-V 2012 R2 故障转移群集之建立域控(AD DS)与加入域
  7. ftp 服务器创建访问连接抱错_如何用固定IP连接FTP服务器?
  8. P. Laguna/Evaluation of an Automatic Threshold Based Detector of Waveform Limits in Holter ECG
  9. target sum java_LeetCode 494. Target Sum
  10. 单片机软件反破解 Hex反破解 破解后的hex不能量产
  11. 【软件工程】数据库设计说明书
  12. git fatal: unable to access *** Timed out
  13. php奖学金系统,java/php/net/pythont奖助学金管理系统设计
  14. 分形植物的c语言源代码,C语言源代码实例.rar
  15. 游戏版署过审注意事项
  16. 修改idea的头部文档注释信息
  17. 余生很短,请珍惜 珍护 珍重
  18. 跨境电商特点有哪些?
  19. Linux中opengl库叫什么名字,Linux下的OpenGL——Mesa和GLX简介
  20. 来不及细说,毕业三天靠Python兼职赚了两千

热门文章

  1. C4D导入模型不显示
  2. MySQL数据库行转列,列转行
  3. mysql重置数据库主键_重置Mysql主键的方法
  4. windows无法访问 计算机打印机,windows无法打开添加打印机的解决方法
  5. windows无法打开添加打印机_win7系统提示无法打开添加打印机怎么办 无法打开添加打印机解决方法【介绍】...
  6. linux重启时无法关机,linux系统无法关机/重启
  7. excel添加列下拉框票价_如何在excel中设置下拉菜单
  8. 咖说 | 富达:企业财资为何该考虑数字货币
  9. iOS NSString字符串截取方法
  10. Netty 单机百万连接测试