Hi,大家好,我是一个爱冒泡的程序猿
在项目中发现EasyDarwin录屏有问题,让我们一起来看看吧
前言
EasyDarwin内部是使用FFmpeg进行分片,然后存储至本地,目前我将FFmpeg安装好,EasyDarwin搭建好,推流的时候,发现根本没存储至本地,存储目录生成了,但是.m3u8和.ts文件没有生成。
log.txt报错:Alternatively use the non experimental encoder ‘libfdk_aac’.
后找到原因,供大家参考
目录
- 前言
- 1、推流命令:
- 2、存储目录log.xml报错
- 4、根据提示,修改源码
- 5、源码示例
- 6、再次推流至流媒体,测验修改结果
- 7、查看下存储的录屏,yes!成功啦~
1、推流命令:
ffmpeg -re -i /data/test123.mp4 -vcodec libx264 -acodec copy -f rtsp -rtsp_transport tcp rtsp:/121.36.35.46/40851360cf284687974fec578c3dd5ae_摄像头
2、存储目录log.xml报错
没有存储到服务器本地,存储时日志记录报错:
ffmpeg version 2.8.15 Copyright (c) 2000-2018 the FFmpeg developers
built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-36)
configuration: --prefix=/usr --bindir=/usr/bin --datadir=/usr/share/ffmpeg --incdir=/usr/include/ffmpeg --libdir=/usr/lib64 --mandir=/usr/share/man --arch=x86_64 --optflags='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic' --extra-ldflags='-Wl,-z,relro ' --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvo-amrwbenc --enable-version3 --enable-bzlib --disable-crystalhd --enable-gnutls --enable-ladspa --enable-libass --enable-libcdio --enable-libdc1394 --enable-libfdk-aac --enable-nonfree --disable-indev=jack --enable-libfreetype --enable-libgsm --enable-libmp3lame --enable-openal --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libv4l2 --enable-libx264 --enable-libx265 --enable-libxvid --enable-x11grab --enable-avfilter --enable-avresample --enable-postproc --enable-pthreads --disable-static --enable-shared --enable-gpl --disable-debug --disable-stripping --shlibdir=/usr/lib64 --enable-runtime-cpudetect
libavutil 54. 31.100 / 54. 31.100
libavcodec 56. 60.100 / 56. 60.100
libavformat 56. 40.101 / 56. 40.101
libavdevice 56. 4.100 / 56. 4.100
libavfilter 5. 40.101 / 5. 40.101
libavresample 2. 1. 0 / 2. 1. 0
libswscale 3. 1.101 / 3. 1.101
libswresample 1. 2.101 / 1. 2.101
libpostproc 53. 3.100 / 53. 3.100
Input #0, rtsp, from 'rtsp://localhost:554/f5b60526bd3047caa62dceb939d14756_摄像头一':
Metadata:
title : No Name
Duration: N/A, start: -2.574331, bitrate: N/A
Stream #0:0: Video: h264 (High), yuv420p, 540x960, 30 fps, 30 tbr, 90k tbn, 60 tbc
Stream #0:1: Audio: aac (LC), 44100 Hz, mono, fltp
[aac @ 0x1c32560] The encoder 'aac' is experimental but experimental codecs are not enabled, add '-strict -2' if you want to use it.
[aac @ 0x1c32560] Alternatively use the non experimental encoder 'libfdk_aac'.
4、根据提示,修改源码
我们将EasyDarwin中 rtsp-server.go 中**Start()**方法进行修改
将原先:-c:a aac
改为:-c:a libfdk_aac
5、源码示例
func (server *Server) Start() (err error) {
logger := server.logger
addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf(":%d", server.TCPPort))
if err != nil {
return
}
listener, err := net.ListenTCP("tcp", addr)
if err != nil {
return
}
localRecord := utils.Conf().Section("rtsp").Key("save_stream_to_local").MustInt(0)
ffmpeg := utils.Conf().Section("rtsp").Key("ffmpeg_path").MustString("")
m3u8_dir_path := utils.Conf().Section("rtsp").Key("m3u8_dir_path").MustString("")
ts_duration_second := utils.Conf().Section("rtsp").Key("ts_duration_second").MustInt(6)
SaveStreamToLocal := false
if (len(ffmpeg) > 0) && localRecord > 0 && len(m3u8_dir_path) > 0 {
err := utils.EnsureDir(m3u8_dir_path)
if err != nil {
logger.Printf("Create m3u8_dir_path[%s] err:%v.", m3u8_dir_path, err)
} else {
SaveStreamToLocal = true
}
}
go func() { // save to local.
pusher2ffmpegMap := make(map[*Pusher]*exec.Cmd)
if SaveStreamToLocal {
logger.Printf("Prepare to save stream to local....")
defer logger.Printf("End save stream to local....")
}
var pusher *Pusher
addChnOk := true
removeChnOk := true
for addChnOk || removeChnOk {
select {
case pusher, addChnOk = <-server.addPusherCh:
if SaveStreamToLocal {
if addChnOk {
dir := path.Join(m3u8_dir_path, pusher.Path(), time.Now().Format("20060102"))
err := utils.EnsureDir(dir)
if err != nil {
logger.Printf("EnsureDir:[%s] err:%v.", dir, err)
continue
}
m3u8path := path.Join(dir, fmt.Sprintf("out.m3u8"))
port := pusher.Server().TCPPort
rtsp := fmt.Sprintf("rtsp://localhost:%d%s", port, pusher.Path())
paramStr := utils.Conf().Section("rtsp").Key(pusher.Path()).MustString("-c:v copy -c:a libfdk_aac")
params := []string{"-fflags", "genpts", "-rtsp_transport", "tcp", "-i", rtsp, "-hls_time", strconv.Itoa(ts_duration_second), "-hls_list_size", "0", m3u8path}
if paramStr != "default" {
paramsOfThisPath := strings.Split(paramStr, " ")
params = append(params[:6], append(paramsOfThisPath, params[6:]...)...)
}
// ffmpeg -i ~/Downloads/720p.mp4 -s 640x360 -g 15 -c:a aac -hls_time 5 -hls_list_size 0 record.m3u8
cmd := exec.Command(ffmpeg, params...)
f, err := os.OpenFile(path.Join(dir, fmt.Sprintf("log.txt")), os.O_RDWR|os.O_CREATE, 0755)
if err == nil {
cmd.Stdout = f
cmd.Stderr = f
}
err = cmd.Start()
if err != nil {
logger.Printf("Start ffmpeg err:%v", err)
}
pusher2ffmpegMap[pusher] = cmd
logger.Printf("add ffmpeg [%v] to pull stream from pusher[%v]", cmd, pusher)
} else {
logger.Printf("addPusherChan closed")
}
}
case pusher, removeChnOk = <-server.removePusherCh:
if SaveStreamToLocal {
if removeChnOk {
cmd := pusher2ffmpegMap[pusher]
proc := cmd.Process
if proc != nil {
logger.Printf("prepare to SIGTERM to process:%v", proc)
proc.Signal(syscall.SIGTERM)
proc.Wait()
// proc.Kill()
// no need to close attached log file.
// see "Wait releases any resources associated with the Cmd."
// if closer, ok := cmd.Stdout.(io.Closer); ok {
// closer.Close()
// logger.Printf("process:%v Stdout closed.", proc)
// }
logger.Printf("process:%v terminate.", proc)
}
delete(pusher2ffmpegMap, pusher)
logger.Printf("delete ffmpeg from pull stream from pusher[%v]", pusher)
} else {
for _, cmd := range pusher2ffmpegMap {
proc := cmd.Process
if proc != nil {
logger.Printf("prepare to SIGTERM to process:%v", proc)
proc.Signal(syscall.SIGTERM)
}
}
pusher2ffmpegMap = make(map[*Pusher]*exec.Cmd)
logger.Printf("removePusherChan closed")
}
}
}
}
}()
server.Stoped = false
server.TCPListener = listener
logger.Println("rtsp server start on", server.TCPPort)
networkBuffer := utils.Conf().Section("rtsp").Key("network_buffer").MustInt(1048576)
for !server.Stoped {
conn, err := server.TCPListener.Accept()
if err != nil {
logger.Println(err)
continue
}
if tcpConn, ok := conn.(*net.TCPConn); ok {
if err := tcpConn.SetReadBuffer(networkBuffer); err != nil {
logger.Printf("rtsp server conn set read buffer error, %v", err)
}
if err := tcpConn.SetWriteBuffer(networkBuffer); err != nil {
logger.Printf("rtsp server conn set write buffer error, %v", err)
}
}
session := NewSession(server, conn)
go session.Start()
}
return
}
6、再次推流至流媒体,测验修改结果
7、查看下存储的录屏,yes!成功啦~
开心,问题解决啦~
掰掰~ 奔向阿里的第17天 ╰(°▽°)╯