rtsp转成flv实现http播放

#安装ffmpeg
安装依赖

yum install autoconf automake bzip2 bzip2-devel cmake freetype-devel gcc gcc-c++ git libtool make pkgconfig zlib-devel

安装ffmpeg插件
1、创建安装目录

  mkdir ~/ffmpeg_sources

2、安装NASM

cd ~/ffmpeg_sources
curl -O -L https://www.nasm.us/pub/nasm/releasebuilds/2.15.05/nasm-2.15.05.tar.bz2
tar xjvf nasm-2.15.05.tar.bz2
cd nasm-2.15.05
./autogen.sh
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin"
make
make install

3、安装YASM

cd ~/ffmpeg_sources
curl -O -L https://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
tar xzvf yasm-1.3.0.tar.gz
cd yasm-1.3.0
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin"
make
make install

4、安装libx264

cd ~/ffmpeg_sources
git clone --branch stable --depth 1 https://code.videolan.org/videolan/x264.git
cd x264
PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" ./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --enable-static
make
make install

5、安装libx265

cd ~/ffmpeg_sources
git clone --branch stable --depth 2 https://bitbucket.org/multicoreware/x265_git
cd ~/ffmpeg_sources/x265_git/build/linux
cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$HOME/ffmpeg_build" -DENABLE_SHARED:bool=off ../../source
make
make install

6、安装libfdk_aac

 cd ~/ffmpeg_sources
git clone --depth 1 https://github.com/mstorsjo/fdk-aac
cd fdk-aac
autoreconf -fiv
./configure --prefix="$HOME/ffmpeg_build" --disable-shared
make
make install

7、安装libmp3lame

cd ~/ffmpeg_sources
curl -O -L https://downloads.sourceforge.net/project/lame/lame/3.100/lame-3.100.tar.gz
tar xzvf lame-3.100.tar.gz
cd lame-3.100
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --disable-shared --enable-nasm
make
make install

8、安装libopus

cd ~/ffmpeg_sources
curl -O -L https://archive.mozilla.org/pub/opus/opus-1.3.1.tar.gz
tar xzvf opus-1.3.1.tar.gz
cd opus-1.3.1
./configure --prefix="$HOME/ffmpeg_build" --disable-shared
make
make install

9、安装ffmpeg

cd ~/ffmpeg_sources
curl -O -L https://ffmpeg.org/releases/ffmpeg-snapshot.tar.bz2
tar xjvf ffmpeg-snapshot.tar.bz2
cd ffmpeg
PATH="$HOME/bin:$PATH" PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" ./configure \
  --prefix="$HOME/ffmpeg_build" \
  --pkg-config-flags="--static" \
  --extra-cflags="-I$HOME/ffmpeg_build/include" \
  --extra-ldflags="-L$HOME/ffmpeg_build/lib" \
  --extra-libs=-lpthread \
  --extra-libs=-lm \
  --bindir="$HOME/bin" \
  --enable-gpl \
  --enable-libfdk_aac \
  --enable-libfreetype \
  --enable-libmp3lame \
  --enable-libopus \
  --enable-libvpx \
  --enable-libx264 \
  --enable-libx265 \
  --enable-nonfree
make
make install

以上完成安装ffmpeg,详细可参考https://blog.csdn.net/jxhaha/article/details/127428026
#安装nginx
1、安装相关依赖

 yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

2、下载nginx

cd /usr/local
mkdir nginx
cd nginx
wget http://nginx.org/download/nginx-1.19.3.tar.gz
tar -zxvf nginx-1.22.1.tar.gz

3、下载nginx-http-flv-module
下载地址:https://github.com/arut/nginx-rtmp-module.git
将包放在目录/usr/local
4、nginx编译

./configure --add-module=/usr/local/nginx-http-flv-module
make
make install

5、nginx配置文件

rtmp{
  server {
   listen 1985;

    application myapp {
       live on;
       gop_cache off;
   }
 }
}
http {
    include       mime.types;
    default_type  application/octet-stream;

   server {
     listen 8085;
     server_name localhost;

    location /live {
            flv_live on;
            chunked_transfer_encoding  on; #open 'Transfer-Encoding: chunked' response

            add_header 'Access-Control-Allow-Origin' '*'; #add additional HTTP header
            add_header 'Access-Control-Allow-Credentials' 'true'; #add additional HTTP header
        }
     location /stat {
            #configuration of streaming & recording statistics

            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }
    location /stat.xsl {
            root /usr/local/nginx-http-flv-module-master;
       }
    }
    }

推流

ffmpeg  -rtsp_transport tcp -i "RTSP地址" -vcodec libx264 -vprofile
baseline -an -tune zerolatency -preset ultrafast -g 25 -f flv
> "rtmp://nginx服务器IP:1985/myapp/test"

http://NGINX地址:8085/live?port=1985&app=myapp&stream=test

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 JavaCV 实现 RTSP HTTP-FLV 需要用到 FFmpeg 和 Netty 的相关功能。以下是实现步骤: 1. 引入 JavaCV、FFmpeg 和 Netty 相关依赖: ```xml <dependency> <groupId>org.bytedeco</groupId> <artifactId>javacv-platform</artifactId> <version>1.5.6</version> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.63.Final</version> </dependency> <dependency> <groupId>com.github.mhrimaz</groupId> <artifactId>netty-http-flv</artifactId> <version>1.0.1</version> </dependency> ``` 2. 创建 FFmpegFrameGrabber 和 FFmpegFrameRecorder,并设置相关参数: ```java FFmpegFrameGrabber grabber = new FFmpegFrameGrabber("rtsp://example.com/stream"); grabber.setOption("rtsp_transport", "tcp"); // 使用 TCP 协议传输数据 grabber.start(); FFmpegFrameRecorder recorder = new FFmpegFrameRecorder("http://localhost:8080/stream.flv", grabber.getImageWidth(), grabber.getImageHeight(), grabber.getAudioChannels()); recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264); // 设置视频编码器为 H.264 recorder.setFormat("flv"); // 设置输出格式为 FLV recorder.start(); ``` 3. 创建 Netty 的 HTTP-FLV 服务器,并将 FFmpegFrameRecorder 中的数据发给客户端: ```java EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new HttpServerCodec()); pipeline.addLast(new HttpObjectAggregator(65536)); pipeline.addLast(new HttpContentCompressor()); pipeline.addLast(new HttpFLVServerHandler(recorder)); } }); ChannelFuture future = bootstrap.bind(8080).sync(); ``` 4. 循环读取 RTSP 数据,将数据换为 FLV 格式并写入 FFmpegFrameRecorder 中: ```java Frame frame = null; while ((frame = grabber.grab()) != null) { recorder.record(frame); } ``` 5. 释放资源: ```java grabber.stop(); grabber.release(); recorder.stop(); recorder.release(); future.channel().closeFuture().sync(); workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); ``` 以上是使用 JavaCV 实现 RTSP HTTP-FLV 的基本步骤。需要注意的是,该方法需要使用 Netty 的 HTTP-FLV 服务器将数据发给客户端,因此需要对 HTTP-FLV 相关技术有一定的了解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值