欢迎访问我的网站,希望内容对您有用,感兴趣的可以加入免费知识星球。

ffmpeg+ffserver实现基于http的视频点播

流媒体 迷途小书童 4年前 (2019-12-25) 8161次浏览 0个评论

环境

  • ubuntu 18.04 64bit
  • ffmpeg

视频看这里

简介

ffmpeg是一个开源的音视频处理的开发套件,它包括几个非常实用的命令行工具,ffmpegffprobeffserverffplay。本文实现的是ffmpeg + ffserver 来搭建基于 http 的视频点播系统。

系统架构

下图是一个简单的系统架构。图中的 cam.ffm,可以理解为是一个缓存文件,ffmpeg 负责从本地或者网络中抓取数据,然后发送给 ffserver,如果此时没有客户端连接,那么数据就会被写入到 cam.ffm 中。

arch

安装环境

安装 ffmpeg 套件

sudo apt install ffmpeg

启动ffserver

首先修改文件 /etc/ffserver.conf,如果没有就自己创建

HTTPPort 8090
HTTPBindAddress 0.0.0.0
MaxClients 10
MaxBandWidth 50000
CustomLog -
#NoDaemon

<Feed cam.ffm>
   File /tmp/cam.ffm
   FileMaxSize 1G
   ACL allow 127.0.0.1
   ACL allow localhost
</Feed>
<Stream cam.mjpeg>
   Feed cam.ffm
   Format mpjpeg
   VideoFrameRate 20
   VideoBitRate 10240
   VideoBufferSize 20480
   VideoSize 1920x1080
   VideoQMin 3
   VideoQMax 31
   NoAudio
   Strict -1
</Stream>
<Stream stat.html>
   Format status
   # Only allow local people to get the status
   ACL allow localhost
   ACL allow 192.168.1.0 192.168.1.255
</Stream>
<Redirect index.html>
   URL http://www.ffmpeg.org/
</Redirect>

然后在终端中执行如下命令来启动服务

ffserver -d -f /etc/ffserver.conf

start_ffserver

启动ffmpeg

准备个本地视频文件作为 ffmpeg 的输入,网络视频流也可以。在 ffmpeg 启动之前需确保 ffserver 已经启动

ffmpeg -i Videos/20170307_CCTV9_Special.Edition-Aerial.China.S01E05-jiangxi.ts http://localhost:8090/cam.ffm

start_ffserver

查看状态

在浏览器中访问 http://localhost:8090/stat.html,页面上会显示出当前系统中可以访问的链接,如下图所示,http://localhost:8090/index.html,这个链接我们在 ffserver.conf 中做了重定向,会直接跳转去访问 http://www.ffmpeg.org/,链接 http://localhost:8090/cam.mjpeg 是我们想要的

start_ffserver

在浏览器中播放mjpeg

浏览器打开 http://localhost:8090/cam.mjpeg

start_ffserver

输出格式为flv

修改 ffserver.conf

HTTPPort 8090
HTTPBindAddress 0.0.0.0
MaxClients 10
MaxBandWidth 50000
CustomLog -
#NoDaemon

<Feed cam.ffm>
   File /tmp/cam.ffm
   FileMaxSize 1G
   ACL allow 127.0.0.1
   ACL allow localhost
</Feed>
<Stream test>
   Feed cam.ffm
   Format flv 
   VideoFrameRate 20
   VideoBitRate 10240
   VideoBufferSize 20480
   VideoSize 1920x1080
   VideoQMin 3
   VideoQMax 31
   NoAudio
   Strict -1
</Stream>
<Stream stat.html>
   Format status
   # Only allow local people to get the status
   ACL allow localhost
   ACL allow 192.168.1.0 192.168.1.255
</Stream>
<Redirect index.html>
   URL http://www.ffmpeg.org/
</Redirect>

重新启动 ffmpeg

ffmpeg -i test.ts http://localhost:8090/cam.ffm

ffplay播放flv

推流成功后,就可以使用 ffplay 进行播放了,urlhttp://localhost:8090/test

start_ffserver

rtsp作为ffmpeg的输入源

ffmpeg 推流时,将本地视频文件换成 rtsp 视频流

ffmpeg -rtsp_transport tcp -i
rtsp://user:password@192.168.1.100:554/Streaming/Channels/1
http://localhost:8090/cam.ffm

参看资料

  1. https://trac.ffmpeg.org/wiki/ffserver
  2. https://www.ffmpeg.org/ffserver.html
喜欢 (1)

您必须 登录 才能发表评论!