15

视频文件自动转rtsp流

 3 years ago
source link: http://www.cnblogs.com/FsharpZack/p/12856160.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

最近碰到一个项目需要用到 rtsp 视频流做测试, 由于真实环境的 摄像头 并不能满足需求,故尝试了一下用本地视频文件转换成rtsp视频流做测试,记录一下~

采用方案: Docker + EasyDarwin + FFmpeg

准备工作:

1. 创建一个文件夹 easydarwin

2. cd easydarwin

3. wget https://github.com/EasyDarwin/EasyDarwin/releases/download/v8.1.0/EasyDarwin-linux-8.1.0-1901141151.tar.gz  (下载EasyDarwin 软件包)

4. 创建: Dockerfile

5. 将easydarwin 的配置文件 也放到此目录下: easydarwin.xml

6. 创建一个自动转换 /root/video 目录下的视频文件成rtsp 流的 shell 脚本: start.sh

编写Dockerfile:

FROM centos:latest
USER root

COPY ./EasyDarwin-linux-8.1.0-1901141151.tar.gz /EasyDarwin-linux-8.1.0-1901141151.tar.gz
COPY ./start.sh /start.sh
RUN mkdir -p /etc/streaming/
COPY ./easydarwin.xml /etc/streaming/easydarwin.xml
RUN yum -y install tar \
        && yum -y install gcc automake autoconf libtool make wget

RUN  gzip -d /EasyDarwin-linux-8.1.0-1901141151.tar.gz \
        && tar -xf /EasyDarwin-linux-8.1.0-1901141151.tar \
        && mv EasyDarwin-linux-8.1.0-1901141151 EasyDarwin \
	&& cd /

RUN wget https://ffmpeg.org/releases/ffmpeg-4.1.5.tar.xz \
                && tar -xf ffmpeg-4.1.5.tar.xz \
        && cd ffmpeg-4.1.5 \
                && curl -O -L http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz \
        && tar -xf yasm-1.3.0.tar.gz \
        && cd yasm-1.3.0 \
        && ./configure \
        && make \
        && make install \
        && cd /

RUN cd /ffmpeg-4.1.5 \
            && ./configure \
        && make \
        && make install \
        && cd /

ENTRYPOINT /start.sh

编写start.sh

#!/bin/sh
cnt=`ps -ef | grep "[e]asydarwin" | wc -l`
if [ $cnt -lt 1 ];then
	nohup /EasyDarwin/easydarwin &
fi
if [[ ! -d /video ]];then
	mkdir -p /video
fi

while true;do
for video in `ls /video`; do
	if [[ "$(ps -ef | grep "$video" | grep -v "grep" | wc -l | tr -d ' ' )" = "1" ]]; then
				echo "Safe $video"
	else
		short_name=`echo $video | cut -d '.' -f 1`
		nohup /ffmpeg-4.1.5/ffmpeg -re -stream_loop -1 -i /video/$video -vcodec copy -rtsp_transport tcp  -f rtsp rtsp://localhost/$short_name &
		echo "Started $video"
	fi
done

sleep 1m
done

用途说明:

Dockerfile将我们需要的环境准备好,包括安装编译安装EasyDarwin以及需要的依赖包, FFmpeg等, 最后运行 start.sh. 注意 Docker中 ENTRYPOINT 和 CMD 命令的用法区别。

start.sh 用来启动EasyEarwin,并每隔1分钟查看 /root/video 目录下的视频文件,如果有新文件,就会自动使用ffmpeg 转为rtsp流, 地址为 rtsp://localhost/short_name, 其中localhost使用时改成服务器的IP地址,short_name 为 当前食品文件的文件名(去掉扩展名, 如: video.mp4 的 short_name 为 video)

最后打包镜像,然后运行container, 可以在 easydarwin 目录的同级别创建 启动脚本: easydarwin.sh

#!/bin/bash
easy=`ls ./easydarwin/*.gz | wc -l`
if [[ $easy -lt 1 ]];then
        wget https://github.com/EasyDarwin/EasyDarwin/releases/download/v8.1.0/EasyDarwin-linux-8.1.0-1901141151.tar.gz
        mv EasyDarwin-linux-8.1.0-1901141151.tar.gz ./easydarwin
fi
local_img=`docker images | grep "[e]asydarwin" | wc -l`
if [ $local_img -lt 1 ];then
        cd easydarwin
        docker build -t easydarwin_qa . --no-cache
        cd ..
fi

#挂载host 的 /root/video 到 container 的对应路径
docker run -dit --net host --restart=always --name easy_qa -v /root/video:/video easydarwin_qa

所以只需在装了docker的服务器上启动此shell就可以: bash easydarwin.sh


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK