使用ffmpeg进行网络直播

一、采集:使用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

七、没有互动没有弹幕没有打赏没有美颜,就是那么任性

八、关于循环录制推流的代码还在进一步测试中

时间: 2024-08-04 13:33:55

使用ffmpeg进行网络直播的相关文章

golang ffmpeg 做网络直播

最近在公司做在线视频转码的工作,研究了下ffmpeg 最后直接研究了下网络直播,我是在我自己的mac 上面测试的,效果,还可以,先看看效果图吧 ffmpeg 我是通过brew安装 的,这步就略了 VLC这个播放器怎么安装的也略了 我先是在github上面找了一个开源的直播流工具 https://github.com/gwuhaolin/livego 然后把它run 起来,最后看几个直播流参数吧: 桌面成功: ffmpeg -f avfoundation -pixel_format uyvy422

使用FFmpeg转录网络直播流

爱奇艺万能播放器的最新版本增加了一个播放网络流的功能.不过,入口藏在播放器区域的右键菜单里,不太好找: 找来一个直播流URL,比如东森新闻 http://60.199.188.151/HLS/WG_ETTV-N/index.m3u8,试了一下,还不赖呢! 有时候,看到精彩的直播内容,想把某些片段保存到本地.无奈播放器不提供这样的功能.那么,开个小窗给FFmpeg吧,它可以搞定! 命令行如下: ffmpeg -i http://60.199.188.151/HLS/WG_ETTV-N/index.

使用ffmpeg搭建HLS直播系统

[时间:2018-04] [状态:Open] [关键词:流媒体,stream,HLS, ffmpeg,live,直播,点播, nginx, ssegment] 0 引言 本文作为HLS综述的后续文章. 主要目的是使用ffmpeg搭建一个简单的HLS点播及直播系统.使用nginx作为HTTP服务器. HLS不管点播还是直播,都是基于HTTP的文件分发系统,所以本文的基本思路是: 使用nginx搭建HTTP服务器 使用ffmpeg实现ts文件的分片,并生成m3u8 ffmpeg使用本地文件模拟HLS

ffmpeg部署现场直播

#所有需要安装的包都在contribs目录里. faac 1. #cd /usr/local/src/contribs 2. [[email protected] contribs]# tar zxvffaac-1.28.tar.gz 3. [[email protected] contribs]# cd faac-1.28 4. [[email protected] faac-1.28]# ./configure--prefix=/usr/local/ --enable-shared 5.[[

互联网影音Steam流式传输-网络直播点播

什么是stream流式传输 流式传输定义很广泛,现在主要指通过网络传送流媒体(如视频.音频)的技术总称.其特定含义为通过Internet 将影视节目传送到PC机,移动端Pad,安卓手机,苹果手机及网络机顶盒(OTT-TV或IPTV的具体应用). 我们也常见一个词"串流",也就是流式传输的一种形象说法.就是指一连串的影像资料压缩后,经过网络分析分段传送资料,在网络上即时传输影音以供观赏的一种技术和过程:串流传输可传送现场live影音或预存与服务器上的影片,当观看者在收看这些影音档时,影音

Android仿网络直播弹幕功能的实现

现在网络直播越来越火,网络主播也逐渐成为一种新兴职业,对于网络直播,弹幕功能是必须要有的,如下图: 首先来分析一下,这个弹幕功能是怎么实现的,首先在最下面肯定是一个游戏界面View,然后游戏界面上有弹幕View,弹幕的View必须要做成完全透明的,这样即使覆盖在游戏界面的上方也不会影响到游戏的正常观看,只有当有人发弹幕消息时,再将消息绘制到弹幕的View上面就可以了,下方肯定还有有操作界面View,可以让用户来发弹幕和送礼物的功能,原理示意图如下所示: 参照原理图,下面一步一步来实现这个功能.

岂能止步于网红 网络直播探索内容创业的正确姿势有多难?

如果要评今年最火爆的社会现象,网络直播应该算作一个.花椒直播.斗鱼直播.虎牙直播.一直播等众多直播平台迅速走进网友的生活,网红.网络主播等成为热词.但是网络直播在制造热门话题吸引眼球之时也面临着诸多问题,如内容有待创新.行业急需规范等,要想从激烈的竞争中脱颖而出走向更远,网络直播平台已经不能只靠网红了. 文/张书乐 刊载于<中国文化报>2016年11月16日网络文化版,刊载时有删节 刚从湖南某高校毕业,就奔赴国外独自旅拍的王欢最近开始尝试直播创业每天用一个小时的时间,把自己的"世界真

转:使用Live555类库实现的网络直播系统

Live555主要有四个类库: libUsageEnvironment.lib:libliveMedia.lib:libgroupsock.lib:libBasicUsageEnvironment.lib 将这四个类库以及相关的头文件导入VC++2010之后,可以轻松实现网络直播系统. 在这里直接贴上完整代码,粘贴到VC里面就可以运行. 注:程序运行后,使用播放器软件(VLC Media Player,FFplay等),打开URL:rtp://239.255.42.42:1234,即可收看直播的

洋铭 NVS-25 网络编码器推送RTMP直播流至流媒体系统进行网络直播

"三网融合"已成趋势. 网络电视直播在广电业大力鼓起,各广电媒体.新闻媒体都逐渐开始树立自个的网络电视直播体系.那么如何搭建网络电视台呢? 今天给大家介绍通过洋铭 NVS-25 网络编码器采集电视信号推送标准的RTMP流到流媒体服务器直播系统进行网络分发. 1.  确认电视设备输出接口 ü  HDMI或SDI输出 ü  网络直播流输出 2.  确认洋铭 NVS-25 网络编码器输出形式 NVS-25 为 Datavideo 设计的小尺寸网络直播编码器 , Plug&Play产品