在Ubuntu 12.04环境下安装ffmpeg并实时转发视频流

来源:互联网 发布:先知者软件 编辑:程序博客网 时间:2024/05/17 01:43

在Ubuntu 12.04环境下安装ffmpeg

在Ubuntu和Debian(树莓派)上分别编译安装了ffmpeg,并推送本地摄像头、局域网内RTSP流至ffserver,在局域网内观看http和rtsp流

  • 编译安装ffmpeg
  • 调用本地摄像头
  • 推送至ffserver

编译安装ffmpeg

ffmpeg实际上包括三个部分:

  • ffmpeg 编解码,包含了几乎所有常见编解码器
  • ffserver 流服务器,响应用户的请求,并将ffmpeg推送的流或本地文件调用ffmpeg按照配置编码后推送给用户
  • ffplay 一个简单的播放器

ffmpeg和ffserver的工作需要一个配置文件来协调。
此外,ffmpeg的核心还包括了自己实现的编解码器等:
- libavformat 用于各种音视频封装格式的生成和解析,包括获取解码所需信息以生成解码上下文结构和读取音视频帧等功能;

在Ubuntu或者树莓派上安装ffmpeg时不要直接从仓库安装,否则会出现无法推送流的问题。安装前需要先卸载系统以后的相关解码器,然后重新安装,安装脚本如下:

#!/bin/bash# Bash script to install latest version of ffmpeg and its dependencies on Ubuntu 12.04 or 14.04# Inspired from https://gist.github.com/faleev/3435377# Remove any existing packages:sudo apt-get -y remove ffmpeg x264 libav-tools libvpx-dev libx264-dev# Get the dependencies (Ubuntu Server or headless users):sudo apt-get updatesudo apt-get -y install build-essential checkinstall git libfaac-dev libgpac-dev \  libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev librtmp-dev libtheora-dev \    libvorbis-dev pkg-config texi2html yasm zlib1g-dev# Install x264sudo apt-get -y install libx264-devcdgit clone --depth 1 git://git.videolan.org/x264cd x264./configure --enable-staticmakesudo checkinstall --pkgname=x264 --pkgversion="3:$(./version.sh | \  awk -F'[" ]' '/POINT/{print $4"+git"$5}')" --backup=no --deldoc=yes \    --fstrans=no --default# Install AAC audio decodercdwget http://downloads.sourceforge.net/opencore-amr/fdk-aac-0.1.0.tar.gztar xzvf fdk-aac-0.1.0.tar.gzcd fdk-aac-0.1.0./configuremakesudo checkinstall --pkgname=fdk-aac --pkgversion="0.1.0" --backup=no \  --deldoc=yes --fstrans=no --default# Install VP8 video encoder and decoder.cdgit clone --depth 1 https://chromium.googlesource.com/webm/libvpx cd libvpx./configuremakesudo checkinstall --pkgname=libvpx --pkgversion="1:$(date +%Y%m%d%H%M)-git" --backup=no \  --deldoc=yes --fstrans=no --default# Add lavf support to x264# This allows x264 to accept just about any input that FFmpeg can handle and is useful if you want to use x264 directly. See a more detailed explanation of what this means.cd ~/x264make distclean./configure --enable-staticmakesudo checkinstall --pkgname=x264 --pkgversion="3:$(./version.sh | \  awk -F'[" ]' '/POINT/{print $4"+git"$5}')" --backup=no --deldoc=yes \  --fstrans=no --default# Installing FFmpegcdgit clone --depth 1 git://source.ffmpeg.org/ffmpegcd ffmpeg./configure --enable-gpl --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb \  --enable-libopencore-amrwb --enable-librtmp --enable-libtheora --enable-libvorbis \    --enable-libvpx --enable-libx264 --enable-nonfree --enable-version3 makesudo checkinstall --pkgname=ffmpeg --pkgversion="5:$(date +%Y%m%d%H%M)-git" --backup=no \  --deldoc=yes --fstrans=no --default  hash x264 ffmpeg ffplay ffprobe# Optional: install qt-faststart# This is a useful tool if you're showing your H.264 in MP4 videos on the web. It relocates some data in the video to allow playback to begin before the file is completely downloaded. Usage: qt-faststart input.mp4 output.mp4.cd ~/ffmpegmake tools/qt-faststartsudo checkinstall --pkgname=qt-faststart --pkgversion="$(date +%Y%m%d%H%M)-git" --backup=no \  --deldoc=yes --fstrans=no --default install -Dm755 tools/qt-faststart \  /usr/local/bin/qt-faststart

ubuntu下调用ffmpeg播放摄像头视频

捕获存入文件后播放

ffmpeg -f video4linux2 -s 320*300 -i /dev/video0 test.asfffplay test.asf

捕获后实时播放

ffplay -f video4linux2 -framerate 15 -video_size hd720 /dev/video0

使用罗技C270摄像头,在ubuntu虚拟机上运行成功


ffserver提供流服务

ffserver配置脚本:

HTTPPort 8090RTSPPort 8099BindAddress 0.0.0.0MaxHTTPConnections 2000MaxClients 1000MaxBandwidth 2000CustomLog -NoDaemon#媒体流文件设置<Feed feed1.ffm>File /tmp/feed1.ffmFileMaxSize 20MLaunch ffmpegACL allow 127.0.0.1ACL allow localhostACL allow 192.168.0.0 192.168.255.255</Feed>#压缩为asf格式<Stream test.asf>Feed feed1.ffmFormat asfVideoFrameRate 15VideoSize 352x240VideoBitRate 256VideoBufferSize 40VideoGopSize 30NoAudioStartSendOnKey</Stream>#rm格式视频#<Stream test.rm>#Feed feed1.ffm#Format rm#AudioBitRate 32#VideoBitRate 128#VideoFrameRate 25#VideoGopSize 25#NoAudio#</Stream><Stream file.asf>File /home/lynn/Desktop/test.asfNoAudio#Metadata author "Me"#Metadata copyright "Super MegaCorp"#Metadata title "Test stream from disk"#Metadata comment "Test comment"</Stream>#可以查看ffserver的启动状态<Stream stat.html>Format statusACL allow localhostACL allow 127.0.0.1ACL allow 192.168.0.0 192.168.255.255</Stream>#不起作用<Redirect index.html>URL http://www.ffmpeg.org/</Redirect>

此脚本在8090端口开了一个http服务器,并提供/stat.html, /test.asf, /file.asf三个api,可以获取运行状态,视频流
执行命令如下:

ffserver -f ffserver.confffmpeg -i rtsp://admin:admin@192.168.1.170/12 http://localhost:8090/feed1.ffm或ffmpeg  -f video4linux2 -i /dev/video0  http://localhost:8090/feed1.ffm

此时就可以在其他客户端观看实时视频

1 0
原创粉丝点击