1. 程式人生 > >使用ffmpeg進行網絡直播

使用ffmpeg進行網絡直播

optional rem pre ntp 做了 refs sample left following

一、采集:使用python調用攝像頭采集,原設想是使用樹莓派攝像頭采集,但是經費緊張買不起,先用攝像頭湊合下,反正很簡單。 原理就是先錄一小段視頻,然後循環播放,用celery做任務控制,每5秒鐘錄一段很小的視頻,然後再循環錄制。控制錄制開始和停止的方法就是在redis鐘設置一個鍵,錄像程序運行的前提是這個鍵允許錄制,如果要求錄制停止就把這個鍵設置為停止。每5秒循環錄制。正式使用後用python調用ffmpeg的命令進行推流直播,錄制視頻的格式是avi格式,要記得定時刪除。錄視頻使用opencv的cv2庫。註意安裝最新版的opencv 二、安裝simple rtmp server:從github上下載simple rtmp server,使用rtmp播放https://github.com/ossrs/srs。 安裝方法http://blog.csdn.net/Henry_wk/article/details/50377881 安裝時記得apt-get的源要使用默認源 安裝好之後改配置文件,目錄在trunk/conf/rtmp.conf,配置文件如下
# the config for srs to delivery RTMP
# @see https://github.com/ossrs/srs/wiki/v1_CN_SampleRTMP
# @see full.conf for detail config.

listen 1935;
max_connections 1000;
daemon off;
srs_log_tank console;
vhost __defaultVhost__ {
transcode {
# whether the transcode enabled.
# if off, donot transcode.
# default: off.
enabled on;
# the ffmpeg
ffmpeg ./objs/ffmpeg/bin/ffmpeg;
# the transcode engine for matched stream.
# all matched stream will transcoded to the following stream.
# the transcode set name(ie. hd) is optional and not used.
engine example {
# whether the engine is enabled
# default: off.
enabled on;
# input format, can be:
# off, do not specifies the format, ffmpeg will guess it.
# flv, for flv or RTMP stream.
# other format, for example, mp4/aac whatever.
# default: flv
iformat avi;
# ffmpeg filters, follows the main input.
vfilter {
# the logo input file.
i ./doc/ffmpeg-logo.png;
# the ffmpeg complex filter.
# for filters, @see: http://ffmpeg.org/ffmpeg-filters.html
filter_complex ‘overlay=10:10‘;
}
# video encoder name. can be:
# libx264: use h.264(libx264) video encoder.
# copy: donot encoder the video stream, copy it.
# vn: disable video output.
vcodec libx264;
# video bitrate, in kbps
# @remark 0 to use source video bitrate.
# default: 0
vbitrate 1500;
# video framerate.
# @remark 0 to use source video fps.
# default: 0
vfps 25;
# video width, must be even numbers.
# @remark 0 to use source video width.
# default: 0
vwidth 768;
# video height, must be even numbers.
# @remark 0 to use source video height.
# default: 0
vheight 320;
# the max threads for ffmpeg to used.
# default: 1
vthreads 12;
# x264 profile, @see x264 -help, can be:
# high,main,baseline
vprofile main;
# x264 preset, @see x264 -help, can be:
# ultrafast,superfast,veryfast,faster,fast
# medium,slow,slower,veryslow,placebo
vpreset medium;
# other x264 or ffmpeg video params
vparams {
# ffmpeg options, @see: http://ffmpeg.org/ffmpeg.html
t 100;
# 264 params, @see: http://ffmpeg.org/ffmpeg-codecs.html#libx264
coder 1;
b_strategy 2;
bf 3;
refs 10;
}
# audio encoder name. can be:
# libfdk_aac: use aac(libfdk_aac) audio encoder.
# copy: donot encoder the audio stream, copy it.
# an: disable audio output.
acodec libfdk_aac;
# audio bitrate, in kbps. [16, 72] for libfdk_aac.
# @remark 0 to use source audio bitrate.
# default: 0
abitrate 70;
# audio sample rate. for flv/rtmp, it must be:
# 44100,22050,11025,5512
# @remark 0 to use source audio sample rate.
# default: 0
asample_rate 44100;
# audio channel, 1 for mono, 2 for stereo.
# @remark 0 to use source audio channels.
# default: 0
achannels 2;
# other ffmpeg audio params
aparams {
# audio params, @see: http://ffmpeg.org/ffmpeg-codecs.html#Audio-Encoders
# @remark SRS supported aac profile for HLS is: aac_low, aac_he, aac_he_v2
profile:a aac_low;
bsf:a aac_adtstoasc;
}
# output format, can be:
# off, do not specifies the format, ffmpeg will guess it.
# flv, for flv or RTMP stream.
# other format, for example, mp4/aac whatever.
# default: flv
oformat flv;
# output stream. variables:
# [vhost] the input stream vhost.
# [port] the intput stream port.
# [app] the input stream app.
# [stream] the input stream name.
# [engine] the tanscode engine name.
output rtmp://127.0.0.1:[port]/[app]?vhost=[vhost]/[stream]_[engine];
}
}
}
改好配置文件後啟動srs,命令./obj/srs -c conf/rtmp這樣就可以了 三、編碼、推流: 這個都使用工具完成了,是使用shell命令推流,命令為ffmpeg -re -i ./output.avi -vcodec libx264 -acodec copy -f flv -y rtmp://192.168.56.101/live/livestream; 是使用.246編碼 四、分發: 用nginx反向代理,在nginx配置文件中加入如下,位置是和http同級
stream{
upstream proxybackend{
server localhost:1935;
}
server {
listen 8888;
proxy_pass proxybackend;
}
}
五、解碼、播放:都用vlc media player來做了。http://mirror.os6.org/videolan/vlc/2.2.6/win32/vlc-2.2.6-win32.exe 六、播放地址是:rtmp://192.168.56.101/live/livestream 七、沒有互動沒有彈幕沒有打賞沒有美顏,就是那麽任性 八、關於循環錄制推流的代碼還在進一步測試中

使用ffmpeg進行網絡直播